How do I get (LaTeX math) typeset matrix with borders in HTML output from *.Rmd?

2.9k views Asked by At

The following rmarkdown works fine when compiling to PDF (via LaTeX), but not when compiling to HTML (via Markdown).

PDF (via LaTeX)

---
title: "test"
author: "Maximilian Held"
output: pdf_document
documentclass: memoir
---

(@matrix-test2) $$
\bordermatrix{
  ~       & Petra & Ingrid \cr
  Petra   & 1     & 0 \cr
  Ingrid  & 0     & 1 \cr
}
$$

tex


HTML (via Markdown)

---
title: "test"
author: "Maximilian Held"
output: html_document
documentclass: memoir
---

(@matrix-test2) $$
\bordermatrix{
  ~       & Petra & Ingrid \cr
  Petra   & 1     & 0 \cr
  Ingrid  & 0     & 1 \cr
}
$$

yields

html

What's up with that and on whose end is this (Pandoc? Markdown? MathML?)

3

There are 3 answers

0
Raniere Silva On BEST ANSWER

knitr, the tool normally used to convert RMarkdown to HTML/PDF (from LaTeX)/DOCX, uses Pandoc. Pandoc is a nice tool to convert Markdown to HTML and LaTeX and allows you to use LaTeX math environments inside Markdown, e.g.

$a x^2 + b x + c = 0$

or

$$a x^2 + b x + c = 0$$

or

\begin{equation}
a x^2 + b x + c = 0
\end{equation}

are properly converted by Pandoc. Pandoc also supports the amsmath, a very popular LaTeX package for math. Unfortunately, Pandoc doesn't support all (La)TeX commands/environment as you discovered.

What I always do when working with Pandoc is try to keep things simple. In the case you present, I would use normal tables instead of a matrix.

0
SabDeM On

I believe this behavior is normale because \bordermatrix is a LaTeX command and it works well when you compile a PDF (which uses LaTeX), but I do not think direct LaTeX markup can be converted in HTML even in a normal document without specific class specification (you use memoir in fact).

---
title: "prova"
author: "SabDeM"
date: "09 giugno 2015"
output: html_document
---

\begin{equation}
x^2
\end{equation}

This example still doesn't work well. So I think that is very probable that you have to convert in another way. I advise you to store your data in an R object and then use the kable function and change the format to html or latex whenever you like.

Edit

I saw that \bordermatrix is not even a LaTeX command but a plain TeX one. This can make things worse or better, it depends on the internal parsing structure of knitr, which I have no idea.

0
mb21 On

Pandoc has a couple of different options for math rendering when exporting to HTML.

You can choose between using various JavaScript libraries like jsMath, MathJax and KaTeX, but I think neither supports the \bordermatrix command. There's also MathML which few browsers support. Finally, if you really want advanced TeX math commands like this to work, Pandoc can work with gladTeX or mimeTeX to render the math to images which will be embedded in the HTML.