编织 R-Markdown 文档导致“!未定义的控制序列。”错误

Knitting R-Markdown document causes "! Undefined control sequence." error

我正在创建 R Markdown 文档,但在尝试打印数学公式时总是出现错误。我认为错误在 header 中的某处,但我不能完全确定。这是文档的 watered-down 版本:

---
output: 
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
header-includes: |
  \usepackage{nath}
  \usepackage{amsmath}
title: "Test Report"
toc: true
number_sections: true
graphics: yes
toc_depth: 2
df_print: kable
fontsize: 13pt
editor_options: 
  chunk_output_type: console
---

```{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)
```

**`r 5 * 20`** entries in the dataset; 


Testing mathematical symbols,  $\varepsilon _{u,i}$.  
The formula used for this is:
$$ Y_{u,i} = \mu  + \varepsilon _{u,i} $$
Where $\Y_{u,i}$ is the predicted rating per user, per movie and $\mu$ is the "true" rating for all movies. Mu-hat -> $\hat{mu}$ .

## 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.

错误信息如下:

output file: final_testing.knit.md

! Undefined control sequence.
\mathop ...\o@mathop {#1}} \else \def \mathop@arg 
                                                  {#1} \def \afterparse@ {\m...
l.112 \begin{document}

Error: LaTeX failed to compile final_testing.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See final_testing.log for more info.
Execution halted

非常感谢任何关于是什么让这一切发生的输入。 TIA

我做了以下事情:

  • 注释掉 Latex 引擎(可能是为了让 Rmarkdown 自己做出选择)
  • header-includes:
  • 之后删除了 |
  • \usepackage{}
  • 之前插入 -
  • 注释掉包 nath 因为它似乎无法加载
  • 已更改 fontsize: 12pt 因为没有 13pt
  • 删除了 white-spaces 和公式中的几个反斜杠

现在适合我,希望你也适合吗?

---
output: 
  pdf_document:
    keep_tex: yes
    # latex_engine: xelatex
header-includes: 
# - \usepackage{nath}
- \usepackage{amsmath}
title: "Test Report"
toc: true
number_sections: true
graphics: yes
toc_depth: 2
df_print: kable
fontsize: 12pt
editor_options: 
  chunk_output_type: console
---

```{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)
```

**`r 5 * 20`** entries in the dataset;


Testing mathematical symbols,  $\varepsilon_{u,i}$.  
The formula used for this is:
$$Y_{u,i} = \mu  + \varepsilon_{u,i}$$
Where $Y_{u,i}$ is the predicted rating per user, per movie and $\mu$ is the "true" rating for all movies. Mu-hat -> $\hat{mu}$.

## 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.