Home > Enterprise >  LaTeX table displayed in HTML
LaTeX table displayed in HTML

Time:12-02

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!

CodePudding user response:

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.

CodePudding user response:

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.

  • Related