R 和 RStudio 升级后,R Markdown 文件中“header-includes”中的 LaTeX 代码出现问题

R 和 RStudio 升级后,R Markdown 文件中“header-includes”中的 LaTeX 代码出现问题

我在我的 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的定义放入名为的文件中,然后将其添加到部分中。如果您愿意,可以添加您正在加载的所有额外包和命令。您可能必须将我的更改恢复为,因为我已将其更改为在系统上适合我的更改。microtypeMT@is@compositemystyles.stymystyles.styheader-includes\definecolor\pagenumbermystyles.sty\setmainfontWindows

---
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文件之前确保您已解决。

相关内容