I have a RMarkdown file and a LaTeX snippet containing bunch of latex \newcommand
calls I want to place in separate file (since I want to reuse them across multiple RMarkdown files). Here's a small reproducible example: test.rmd
---
title: "Test"
---
# Test Document
## Math
$$
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
$$
$$
\bmat
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\emat
$$
and the LaTeX snippet:
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
I wish to add the LaTeX snippet in the document header and compile test.rmd
to a standalone HTML document. How do you do this?
I have made two attempts, in attempt (1) the compiler is ignoring header-includes
in the metadata, and in attempt (2) the \newcommand{\bmat}{...
is shown in the browser as plain-text. Both of these attempts work when compiling to PDF (i.e. replacing rmarkdown::html_document
by rmarkdown::pdf_document
). I suspect this is because MathJax works very differently from LaTeX compilers but I haven't found a very clear explanation on StackOverflow or docs about this...
Attempt 1
Define common.yaml
YAML metadata file for Pandoc
---
urlcolor: blue
header-includes: |
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
---
then I compile test.rmd
by doing
Rscript -e "rmarkdown::render('test.rmd',quiet=TRUE,output_format=rmarkdown::html_document(pandoc_args=c('--metadata-file=/current/working/dir/common.yaml')))"
Attempt 2
Define header.tex
header file for Pandoc
\newcommand{\bmat}{\begin{bmatrix}}
\newcommand{\emat}{\end{bmatrix}}
then I compile test.rmd
by doing
Rscript -e "rmarkdown::render('test.rmd',quiet=TRUE,output_format=rmarkdown::html_document(pandoc_args=c('--include-in-header=/current/working/dir/header.tex')))"