Matrix with 3 dots does not render to pdf in Rmarkdown

Viewed 258

I want to show a matrix in Rmarkdown pdf file. Using basic LaTeX I created my desired matrix representation:

$$X_{123} = \begin{bmatrix}
a_{1} & a_{1} & a_{1} & \dots & a_{1} & a_{1} & a_{1} & a_{1} & a_{1} & a_{1} & a_{1}\\
\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots &\ \vdots\\
a_{n} & a_{n} & a_{n} & \dots & a_{n} & a_{n} & a_{n} & a_{n} & a_{n} & a_{n} & a_{n}\\
\end{bmatrix}$$

When I type this matrix in Rmarkdown file, the preview window shows my desired matrix:

enter image description here

However, when I knit the Rmd file to pdf I get this error:

! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate 

When I delete all the \vdots and \dots which create the 3 dot symbols the rendering works fine. What am I missing here?

1 Answers

This looks like a LaTeX problem rather than an RMarkdown problem. If you delete any column (so the matrix only has 10 columns, not 11) it is fine.

The reason it works in the preview is that RStudio uses MathJax, not LaTeX, for rendering.

Searching online for "10 column limit in matrix in amslatex" finds this link: https://tex.stackexchange.com/questions/3519/how-to-use-more-than-10-tab-stops-in-bmatrix-or-other-amsmath-matrix-environment, which explains that you can increase the limit by using

\setcounter{MaxMatrixCols}{20}
Related