【发布时间】:2021-08-23 05:47:10
【问题描述】:
我有一个 RMarkdown 文件和一个包含一堆乳胶 \newcommand 调用的 LaTeX sn-p 我想放在 separate 文件中(因为我想在多个 RMarkdown 文件中重复使用它们)。这是一个可重现的小例子:test.rmd
---
title: "Test"
---
# Test Document
## Math
$$
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
$$
$$
\bmat
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\emat
$$
和 LaTeX sn-p:
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
我希望在文档标题中添加 LaTeX sn-p 并将 test.rmd 编译为独立的 HTML 文档。你是怎么做到的?
我做了两次尝试,尝试 (1) 编译器忽略元数据中的 header-includes,尝试 (2) \newcommand{\bmat}{... 在浏览器中显示为纯文本。在编译为 PDF 时,这两种尝试都有效(即将rmarkdown::html_document 替换为rmarkdown::pdf_document)。我怀疑这是因为 MathJax 与 LaTeX 编译器的工作方式非常不同,但我没有在 StackOverflow 或 docs 上找到关于此的非常明确的解释......
尝试 1
为 Pandoc 定义 common.yaml YAML 元数据文件
---
urlcolor: blue
header-includes: |
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
---
然后我编译test.rmd
Rscript -e "rmarkdown::render('test.rmd',quiet=TRUE,output_format=rmarkdown::html_document(pandoc_args=c('--metadata-file=/current/working/dir/common.yaml')))"
尝试 2
为 Pandoc 定义 header.tex 头文件
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
然后我编译test.rmd
Rscript -e "rmarkdown::render('test.rmd',quiet=TRUE,output_format=rmarkdown::html_document(pandoc_args=c('--include-in-header=/current/working/dir/header.tex')))"
【问题讨论】:
标签: latex r-markdown pandoc html-generation