Create a LaTeX Preamble for Thai Language in R Markdown
Source:R/use_thai_preamble.R
use_thai_preamble.Rd
This function creates LaTeX preamble file needed to render Thai language in R Markdown to a PDF document.
Usage
use_thai_preamble(
name = "thai-preamble.tex",
thai_font = "TH Sarabun New",
line_spacing = 1.5,
open = FALSE,
overwrite = FALSE
)
Arguments
- name
(Character) Thai LaTeX preamble file name or path of file to create, which can be relative path or absolute path. Default value is
thai-preamble.tex
.- thai_font
(Character) Name of the Thai font to use. Default font is "TH Sarabun New". It can be any Thai font that installed in your system.
- line_spacing
(Numeric) Spacing between each line. Line spacing 1.5 is recommended for Thai language (default).
- open
(Logical) Open the newly created file for editing? Using default editor of
.tex
to open.- overwrite
(Logical) If file already exist, do you want to overwrite?
Details
Here is the steps:
Call
use_thai_preamble()
. You can set other file name byname
argument and Thai font to use bythai_font
argument.Follow the instructions printed to R console.
File
thai-preamble.tex
should be created in the current working directory (by default).(If not already) Create R Markdown file with
pdf_document:
orbookdown::pdf_document2
format.Modify YAML header in
pdf_document:
orbookdown::pdf_document2
option. Setlatex_engine
toxelatex
and set to include path to the LaTeX preamble file.Add LaTeX macro
\sloppy
to the beginning of the body of R Markdown (just after YAML header).
Write some Thai language in R Markdown then knit to PDF. It's DONE!
Examples
if (FALSE) {
# Running this will write `thai-preamble.tex` to your working directory
use_thai_preamble()
# Write `thai-preamble.tex` under pre-tex/ directory (a directory must exist)
use_thai_preamble(name = "pre-tex/thai-preamble.tex")
# Specify Thai font to use
use_thai_preamble(thai_font = "Laksaman")
}
# Example
.old_wd <- setwd(tempdir())
use_thai_preamble()
#> ✔ Writing "thai-preamble.tex" at /tmp/RtmpDJQsyk/thai-preamble.tex
#> ✔ Thai font was set to "TH Sarabun New" in the preamble.
#> ✔ Line spacing was set to 1.5 in the preamble.
#>
#> ── TODO ────────────────────────────────────────────────────────────────────────
#> For YAML header of R Markdown in `pdf_document:` or `bookdown::pdf_document2:`
#> • Set `latex_engine` to `xelatex`
#> • Set to include the path to LaTeX preamble
#>
#> ── Like This ───────────────────────────────────────────────────────────────────
#> latex_engine: xelatex
#> includes:
#> in_header: thai-preamble.tex
#>
#> ────────────────────────────────────────────────────────────────────────────────
#>
#> • Add LaTeX macro `\sloppy` to the beginning of the body of R Markdown (just
#> after YAML header).
#>
#> For more details see
#> • How to include preamble in R Markdown
#> <https://bookdown.org/yihui/rmarkdown-cookbook/latex-preamble.html>
#> • LaTeX setting in Thai
#> <http://pioneer.netserv.chula.ac.th/~wdittaya/LaTeX/LaTeXThai.pdf>
setwd(.old_wd)