Search code examples
htmllatexr-markdownxaringan

Label rows and columns of a matrix in R Markdown with Latex and HTML rendering


I am having trouble rendering some Latex code with R Markdown in HTML using the excellent xaringan R package. Specifically I would like to label the rows and columns of a matrix as described here. The thing is that, as far as I understand, some Latex packages cannot be used in R Markdown. Therefore, I am using a <div class="math">My matrix in Latex here</div>. I'm almost there but I keep getting random opening single quotes that I cannot get rid of:

opening single quotes in matrix

Must be something silly. Any help would be much appreciated.

The Rmd code is as follows:

---
title: 'Matrix'
output:
  xaringan::moon_reader
---

<div class="math">
\[
\begin{array}{cc} &
\begin{array}{ccc} a & b & c
\end{array}
\\
\begin{array}{ccc}
p \\
q \\
r
\end{array}
&
\left(
\begin{array}{ccc}
.1 & .1 & 0 \\
.4 & 1 & 0 \\
.8 & 0 & .4
\end{array}
\right)
\end{array}
\]
</div>

Solution

  • This is a bug of xaringan. A workaround is to move \end{array} to the end of the previous line, e.g.,

    ---
    title: 'Matrix'
    output:
      xaringan::moon_reader
    ---
    
    <div class="math">
    \[
    \begin{array}{cc} &
    \begin{array}{ccc} a & b & c\end{array}
    \\
    \begin{array}{ccc}
    p \\
    q \\
    r\end{array}
    &
    \left(
    \begin{array}{ccc}
    .1 & .1 & 0 \\
    .4 & 1 & 0 \\
    .8 & 0 & .4\end{array}
    \right)\end{array}
    \]
    </div>