我在我的 R Markdown 文档(生成 PDF 作为输出)的 YAML 中使用以下 LaTeX 代码:
---
output:
pdf_document:
latex_engine: xelatex
header-includes:
- |
```{=latex}
\usepackage{microtype}
\usepackage{fontspec}
\setmainfont{Times.otf}[
BoldFont = TimesBold.otf]
\usepackage{ragged2e}
\renewcommand{\footnotesize}{\scriptsize \justify}
\usepackage{setspace}
\usepackage{xcolor}
\definecolor{very-light-gray}{gray}{0.95}
\pagenumbering{gobble}
\makeatletter
\def\MT@is@composite#1#2\relax{%
\ifx\\#2\\\else
\expandafter\def\expandafter\MT@char\expandafter{\csname\expandafter
\string\csname\MT@encoding\endcsname
\MT@detokenize@n{#1}-\MT@detokenize@n{#2}\endcsname}%
% 3 lines added:
\ifx\UnicodeEncodingName\@undefined\else
\expandafter\expandafter\expandafter\MT@is@uni@comp\MT@char\iffontchar\else\fi\relax
\fi
\expandafter\expandafter\expandafter\MT@is@letter\MT@char\relax\relax
\ifnum\MT@char@ < \z@
\ifMT@xunicode
\edef\MT@char{\MT@exp@two@c\MT@strip@prefix\meaning\MT@char>\relax}%
\expandafter\MT@exp@two@c\expandafter\MT@is@charx\expandafter
\MT@char\MT@charxstring\relax\relax\relax\relax\relax
\fi
\fi
\fi
}
% new:
\def\MT@is@uni@comp#1\iffontchar#2\else#3\fi\relax{%
\ifx\\#2\\\else\edef\MT@char{\iffontchar#2\fi}\fi
}
\makeatother
---
我在 StackExchange 上的一篇文章中找到了这样的代码(不幸的是我再也找不到了),并且它从现在起就一直有效。在更新 R 和 RStudio(以及一些 R 包)后,我总是收到以下 LaTeX 错误:
! LaTeX Error: Missing \begin{document}.
更准确地说,这个错误不是由于文档中其余代码的问题造成的。我隔离了这个问题,发现它与“header-includes:”部分中的一段代码有关。
您有什么建议可以让我的代码再次运行吗?
谢谢你!
答案1
欢迎来到 TeX.SE。我更改了语法,并将宏header-includes
的定义放入名为的文件中,然后将其添加到部分中。如果您愿意,可以添加您正在加载的所有额外包和命令。您可能必须将我的更改恢复为,因为我已将其更改为在系统上适合我的更改。microtype
MT@is@composite
mystyles.sty
mystyles.sty
header-includes
\definecolor
\pagenumber
mystyles.sty
\setmainfont
Windows
---
output:
pdf_document:
keep_tex: true
latex_engine: xelatex
html_document:
df_print: paged
header-includes:
- \usepackage{microtype}
- \usepackage{fontspec}
- \setmainfont{Times New Roman} #<- changed this
- \usepackage{ragged2e}
- \renewcommand{\footnotesize}{\scriptsize\justify}
- \usepackage{setspace}
- \usepackage{xcolor}
- \definecolor{very-light-gray}{gray}{0.95}
- \pagenumbering{gobble}
- \input{mystyles.sty}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
这是的内容mystyles.sty
:
\makeatletter
\def\MT@is@composite#1#2\relax{%
\ifx\\#2\\\else
\expandafter\def\expandafter\MT@char\expandafter{\csname\expandafter
\string\csname\MT@encoding\endcsname
\MT@detokenize@n{#1}-\MT@detokenize@n{#2}\endcsname}%
% 3 lines added:
\ifx\UnicodeEncodingName\@undefined\else
\expandafter\expandafter\expandafter\MT@is@uni@comp\MT@char\iffontchar\else\fi\relax
\fi
\expandafter\expandafter\expandafter\MT@is@letter\MT@char\relax\relax
\ifnum\MT@char@ < \z@
\ifMT@xunicode
\edef\MT@char{\MT@exp@two@c\MT@strip@prefix\meaning\MT@char>\relax}%
\expandafter\MT@exp@two@c\expandafter\MT@is@charx\expandafter
\MT@char\MT@charxstring\relax\relax\relax\relax\relax
\fi
\fi
\fi
}
% new:
\def\MT@is@uni@comp#1\iffontchar#2\else#3\fi\relax{%
\ifx\\#2\\\else\edef\MT@char{\iffontchar#2\fi}\fi
}
\makeatother
然后,您的标题内容就会出现在文件的前言中.tex
,正如预期的那样。我添加了keep_tex: true
保留YAML
生成的中间.tex
文件,以便我们可以看到标题中发生了什么。
\usepackage{microtype}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage{ragged2e}
\renewcommand{\footnotesize}{\scriptsize\justify}
\usepackage{setspace}
\usepackage{xcolor}
\definecolor{very-light-gray}{gray}{0.95}
\pagenumbering{gobble}
\input{mystyles.sty}
如果您遇到问题,请Clear Knitr Cache
在重新执行.Rmd
文件之前确保您已解决。