In a package, I have several vignettes that use mathematical notation, like:
This vignette illustrates the ideas behind solving systems of linear equations of the form $\mathbf{A x = b}$
where
- $\mathbf{A}$ is an $m \times n$ matrix of coefficients for $m$ equations in $n$ unknowns
- $\mathbf{x}$ is an $n \times 1$ vector unknowns, $x_1, x_2, \dots x_n$
- $\mathbf{b}$ is an $m \times 1$ vector of constants, the "right-hand sides" of the equations
When I compile the vignette using R Studio (Ctrl+Shift+K), it renders properly in the HTML file, whether viewed in the R Studio viewer or the browser.
Yet, when the package is submitted to CRAN, and the vignettes are built there from the source package, the math is not rendered properly, but appears as markup,
Is there something I need to add to my YAML header so that vignettes built outside of R Studio will render mathematics properly? Currently, I use just a standard header:
---
title: "Solving Linear Equations"
author: "Me"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Solving Linear Equations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
The generated pandoc
command from R Studio is
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS linear-equations.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc8141d044063.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\R\R-3.2.5\library\rmarkdown\rmd\h\default.html" --css "C:\R\R-3.2.5\library\rmarkdown\rmarkdown\templates\html_vignette\resources\vignette.css" --mathjax --variable "mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --highlight-style pygments
Perhaps I need to add a YAML option corresponding to --self-contained
or --standalone
?
Math inside RMarkdown In side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”. Do not leave a space between the $ and your mathematical notation.
In our experience, it is also mostly workable to include LATEX code in R markdown documents. Recall that documents in R markdown are converted from Rmd to md by the knitting process, and then into LATEX by pandoc, and then into PDF by a LATEX compiler (pdflatex, xetex or similar).
We can write fractions: $\frac{2}{3}$. We can also handle things like estimated population growth rate, e.g., $\hat{\lambda}=1.02$. And, $\sqrt{4}=2$. Note that this equation looks quite nice above where it's in display math mode.
Posting years later in case another user has the same question. I noticed the same issues rendering equations in my vignettes---not on CRAN, just locally hosted packages. Ctrl + Shift + K rendered fine, but viewing with vignette()
in the RStudio Help panel renders the equations badly.
I think this is just an issue that the Help panel doesn't render the HTML completely. If I view the packaged vignette in a browser, e.g. using RShowDoc()
, then the equations are fine. So maybe users should be directed to RShowDoc()
more often than vignette()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With