4

I have the following LateX table that renders as expected when format: pdf:

---
title: "Test Table"
format: pdf
---

\begin{center}
\begin{tabular}{|l|l|l|}
\hline
Var           & Class  & Description\\
\hline
$x $             &  numeric      &   xyz \\
$y$            &  numeric      &   xzh \\
$z $          &  integer      &   xlp \\
\hline
\end{tabular}
\end{center}

enter image description here

I look for possibilities that this table also get's displayed in HTML format, e.g. format: html. I have many (many) LaTeX tables that I need to transform, hence I hope for a solution that avoids the manual work to write them all as markdown tables. Any help is as always much appreciated!

shafee
  • 15,566
  • 3
  • 19
  • 47
Julian
  • 6,586
  • 2
  • 9
  • 33
  • 1
    Unrelated to the question, but you might want to read the documentation of the booktabs package on how to create professional looking tables instead of such data prisons... – samcarter_is_at_topanswers.xyz Dec 01 '22 at 13:37
  • If you feel better, I can make a booktabs example - would still work for the question ;) And I totally agree with you, I would never have produced such tables myself.. – Julian Dec 01 '22 at 13:43
  • 1
    What a relieve to hear :) – samcarter_is_at_topanswers.xyz Dec 01 '22 at 13:45
  • Does latex syntax works for html?! :) – shafee Dec 01 '22 at 13:52
  • That is essentially my question, if there is some kind of conversion possible... – Julian Dec 01 '22 at 13:55
  • 2
    This is the scenario for which I've written the [parse-latex](https://github.com/tarleb/parse-latex) Quarto extension. – tarleb Dec 05 '22 at 13:02
  • 1
    That is amazing, that was exactly what I have been dreaming about! Could you post it as an answer? I am unable to install it however, I create an issue and we can discuss it there. Thank you so much @tarleb! – Julian Dec 05 '22 at 13:12
  • 1
    @Julian Maybe Quarto is too old. I seem to remember that extensions work best with Quarto 1.2 or later? Please do raise an issue on that repo if it's still not working. – tarleb Dec 05 '22 at 13:27

3 Answers3

3

You can use the array environment of MathJax with the help of the xtable package:

---
title: "Untitled"
output: html_document
date: "2022-12-01"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(xtable)
```

```{r, results='asis'}
dat <- data.frame(
  "\\text{Var}" = c("x", "y", "z"),
  "\\text{Class}" = c("\\text{numeric}", "\\text{numeric}", "\\text{integer}"),
  check.names = FALSE
)

M <- print(xtable(dat, align=rep("|c|", ncol(dat)+1)), 
           floating = FALSE, tabular.environment="array", 
           comment=FALSE, print.results=FALSE, 
           include.rownames = FALSE,
           sanitize.text.function = function(x) x)
cat(M)
```

enter image description here

Or you can directly type the array:

\begin{array}{|l|l|l|}
\hline
\text{Var}   & \text{Class}     & \text{Description} \\
\hline
x            &  \text{numeric}  &   \text{xyz}       \\
y            &  \text{numeric}  &   \text{xzh}       \\
\hline
\end{array}

The array environment is in math mode, so you don't have to put the $ for maths symbols, and you have to use \text for text mode.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thank you Stephane! Unfortunately, the LaTeX tables are almost all written as `tabular` tables and it would involve lot of manual labor to transform those to array (especially with the \text) I know had the idea to write a function to translate the LateX code to markdown code such that it can displayed as LaTeX as well as HTML code. – Julian Dec 05 '22 at 09:09
3

The parse-latex Quarto extension was written with this scenario in mind. It works by using pandoc's LaTeX parser to process all raw LaTeX snippets in the document, thereby making it possible to convert those to arbitrary formats.

The conversions are limited by pandoc's LaTeX parser; especially styling-information will not be preserved in the conversion.

For completeness, and because it's fairly short, here is the full code of the pandoc Lua filter shipped in that extension. It can also be used directly by saving it to a file parse-latex.lua and then using it with filters: [parse-latex.lua] in the YAML header of the qmd file.

--- parse-latex.lua – parse and replace raw LaTeX snippets
---
--- Copyright: © 2021–2022 Albert Krewinkel
--- License: MIT – see LICENSE for details

-- Makes sure users know if their pandoc version is too old for this
-- filter.
PANDOC_VERSION:must_be_at_least '2.9'

-- Return an empty filter if the target format is LaTeX: the snippets will be
-- passed through unchanged.
if FORMAT:match 'latex' then
  return {}
end

-- Parse and replace raw TeX blocks, leave all other raw blocks
-- alone.
function RawBlock (raw)
  if raw.format:match 'tex' then
    return pandoc.read(raw.text, 'latex').blocks
  end
end

-- Parse and replace raw TeX inlines, leave other raw inline
-- elements alone.
function RawInline(raw)
  if raw.format:match 'tex' then
    return pandoc.utils.blocks_to_inlines(
      pandoc.read(raw.text, 'latex').blocks
    )
  end
end
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • also works for my other [question](https://stackoverflow.com/questions/74655561/indented-enumeration-including-equation/74701348#74701348) :) – Julian Dec 06 '22 at 11:01
2

To answer this question in general,

Not all latex syntax or environment is supported for html output rendering. You can only use those that are supported by MathJax or KaTeX or other listed here.

And you need to read the docs of these rendering methods to know which TeX commands they support for html rendering and convert the latex code accordingly.

For example,

  • For MathJax, Supported Tex LaTex commands contains such an exclusive list of TeX commands that MathJax supports for html rendering.

  • For KaTeX, this page contains list of TeX commands supported by KaTeX.

shafee
  • 15,566
  • 3
  • 19
  • 47