Render LaTeX tables in HTML using rmarkwown

感情迁移 提交于 2021-02-11 06:08:44

问题


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}

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


回答1:


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.

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.

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*.

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.

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



来源:https://stackoverflow.com/questions/38386808/render-latex-tables-in-html-using-rmarkwown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!