使用rmarkwown在HTML中渲染LaTeX表 [英] Render LaTeX tables in HTML using rmarkwown

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

问题描述

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

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

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{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?

推荐答案

在降价文档中,预期的输入标记语言为(r)降价.您不应该期望pandoc自动识别任意混合的标记语言. LaTeX数学标记只能在标记文档中使用,因为要处理 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.

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

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 = "\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,但是对于简单的任务,它应该可以工作.请注意,反斜杠需要通过附加的反斜杠进行转义.

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文档中的降价表!

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

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

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