使用rmarkwown以HTML格式渲染LaTeX表

时间:2016-07-15 02:03:57

标签: r latex knitr r-markdown

我正在尝试在RMD文件中呈现下表:

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|}
\hline
 \\ \hline
\end{tabular}
\end{table}
到目前为止还没有成功。有没有什么根本原因让rmarkdown无法将LaTeX环境(方程式除外)编译为HTML?

1 个答案:

答案 0 :(得分:2)

在降价文档中,预期的输入标记语言为(r)markdown。您不应期望pandoc能够自动识别任意混合的标记语言。 LaTeX数学标记只能用于降价文档,因为有rmarkdown extension来处理它。

但是,仍然可以在rmarkdown文档中使用类似问题中显示的LaTeX表。我在this answer中演示了“逆”(RNW文件中的降价表)。请注意,这是一种相当实验性的方法,在其他情况下可能会失败。

tex2markdown功能背后的想法被解释为here

---
output: html_document
---

# My document

This is `rmarkdown`.

This table is converted from LaTeX:
```{r, results = "asis", echo = FALSE, message = FALSE}
library(knitr)

tex2markdown <- function(texstring) {
  writeLines(text = texstring,
             con = myfile <- tempfile(fileext = ".tex"))
  texfile <- pandoc(input = myfile, format = "html")
  cat(readLines(texfile), sep = "\n")
  unlink(c(myfile, texfile))
}

textable <- "
\\begin{table}[]
\\centering
\\caption{Food order}
\\begin{tabular}{| l | l |}
\\hline
 Hamburgers & 3 \\\\ 
 Hot dogs & 2 \\\\ \\hline
\\end{tabular}
\\end{table}
"

tex2markdown(textable)
```

--- 

Time for *lunch*.

并非所有LaTeX功能都可以转换为HTML,但对于简单的任务,这应该可行。请注意,反斜杠需要通过额外的反斜杠进行转义。

这主要是概念证明。对于生产,使用RNW文档中的LaTeX表和RMD文档中的降价表!

相关问题