Fernando Hoces De La Guardia Fernando Hoces De La Guardia - 2 months ago 10x
LaTeX Question

Render LaTeX tables in HTML using rmarkwown

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

\caption{My caption}
\\ \hline

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


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}

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 <- "
\\caption{Food order}
\\begin{tabular}{| l | l |}
 Hamburgers & 3 \\\\ 
 Hot dogs & 2 \\\\ \\hline



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!