【问题标题】:How do I get amsmath to work in RMarkdown when knitting to PDF?编织成 PDF 时如何让 amsmath 在 RMarkdown 中工作?
【发布时间】:2020-01-25 04:39:16
【问题描述】:

在 RMarkdown 中,我有一个想要编织成 pdf 的文档。该文档有我需要自动编号的方程式。我一直使用$$ 1+1=2 \tag{1} $$ 约定来编写方程式,但现在想切换到\begin{equation} 1+1=2 \eq:this_eq \end{equation} 约定,以便我可以自动编号并轻松交叉引用方程式。我发现的少数在线资源使这看起来应该相当简单。例如herehere。但是,在尝试这样做时,我遇到了无穷无尽的心碎。

我正在使用带有 RStudio 的 3.4.3 版、tinytex 发行版,并且已经安装了 bookdown(我仍然不确定是否真的有必要在这里实现我的目标)。这是一个重复:

---
title: This title
author: "This guy"
date: "This date"
header-includes:
   - \usepackage{amsmath}
output:
  pdf_document:
    toc: yes
    toc_depth: '4'
    df_print: kable
    fig_caption: yes
    latex_engine: xelatex
mainfont: Calibri Light
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Introduction

blah blah...

\begin{equation}
S = X \bar{P}
(\#eq:signals)
\end{equation}

## Later on

blah blah \@ref(eq:signals)

当我尝试“编织到 PDF”时,我遇到了错误

! Package mathspec Error: `amsmath' must be loaded earlier than `mathspec'.

已被报告为错误并“修复”here,但我无法理解修复或遵循其说明。我要的是一组非常明确的步骤,可以让我顺利地运行上面的 repex。

我尝试过的一些事情:

当我将pdf_document 替换为bookdown::pdf_document2 时,也会出现同样的错误。或者当我删除

header-includes:
   - \usepackage{amsmath}

改为放

includes:
  in_header: preamble.tex

latex_engine: xelatex 行之后,其中“preamble.tex”是包含\usepackage{amsmath} 行的记事本文件

this other SO post 中的 cmets 似乎表明,甚至没有必要在 YAML 选项中说任何关于 amsmath 的内容,这让我更加困惑。当我从 YAML 选项中删除任何提及 amsmath 时,我收到错误消息,指出无法识别 mathjax 脚本,例如:

! Package amsmath Error: \bar allowed only in math mode.

【问题讨论】:

    标签: yaml r-markdown knitr bookdown tinytex


    【解决方案1】:

    当我尝试您的示例时,方程式未成功标记。

    然后我将输出设置替换为bookdown::pdf_book。它有效。

    ---
    title: This title
    author: "This guy"
    date: "This date"
    output:
      bookdown::pdf_book
    mainfont: Calibri Light
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ## Introduction
    
    blah blah...
    
    \begin{equation}
      S = X \bar{P}
      (\#eq:signals)
    \end{equation}
    
    ## Later on
    
    blah blah \@ref(eq:signals)
    

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-03-24
      相关资源
      最近更新 更多