Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr/markdown: how to render math equations in vignettes built by CRAN?

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.

enter image description here

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,

enter image description here

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 ?

like image 457
user101089 Avatar asked Oct 03 '16 18:10

user101089


People also ask

How do you show math in markdown?

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.

Can I use LaTeX in R markdown?

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).

How do you write a fraction in R markdown?

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.


1 Answers

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().

like image 186
A Bev Avatar answered Oct 11 '22 09:10

A Bev