使用r-markdown将LaTeX表格渲染为HTML

8

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

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|}
\hline
 \\ \hline
\end{tabular}
\end{table}

迄今为止还没有成功。除了公式以外,rmarkdown不能将LaTeX环境编译成HTML的根本原因是什么?请注意,保留HTML标记。

1个回答

9
在一个Markdown文档中,期望的输入标记语言是(r)markdown。你不应该期望pandoc自动识别任意混合的标记语言。LaTeX数学标记只能在Markdown文档中使用,因为有一个rmarkdown扩展来处理它。
然而,在rmarkdown文档中仍然可以使用类似于问题中所示的LaTeX表格。我在这个答案中演示了“反向”(在RNW文档中的Markdown表格)。请注意,这是一种相当实验性的方法,可能在其他情况下失败。
函数tex2markdown背后的思想在这里解释了。
---
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文档中使用markdown表格!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接