0
votes

I have a two part question regarding the use of LaTeX in Rmarkdown:

1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. \documentclass{...}, \begin{document}, \end{document}, etc.

I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.

2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:

\documentclass{article}
\usepackage{amsmath}

\newenvironment{spmatrix}[1]
 {\def\mysubscript{#1}\mathop\bgroup\begin{pmatrix}}
 {\end{pmatrix}\egroup_{\textstyle\mathstrut\mysubscript}}

\begin{document}
\begin{equation}
\begin{spmatrix}{A}
    a & b \\
    c & d
\end{spmatrix}
\begin{spmatrix}{x}
    x_1 \\
    x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
    b_1  \\
    b_2
\end{spmatrix}
\end{equation}
\end{document}

How would one implement this code in Rmarkdown? Do you need to move the \usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, \documentclass{article} even needed within an Rmarkdown document?

I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!

1

1 Answers

1
votes

It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.

Concerning your concrete problem:

  • amsmath is already included by the default template, which also takes care of \ḑocumentclass and the document environment.
  • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.
  • You can then use the LaTeX constructs as is in the Rmd body.

Putting things together:

---
output:
  pdf_document: 
    keep_tex: yes
    includes:
      in_header: preamble.tex
---

\begin{equation}
\begin{spmatrix}{A}
    a & b \\
    c & d
\end{spmatrix}
\begin{spmatrix}{x}
    x_1 \\
    x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
    b_1  \\
    b_2
\end{spmatrix}
\end{equation}