Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 310
user2070174 Avatar asked Apr 13 '21 14:04

user2070174


People also ask

Can you use LaTeX in R markdown?

In our experience, it is also mostly workable to include LATEX code in R markdown documents. Recall that documents in R markdown are converted from Rmd to md by the knitting process, and then into LATEX by pandoc, and then into PDF by a LATEX compiler (pdflatex, xetex or similar).

How do I create a column in R markdown?

If you're using R Studio with the visual editor, you can add columns using Insert > DIV , creating an empty wrapper DIV of class . columns and adding child DIV's with Classes set to . column and width=x% in 'Other'.

How do I add text in R markdown?

Rmd file, you may now add text to your document. You may start with the most basic word processing—simply typing your text below the YAML header section. If you want to start a new paragraph, you can simply hit Enter twice on your key- board, and begin writing again.


1 Answers

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>
like image 84
Yihui Xie Avatar answered Sep 18 '22 07:09

Yihui Xie