My R Markdown (.Rmd) file looks like this:
---
title: Foo
author: Marius Hofert
header-includes:
- \usepackage{bm}
output:
pdf_document
vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Foo}
---
\[
\begin{align}
\bm{U}=a\bm{X}\quad\boldmath{U}=a\boldmath{X}\quad\mathbf{U}=a\mathbf{X}.
\end{align}
\]
The output (obtained via R CMD build
and the looking in ./inst/doc/*.html
) is this:
For getting italics bold vectors, I would like to use \bm{X}
in my .Rmd
document, but it fails (although I load the package bm
). Why? The same happens without the output: pdf_document
part.
UPDATE
If I'm running
---
title: Foo
author: Marius Hofert
header-includes:
- \usepackage{bm}
output:
pdf_document
vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Foo}
---
\[
\begin{align}
\bm{U}=a\bm{X}\quad\boldmath{U}=a\boldmath{X}\quad\mathbf{U}=a\mathbf{X}.
\end{align}
\]
\[
\bm{U}=a\bm{X}\quad\boldmath{U}=a\boldmath{X}\quad\mathbf{U}=a\mathbf{X}.
\]
\begin{align}
\bm{U}=a\bm{X}\quad\boldmath{U}=a\boldmath{X}\quad\mathbf{U}=a\mathbf{X}.
\end{align}
I get (without errors)
I don't think Mathjax (which is what Pandoc uses in HTML output) can
\usepackage{}
. I work around this by having 2 files: one calledpreamble-mathjax.tex
, one calledpreamble-latex.tex
My YAML metadata is set up like this (forrmarkdown
):And
preamble-mathjax.tex
has (note surrounding\( \)
so that mathjax parses as a maths block)while
preamble-latex.tex
has:So that whenever I use
\bm{..}
in my document, it works whether I compile to HTML or PDF. (stacking theboldsymbol
with themathbf
so that both greek letters and normal letters are made bold, and bold letters remain upright as they would if you used\bm
).Peripheral to your question: Eventually you may wish to have a third file,
preamble-both.tex
, with macros that are not package-specific (supposing the relevantpreamble-*
has already been included) e.g.And then you include this with both output formats. This saves you from writing all your macros twice, once for html_document and again for pdf_document. However, MathJax requires the macros to be surrounded by
\(
and\)
, whereas LaTeX will error out if this is the case.The only way I've found to work around this is to have a file
bracket-start.txt
containing just\(
and a filebracket-end.txt
containing just\)
, so that my YAML is:which is pretty unwieldy, but it works (there is the Pandoc
latex_macros
extension, but it has never worked for me)A third solution works well if you are using Mathjax to render equations in your html docs. Mathjax is on by default in pandoc in RStudio. This has the advantage of no flickering and works in $$ $$ and the equation environment. The big downside to this is that pandoc strips \ref{} out of the html so you have to add a knit hook to change to \ref{}. Surely there is a way to tell pandoc not to do this, but I couldn't find it. I tried many different pandoc args with no success.
This example assumes you want equation numbers and you want to crossref those in your text. It also assumes you are knitting from RStudio. Probably works otherwise, but that is what I tested in.
mathjax.js --- define the macros here and tell Mathjax to add eqn #s
defs.tex --- this is for pdf_document
test.Rmd --- now we can make Rmd docs that work with html and pdf. I could not figure out how to tell pandoc not to strip LaTeX commands out of the html. So passed in a knit hook to double \ the \ref{} calls.
Here is the knit function:
( function(inputFile, encoding) { if(rmarkdown::all_output_formats(inputFile)[1]=="html_document"){ f <- inputFile; x <- readLines(f); y <- gsub("[\]ref[{]","\\\\ref{", x); cat(y,file="tmp.Rmd", sep="\n"); rmarkdown::render("tmp.Rmd", encoding = encoding ) }else{ rmarkdown::render(inputFile, encoding = encoding ) } })
I think your
\[ \]
and\begin{align} ... \end{align}
are redundant. When I ran it as written above I gotWorked fine for me when I deleted
\begin{align} ... \end{align}
...(It seems that a similar issue arose in your previous question too ...)
(Perhaps you were getting errors that you didn't notice and were accidentally looking at a previously compiled version?)
As far as why you don't get the right HTML output: I'm pretty certain that MathJax (the engine used to render LaTeX embedded in Rmarkdown-produced HTML) doesn't know about
\boldmath
; adding the package to your LaTeX input won't help, you'll have to use\mathbf
and\boldsymbol
instead. You can play around here to see what works and what doesn't: enteringat that web page gives
Bottom line, if you want fancy math rendered properly, you're probably better off sticking to PDF output.
Another solution is to use the child chunk argument. The downside, which is kind of major, is that it will only work for math surrounded by $ $ or $$ $$. It won't work in the equation environment. The upside is that you do not get your definitions "flashing" at the top of your html pages for a moment, which happens to me with the solution above.
demo.Rmd
defs.tex