【问题标题】:How to add LaTeX command definitions when compiling from RMarkdown to HTML从 RMarkdown 编译到 HTML 时如何添加 LaTeX 命令定义
【发布时间】: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


    【解决方案1】:

    我找到了问题的答案。尝试 (2),除了标题必须是 HTML 文件,比如 header.html 和内容

    \(
    \newcommand{\bmat}{\begin{bmatrix}}
    \newcommand{\emat}{\end{bmatrix}}
    \)
    

    注意\(\) 的使用。现在我可以使用了

    Rscript -e "rmarkdown::render('test.rmd',quiet=TRUE,output_format=rmarkdown::html_document(pandoc_args=c('--include-in-header=/current/working/dir/header.html')))"
    

    请注意,不能在 sn-p 中添加\usepackage,如提到的here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2021-03-12
      • 2015-05-31
      • 2021-05-10
      • 2021-08-21
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多