从 R Markdown 到 LaTeX 的转换不正确
Incorrect conversion from R Markdown to LaTeX
为什么以下 R Markdown 最小(非)工作示例无法编译为 PDF?
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
特别是,有问题的转换发生在 -\lfoot{From: K. Grant}
和 -\cfoot{To: Dean A. Smith}
上,如输出 .tex
文件所示:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true
出于某种原因,这两行都被转换为 true
,导致
LaTeX error: Missing \begin{document}
从而阻止文档编译为 PDF。
将 \lfoot
和 \cfoot
更改为任何其他内容似乎会导致它们被正确转换。那么这是怎么回事?我认为一定是转换过程中knitr
或pandoc
有问题。
注意:我对 R Markdown 不太熟悉,这是 TeX.SX 代表 Tom 向 Headers and footers created in Fancyhead not shown in PDF 提出的后续问题。
:
字符是问题所在。 pandoc
好像是想把header-includes
的内容当成变量来解析,而:
是用来分隔变量和值的。如果您引用有问题的行,它会编译(然后不要忘记转义前导反斜杠)
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\lfoot{From: K. Grant}"
- "\cfoot{To: Dean A. Smith}"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
通过试错拼接答案here,发现只有删除title
键,使用YAMLpipe-denoted多行字符串语法才能编译:
---
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
---
我建议使用 pandoc 的 raw_attribute
feature(默认启用)来标记原始 LaTeX 部分。这是最稳健的解决方案。
header-includes: |
```{=latex}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
```
为什么以下 R Markdown 最小(非)工作示例无法编译为 PDF?
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
特别是,有问题的转换发生在 -\lfoot{From: K. Grant}
和 -\cfoot{To: Dean A. Smith}
上,如输出 .tex
文件所示:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true
出于某种原因,这两行都被转换为 true
,导致
LaTeX error: Missing
\begin{document}
从而阻止文档编译为 PDF。
将 \lfoot
和 \cfoot
更改为任何其他内容似乎会导致它们被正确转换。那么这是怎么回事?我认为一定是转换过程中knitr
或pandoc
有问题。
注意:我对 R Markdown 不太熟悉,这是 TeX.SX 代表 Tom 向 Headers and footers created in Fancyhead not shown in PDF 提出的后续问题。
:
字符是问题所在。 pandoc
好像是想把header-includes
的内容当成变量来解析,而:
是用来分隔变量和值的。如果您引用有问题的行,它会编译(然后不要忘记转义前导反斜杠)
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\lfoot{From: K. Grant}"
- "\cfoot{To: Dean A. Smith}"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
通过试错拼接答案here,发现只有删除title
键,使用YAMLpipe-denoted多行字符串语法才能编译:
---
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
---
我建议使用 pandoc 的 raw_attribute
feature(默认启用)来标记原始 LaTeX 部分。这是最稳健的解决方案。
header-includes: |
```{=latex}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
```