rlatexmarkdown

How to get \bm{} to work in an R markdown (to HTML) file?


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:

enter image description here

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)

this


Solution

  • I think your \[ \] and \begin{align} ... \end{align} are redundant. When I ran it as written above I got

    ! Package amsmath Error: Erroneous nesting of equation structures; (amsmath) trying to recover with `aligned'.

    See the amsmath package documentation for explanation. Type H for immediate help. ...

    l.84 \end{align}

    Worked 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: entering

    $\bm X \boldmath X \boldsymbol X \mathbf X$
    

    at that web page gives

    MathJax output: red "\bm" black math-italic X; red "\boldmath" math-italic X; bold Roman X; bold math-italic X

    Bottom line, if you want fancy math rendered properly, you're probably better off sticking to PDF output.