LoginSignup
11
17

More than 3 years have passed since last update.

RStudioを用いたRMarkdownによる日本語PDFファイル出力のために必要なこと

Last updated at Posted at 2020-01-05

日本語とggplot2を利用したpdfファイル出力

はじめに

RStudioからrmarkdownを用いて簡単にソースコード, text, 動的に得られた図表を埋め込んだ
文書を作成できる. デフォルト設定ではhtmlファイルを出力する場合のみ容易に実現できる.
日本語を用いたpdfファイル出力, さらに日本語文字列を含むggplot2による画像の埋め込みなども行うには, LaTeX関連の設定が必要となる. ここではそれをまとめておく.

更新日

2020/01/06

利用環境

動作確認に用いた環境をまとめておく.

  • OS: Windows 10 home
  • RStudio: 1.2.5019
  • R: 3.6.2
  • rmarkdown: 1.X (失念したが2.0ではない. 2.0の場合についても言及する.)

必要なライブラリ等のinstall

  • LaTeX環境: TinyTeX
  • フォント: ipaex
install.packages("tinytex") # Rのtinytex パッケージ
tinytex::install_tinytex()  # TinyTeX本体のインストール
tinytex::tlmgr_install("ipaex") # IPAexフォントのインストール

pandoc template file の修正

注意: rmarkdown 2.0 の場合はpandoc template 指定がなく, yamlでgeometryについて記述しないだけで良い.

knit (rmarkdown::render) を実行すると出てくるエラー

! LaTeX Error: Option clash for package geometry.

への対処として,
yamlでno指定した上で, pandocのtemplateファイル中のgeometry部分をコメントアウト.

knit実行のlogをみると, 下のようにpandocを実行している部分がある.

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS sample_fig_float_adjustment.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output sample_fig_float_adjustment.tex --template "c:\Users\your_user_name_here\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --pdf-engine xelatex --include-in-header preamble_latex.tex --variable graphics=yes --lua-filter "c:/Users/your_user_name_here/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "c:/Users/your_user_name_here/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua" --variable "compact-title:yes" 

この例だと
--template
で指定されているファイル default-1.17.0.2.tex
について, 下のように%でコメントアウトする.

$if(geometry)$
%\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$

ちなみにrmarkdown 2.0の場合は下のようなコマンドが実行される.

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS sample_fig_float_adjustment.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output sample_fig_float_adjustment.tex --self-contained --highlight-style tango --pdf-engine xelatex --include-in-header preamble_latex.tex --variable graphics --lua-filter "c:/Users/your_user_name/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "c:/Users/your_user_name/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua" --include-in-header "C:\Users\your_user_name\AppData\Local\Temp\RtmpWGApkP\rmarkdown-str496066c047fe.html" --include-in-header "C:\Users\your_user_name\AppData\Local\Temp\RtmpWGApkP\rmarkdown-str49603faa69d7.html"

rmarkdownファイルのyamlヘッダ

日本語pdf出力するために以下のような設定とする.

output:
  pdf_document: 
    latex_engine: xelatex 
    number_sections: true
documentclass: bxjsarticle
header-includes: 
  - \usepackage{zxjatype} 
  - \usepackage[ipa]{zxjafont} 
geometry: no

rmarkdown v2.0の場合は

geometry: no

をつけない. つけるとエラーになる.

knitrオプション設定

ggplot2で日本語含む図を出力する際に必要となる.

knitr::opts_chunk$set(
  dev = "cairo_pdf",
  dev.args = list(family = "ipaexg")
)

補足: 画像出力位置調整

コードブロックと画像の出力位置が前後しないように順に出力したい.

preamble fileを作成してそれを読み込むことでlatex設定を変えることで実現する.
ただし, yamlのheader_includes と, includes: in_header: は両立できないようである (例外はあるようだが条件不明). そこでpreamble fileを使う場合には, 日本語利用のためのusepackage命令も含めて記述し, header_includesを使わないようにする.

output:
  pdf_document: 
    latex_engine: xelatex 
    number_sections: false
    includes:
      in_header: preamble_latex.tex
documentclass: bxjsarticle
geometry: no

注意: rmarkdown 2.0 の場合はgeometry: noは付けない

preamble_latex.tex

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}
% include_headersで指定していたusepackage文をこちらに書いておく
\usepackage{zxjatype} 
\usepackage[ipa]{zxjafont}

サンプル実装など

にまとまっている.

11
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
17