Jupyter-to-LaTex | How to render tables?

My attempts to convert from beautiful Jupyter notebook format (with output generated by R code) to publication-ready LaTex format continue. Tables are not being rendered as desired.

Here’s where I’m at …

• The R data.frame output using Jupyter’s default display DOES convert correctly.
• The R data.frame output using knitr’s kable function and IRdisplay’s display_html function renders correctly in Jupyter, but DOES NOT render at all in LaTex/PDF – the generated HTML is just ignored in the conversion process.
• The R data.frame output using knitr’s kable function and IRdisplay’s display_latex function does render correctly in LaTex/PDF, but DOES NOT render in Jupyter – only the LaTex code is displayed.
• The markdown table from a markdown cell DOES convert correctly.

Here’s what I did …

• little_test.ipynb (a Jupyter notebook) is converted to little_test.tex (a latex document) using pandoc like this:
pandoc “little_test.ipynb” --output=”little_test.tex” --to=latex --lua-filter=rah.lua --standalone --extract-media=images --number-sections --shift-heading-level-by=1 --dpi=125 -V documentclass=book -V block-headings -V papersize=letter -V fontsize=10pt -V margin-left=1in -V margin-right=1in -V margin-top=1in -V margin-bottom=1in

• little_test.tex (a ltext document) is edited slightly like this:
\setcounter{chapter}{8}
\setcounter{section}{5} % section number start minus one

\begin{center}
\includegraphics[width=4in]{images/decision-model_pets_provider.jpg}
\end{center}

• little_test.tex (a latex document) is converted to little_test.pdf (a PDF document) using MikTex’s TeXworks.

Also, notice little_test (from Chrome print).pdf, which is generated by the Chrome browser print applied directly to little_test.ipynb. Using this alternative approach, all the tables (and all html images) are rendered correctly. So, I can see there must be some way to do it. But of course, I’d still need the LaTex so that I can adjust the styling.

How to get HTML-format tables from Jupyter to LaTex? -or-
How to display (kable-generated) LaTex-format tables in Jupyter?

1 Like

Cross-posted here.

Here’s the solution thanks to tarleb on Stack Overflow:

The below Lua filter will parse and, if possible, convert all raw HTML blocks in your input. That should make it possible to render tables as HTML, but still have them show up in the final output. Use it by saving the code to a file parse-html.lua and pass it to pandoc via --lua-filter=parse-html.lua.

function RawBlock (raw)
  if raw.format:match 'html' then
    return pandoc.read(raw.text, 'html').blocks
  end
end
1 Like