Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access MathJax extensions (like siunitx) from Rmarkdown via Pandoc & Knitr?

Working with Rmarkdown in Rstudio, using pandoc and knitr, I am targetting PDF output via LaTeX and HTML output with MathJax. I would like to use some of the MathJax extensions that are available, to allow richer LaTeX for the PDF target. Specifically, I am trying to use the siunitx extension right now, although I am also interested in others (e.g. physics).

Using siunitx works fine with LaTeX for PDF output, but I've had a hard time getting it working with HTML output.

Here an example Rmarkdown file:

---
title: "siunitx test"
author: "chriss"
date: "June 13, 2017"
output:
  html_document:
    mathjax: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML
    number_sections: yes
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
    number_sections: yes
header-includes: \usepackage{siunitx}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# The Problem

I would like to be able to use `siunitx` latex macros from `Rmarkdown`,
targetting PDF output via latex and html with MathJax. It should get me proper
formatting of things like $\SI{120}{\W\per\square\m}$ and $\SI{0.8}{\A\per\W}$,
as long as I put them in a latex math environment, so that MathJax picks them
up.

The PDF output is OK when I add the `header-includes: \usepackage{siunitx}` to
the `YAML` header, but how can I access the MathJax `siunitx` extension via the
knitr -> pandoc -> mathjax/html route?

Check: is MathJax working in general: $\frac{1}{r^2}$

This knits fine to PDF, but the $\SI{}{}$ are output verbatim and hilighted red in the HTML output, and in RStudio. I'm having pandoc get MathJax from rawgit.org, since the default of cdn.mathjax.org is soon-to-be defunct, and it seems, no longer has a Contrib path with the extensions.

I have tried adding MathJax's $\require{siunitx}$ with variations on the path to the siunitx extension, to no avail. This causes the HTML to look for the siunitx extension, but apparently in the wrong place: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/extensions/TeX/siunitx.js?V=2.7.1, which is a 404.

If I remove the \require{} and remove the part of the output HTML file that loads MathJax dynamically (labelled <!-- dynamically load mathjax for compatibility with self-contained -->), and manually add:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]},
  errorSettings: {message: undefined},
    TeX: { extensions: ["[burnpanck]/siunitx/unpacked/siunitx.js"] }
  };
  MathJax.Ajax.config.path['burnpanck']  = 
'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
</script>
<script type="text/javascript" 
src="https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-
MML_HTMLorMML"></script>

To header of the HTML file, then it briefly pops up a complaint about some issue with siunitx.js but produces correct output (this is a modified version of the header from the example for the siunitx MathJax extension, from here )

This suggests that I could modify the HTML template for pandoc to reflect those changes, and things would basically work.

However, the following questions remain:

  • Is changing the HTML template in this way the proper way to fix the HTML output? Are these the URLs that are intended to be used now that cdn.mathjax.org is going down, or are there better ones that I should use instead?
  • Why do I still get the warning about siunitx.js?
  • What would need to be done to have Rstudio understand the siunitx content in its preview? Is there already a way to enable this (e.g. convince it to use siunitx extension, assuming it's built on MathJax), or would this be a feature request..?

Indeed, it would be nice if there was an easy way to access the MathJax extensions out-of-the-box, without having to go to the trouble of editing templates and the like, with proper handling in the Rstudio GUI. I can imagine there may be Rstudio users who would benefit from the extra functionality but don't want to / aren't able to jump through these kind of hoops to access it.

UPDATE The warning message I see when loading the 'working' HTML about siunitx.js seems to be a general issue with the current version of siunitx.js, due to changes to the MathJax CDN, see issue raised here: https://github.com/burnpanck/MathJax-third-party-extensions/issues/5

like image 473
chriss Avatar asked Oct 30 '22 06:10

chriss


1 Answers

I'm using includes in_header to solve the problem.

---
title: "doku1"
output:
  html_document:
    includes:
     in_header: header.html
  pdf_document:
    keep_tex: yes
    latex_engine: pdflatex
    number_sections: no
header-includes: \usepackage{mhchem, siunitx}
---

header.html looks like this

<script type="text/x-mathjax-config">
MathJax.Ajax.config.path["mhchem"] = "https://cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.3.2";
MathJax.Ajax.config.path['myExt']  = 'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
MathJax.Hub.Config({
  TeX: { extensions: ["AMSmath.js","AMSsymbols.js","[myExt]/siunitx/unpacked/siunitx.js","[mhchem]/mhchem.js", "color.js"] }
  });
</script>

It works, but is rather slow.

John

like image 131
user11316043 Avatar answered Nov 09 '22 10:11

user11316043