Search code examples
htmlrlatexpandocquarto

LaTeX table displayed in HTML


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, i.e. 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!


Solution

  • 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