A.3 LaTeX

只有当你想要将书籍输出为 PDF 时,你才需要 LaTeX。你可以通过访问 https://www.latex-project.org/get/ 来获取更多关于 LaTeX 及其安装的信息,但是我们强烈推荐你安装名为 TinyTeX 的轻量级跨平台 LaTeX 发行版,它是基于 TeX Live 构建的。TinyTeX 能够通过 R 包 tinytex 轻松安装(安装 bookdown 时将自动安装):

tinytex::install_tinytex()

使用 TinyTeX,你应该永远不会看见这样的错误信息:

! LaTeX Error: File `titling.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

Enter file name: 
! Emergency stop.
<read *> 
         
l.107 ^^M

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted

上面的错误信息表示你使用了一个包含 titling.sty 文件的 LaTeX 包,但并没有安装该软件包。LaTeX 包名通常是 *.sty 的格式,因此在本例中,你可以尝试安装 titling 包。如果你在 R Markdown 中使用 TinyTeX,缺少的 LaTeX 包将会自动安装,因此无需担心此类问题。

LaTeX 发行版和其软件包也会不时进行更新,你可以考虑更新它们,特别是当遇到 LaTeX 问题时。你可以通过以下方式查看 LaTeX 发行版的版本:

system('pdflatex --version')
##  pdfTeX 3.141592653-2.6-1.40.26 (TeX Live 2024)
##  kpathsea version 6.4.0
##  Copyright 2024 Han The Thanh (pdfTeX) et al.
##  There is NO warranty.  Redistribution of this software is
##  covered by the terms of both the pdfTeX copyright and
##  the Lesser GNU General Public License.
##  For more information about these matters, see the file
##  named COPYING and the pdfTeX source.
##  Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
##  Compiled with libpng 1.6.43; using libpng 1.6.43
##  Compiled with zlib 1.3.1; using zlib 1.3.1
##  Compiled with xpdf version 4.04

你可以运行以下代码来更新 TinyTeX:

tinytex::tlmgr_update()

随着时间的推移,你可能也需要升级 TinyTeX(否则你无法安装或更新任何 LaTeX 软件包),在这种情况下你需要重新安装 TinyTeX:

tinytex::reinstall_tinytex()