使用 r-markdown 在 HTML 中渲染 LaTeX 表格 [英] Render LaTeX tables in HTML using r-markdown

查看:19
本文介绍了使用 r-markdown 在 HTML 中渲染 LaTeX 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to render the following table in a RMD file:

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

到目前为止还没有成功.rmarkdown 无法将 LaTeX 环境(方程式除外)编译为 HTML 有什么根本原因吗?

So far no success. Is there any fundamental reason why rmarkdown cannot compile LaTeX enviroments (other than equations) to HTML?

推荐答案

在 Markdown 文档中,预期的输入标记语言是 (r)降价.您不应该期望 pandoc 自动识别任意混合的标记语言.LaTeX 数学标记只能在 markdown 文档中使用,因为有一个 rmarkdown 扩展 来处理这个.

In a markdown document, the expected input markup language is (r)markdown. You should not expect pandoc to automatically recognize arbitrarily mixed markup languages. LaTeX math markup can only be used in markdown documents because there is a rmarkdown extension to handle this.

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

However, it is still possible to use a LaTeX table like the one shown in the question in a rmarkdown document. I demonstrated the "inverse" (markdown table in RNW document) in this answer. Please note that this a rather experimental approach that might fail in other situations.

tex2markdown 函数背后的想法在这里进行了解释.

The idea behind the function tex2markdown is explained 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 = "
")
  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,但对于简单的任务,这应该可以.请注意,反斜杠需要通过额外的反斜杠进行转义.

Not all LaTeX features can be converted to HTML, but for simple tasks this should work. Note that backslashes need to be escaped by an additional backslash.

这主要是一个概念证明.对于生产,在 RNW 文档中使用 LaTeX 表,在 RMD 文档中使用 markdown 表!

This is mainly a proof of concept. For production, use LaTeX tables in RNW documents and markdown tables in RMD documents!

这篇关于使用 r-markdown 在 HTML 中渲染 LaTeX 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆