Dambo Dambo - 1 month ago 9
LaTeX Question

Incosistency between RMardkown preview and pdf output

I have this latex code in an RMarkdown file in R studio:

---
title: "matrix"
date: "November 2, 2016"
output:


pdf_document: default
---

$$
\mathbf{y} = \left[\begin{array}
{rrr}
y_{1,1} \\
y_{4,3}

\end{array}\right]
\mathbf{X} = \left[\begin{array}
{rrr}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0
\end{array}\right]
$$


The preview shows up fine in Rstudio:enter image description here
But when I knit to pdf I get this error message:

! LaTeX Error: \mathbf allowed only in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...

l.88 \$\$ \mathbf{y}

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted


How can I fix it?

Answer

Works okay if you change to an explicit math environment:

---
title: "matrix"
date: "November 2, 2016"
output:
  pdf_document: default
---

\begin{equation}
\mathbf{y} = \left[\begin{array}
{r}
y_{1,1} \\
y_{4,3} 

\end{array}\right]
\mathbf{X} = \left[\begin{array}
{rrrr}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 
\end{array}\right]
\end{equation}

except you also want to fix your array alignments, otherwise

Extra alignment tab has been changed to \cr.

(you need just {r} for the first, and {rrrr} for the second)

enter image description here