Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

markdown

r

latex

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

like image 861
Marius Hofert Avatar asked Oct 20 '15 01:10

Marius Hofert


People also ask

How to use R Markdown in R?

R Markdown is a free, open source tool that is installed like any other R package. Use the following command to install R Markdown: Now that R Markdown is installed, open a new R Markdown file in RStudio by navigating to File > New File > R Markdown…. R Markdown files have the file extension “.Rmd”. 2. Default Output Format

How do I convert a Rmarkdown file to another format?

The rmarkdown package will use the pandoc program to transform the file into a new format. For example, you can convert your .Rmd file into an HTML, PDF, or Microsoft Word file. You can even turn the file into an HTML5 or PDF slideshow. rmarkdown will preserve the text, code results, and formatting contained in your original .Rmd file.

How do I convert a markdown file to an HTML file?

RStudio will open the Markdown Quick Reference guide in the Help pane. To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want.

How do I use Rmarkdown with knitr?

The rmarkdown package will call the knitr package. knitr will run each chunk of R code in the document and append the results of the code to the document next to the code chunk. This workflow saves time and facilitates reproducible reports. Consider how authors typically include graphs (or tables, or numbers) in a report.


2 Answers

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

enter image description here

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

like image 114
Ben Bolker Avatar answered Oct 23 '22 09:10

Ben Bolker


I don't think Mathjax (which is what Pandoc uses in HTML output) can \usepackage{}. I work around this by having 2 files: one called preamble-mathjax.tex, one called preamble-latex.texMy YAML metadata is set up like this (for rmarkdown):

output:
    html_document:
       includes:
         before_body: preamble-mathjax.tex
    pdf_document:
       includes:
         in_header: preamble-latex.tex

And preamble-mathjax.tex has (note surrounding \( \) so that mathjax parses as a maths block)

\(
\newcommand{\bm}[1]{\boldsymbol{\mathbf{#1}}}
\)

while preamble-latex.tex has:

\usepackage{bm}

So that whenever I use \bm{..} in my document, it works whether I compile to HTML or PDF. (stacking the boldsymbol with the mathbf 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 relevant preamble-* has already been included) e.g.

\newcommand{\bX}{\bm{X}} % bold X
\newcommand{\R}{\mathbb{R}} % real numbers

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 file bracket-end.txt containing just \), so that my YAML is:

output:
    html_document:
       includes:
         before_body: [preamble-mathjax.tex, bracket-start.txt, preamble-both.tex, bracket-end.txt]
    pdf_document:
       includes:
         in_header: preamble-latex.tex
         before_body: preamble-both.tex

which is pretty unwieldy, but it works (there is the Pandoc latex_macros extension, but it has never worked for me)

like image 32
mathematical.coffee Avatar answered Oct 23 '22 10:10

mathematical.coffee