How to load MathJax extensions in Bookdown?
Asked Answered
C

1

6

Update: I think the problem is related to these two bookdown-bug reports: Cannot load mathtools MathJax extension in bookdown and Custom MathJax URL not working. The problem is that the upgreek extension requires MathJax V3, but bookdown/Rstudio uses the outdated V2.7. According to those bugs it is at present not possible to use V3 in bookdown.

Writing a text with Bookdown I would like to use upright Greek letters in maths. MathJax provides the upgreek extension for this, but I do not manage to load it or make it work in Bookdown. This is what I tried:

Following the Bookdown manual, I created an html file called "mathjax.html", containing the following script:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  loader: {load: ['[tex]/upgreek']},
  TeX: { TagSide: "right" }
});
</script>

then I include:d it in the "_output.yml" configuration file:

bookdown::gitbook:
  includes:
    in_header: mathjax.html
[...]

But upgreek letters are still not rendered (for example, an $\uppi$ outputs a red \uppi, signaling an unknown macro).

I don't understand where the problem is. The MathJax config script above tries to follow both the instruction in the Bookdown manual, which uses MathJax.Hub.Config({});, and the instruction in the MathJax manual, which instead uses window.MathJax = {};. I tried also using the latter, but upgreek letters are still not rendered. I'm quite ignorant about the syntax of these kinds of script.

My general question is: how can I make Bookdown load particular MathJax packages?

Chen answered 23/5, 2023 at 6:54 Comment(6)
It's hard to know from your question whether what you're facing is a MathJax rendering problem or bookdown not finding the linked html file, or loading it at the correct time. If it's the latter, you may be able to insert the html directly in the header as in this answer. The snippets you've posted are helpful but it would be easier to isolate the problem if you included a minimum reproducible example.Toboggan
@Toboggan Thank you for the suggestions! I just tried the first and unfortunately it doesn't work (it compiles, but I still don't see the upcase pi). I'll try to add a reproducible example.Chen
Did you try loading the upgreek latex package via a .tex file, as they do in the linked Bookdown reference?Gabriellegabrielli
Alternatively, you said you tried the window.MathJax example. Did you try the MathJax.Hub.Config({}); example, but replacing your TeX: { TagSide: "right" } with tex: {packages: {'[+]': ['upgreek']}} (as per the MathJax-ref syntax)?Gabriellegabrielli
@Toboggan I think I found the culprit, see update in post.Chen
@Gabriellegabrielli Thank you for the suggestions. Yes I tried those and they don't work. The problem seems to be that Bookdown uses V2.7 of MathJax.Chen
C
2

I report here the workaround I used – a completely different but apparently superior approach – in case it's of use to others.

According to this brilliant suggestion on github, I'm now using Quarto to create the book, instead of Bookdown. Upright lowercase Greek letters are implemented out of the box, as are many other latex-maths niceties. I think the reason is that MathJax 3 is used by Quarto.

Note that if you change the HTML maths implementation to KaTeX (for example by specifying

format: 
  html: 
    code-fold: true
    html-math-method: katex

in the "_quarto.yml" file) then upright lowercase Greek doesn't work because it isn't (yet) implemented in KaTeX.

Chen answered 26/5, 2023 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.