Repository navigation
Description
Thanks for the great package!
Custom URLs for MathJax aren't working for me in Bookdown. I have tried setting the mathjax option in _output.yml as below. However the rendered HTML just uses the out of date version 2.7.2 of MathJax from mathjax.rstudio.com/latest, not the version I have specified.
Am I doing something wrong, or is this a bug?
I can see in RStudio that the mathjax variable argument has been passed to the pandoc command line call. However it seems that the variable isn't being processed into the template gitbook.html.
I asked asked about this on StackOverflow, but I got no response. [Edit: fixed link]
Minimal example:
_output.yml:
bookdown::gitbook:
css: style.css
mathjax: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
index.Rmd:
---
title: "Untitled"
author: "An author"
site: bookdown::bookdown_site
documentclass: book
---
$$E=mc^2$$
Session info:
> xfun::session_info('rmarkdown')
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6, RStudio 1.3.959
Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8
Package version:
base64enc_0.1.3 digest_0.6.25 evaluate_0.14
glue_1.4.1 graphics_3.6.3 grDevices_3.6.3
highr_0.8 htmltools_0.5.0 jsonlite_1.7.0
knitr_1.29 magrittr_1.5 markdown_1.1
methods_3.6.3 mime_0.9 rlang_0.4.7
rmarkdown_2.3 stats_3.6.3 stringi_1.4.6
stringr_1.4.0 tinytex_0.24 tools_3.6.3
utils_3.6.3 xfun_0.15 yaml_2.2.1
Pandoc version: 2.9.2.1
By filing an issue to this repo, I promise that
- I have fully read the issue guide at https://yihui.org/issue/.
- I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included
xfun::session_info('bookdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/bookdown'). - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
cderv commentedon Jul 20, 2020
Yes I think this is a bug.
In bookdown, to change the mathjax CDN used by gitbook, currently I think this should be passed as pandoc variable in
index.Rmdyaml header because the template use a special js scriptbookdown/inst/templates/gitbook.html
Lines 169 to 183 in aa6bd19
however, this does not behave as it should either, because
is rendered as a html link before being passed to pandoc template. I need to understand why.
Anyway, I think it should work as you tried.
When passing
mathjaxin the output format argument, it passes to--mathjaxflag in pandoc commandline and this setmathjaxvariable to true , andmathjaxurlto the url.I think, that gitbook.html template should use that
mathjaxurlvariable.Thanks for the report. I'll think about the correct fix
cderv commentedon Aug 26, 2020
@yihui so I have looked into this and I think we don't currently support properly setting a custom mathjax url with
bookdown::gitbook.Currently, all the mathjax handling is dealt in
rmarkdown:::pandoc_mathjax_argsmainly. What causing issue here is thatrmarkdowndoes a correct handling only for its internal template (html_document(template = "default")) and as bookdown use its own template, we don't benefit from it in bookdown.Here my analysis of the situation, to be a base of comparaison with a potential fix; I spend some time on it, mainly to be up to date with how mathjax is handle in rmarkdown and bookdown.
Current situation
Let's note first that
bookdown::gitbookwithself_contained = TRUEdoes not work because mathjax won't be included. I think it should as we ofter to customiseself_containedargument.In fact this is due to the handling of mathjax in
rmarkdown:::pandoc_mathjax_argwhich remove mathjax is another template than the one included with rmarkdown. Obviously,bookdown::gitbook()is using another templateNow on our issue here, currently custom url is not taken into account in the gitbook template. This is because only
mathjaxpandoc variable is used (and it is logical with no value set)Then,
mathjax = "local"is not supported in gitbook either - the included code is still the same.For no mathjax asked, it is working - no mathjax is used.
I also tried with custom template, it has mostly the same behavior than rmarkdown for now.
We create a dummy template to show the pandoc variables set for the rendering, and we test several scenarios.
That means that with a custom template, it is to the user responsability to do something based on pandoc feature mainly. (But let's note there is no easy way to get the full url with the query part in
mathjaxurlvariable)Rmarkdown mathjax-url is never sets to helps create another self-contained format.
What do we want to do ?
gibook()?mathjax = "local"also, and more generally have the same behavior in gitbook template than indefault.htmlin rmarkdown ?Depending on the choices, we could
Choice 1 doesn't allow to remove the warning when
self_contained = TRUEbut choice 2 can enable that. Do we want to support self contained for gitbook (not sure).I will share the work I have done for 1, but I would be interested in your thinking on all this. Thanks !
yihui commentedon Aug 26, 2020
Thanks @cderv! I need more time to digest this, but let me ask a quick question first: will it help if we remove the restriction here https://github.com/rstudio/rmarkdown/blob/204aa41faea12212dc80642f5a8e53e4e52e0999/R/pandoc.R#L478-L483?
cderv commentedon Aug 27, 2020
Yes this is the part in Rmarkdown that is currently activated when using Gitbook. I see two impacts of this :
this restriction on
self_containedwith the warning is to me the cause ofbookdown::gitbook()not having mathjax whenself_contained = TRUE. In this case,--mathjaxis not added to command line pandoc and I guess default pandoc rendering is used. (again I don't know if anyone use some "self contained gitbook" and if it really makes sense - never used it myself). So math appears in the doc (I tried on bookdown-demo) but they are not rendered using mathjax.currently if the default rmarkdown template is not used (by default, we mean
template = "default"forhtml_documentonly, so thedefault.htmlfile in rmarkdown), rmarkdown only sets the--mathjaxpandoc command line argument.That is what happens with
gitbook()because a custom template is used (from rmarkdown point of view).It means we rely on pandoc mathjax handling for template, :
$math$variable that contains the<script>line, transformed when--self-containedis used$mathjax$variable, logical and equal TRUE is--mathjaxis used$mathjaxurl$variable, contains the url (up until the query part, so before any?), mainly to be used in default beamer template from my understanding.but currently we only support the default Rstudio's mathjax url if mathjax is to be used.
bookdown/inst/templates/gitbook.html
Lines 175 to 176 in aa6bd19
My current PR tries to solve 2 from bookdown directly. If we remove (or change the restriction) on rmarkdown side that would be different. I mentioned that in the PR discussion. 1 is blocked by rmarkdown restriction I guess - we could bypass it but we'll still get the warning.
I am wondering is this rmarkdown restriction is also impacting other 📦 BTW 🤔 - will look into it.
cderv commentedon Aug 27, 2020
Another thought on that: If fact, it seems it is really discourage, from an old comment of yours #61 (comment) where you say
However, it is not really specified in the bookdown book : https://bookdown.org/yihui/bookdown/html.html#gitbook-style
only:
I really wonder if we can't force
self_contained = FALSEin gitbook and use the$math$pandoc variable directly in thegitbook.htmlpandoc template.sentausa commentedon Nov 9, 2020
cderv commentedon Nov 9, 2020
sentausa commentedon Nov 9, 2020
cderv commentedon Nov 9, 2020
cderv commentedon Nov 9, 2020
sentausa commentedon Nov 9, 2020
sentausa commentedon Nov 10, 2020
@cderv finally I put my question on StackOverflow. Thanks!
cderv commentedon Mar 8, 2022
New extended math support in rmarkdown brings a workaround to this by switching to Katex rendering.
See here: https://community.rstudio.com/t/cannot-load-mathtools-mathjax-extension-in-bookdown/131073/2?u=cderv
this is not a solution and now that new rmarkdown is on CRAN we'll improve support in bookdown.
piiskop commentedon Jul 30, 2022
With
r-katexlabelling doesn't work and it doesn't recognizemhchem's\ce.pglpm commentedon May 26, 2023
I was wondering whether there are any updates on this issue. As I explain in this Stackoverflow question, I'd like to use the upgreek MathJax extension in Bookdown, but it's only available in V3, which cannot be used in Bookdown at present. Unfortunately the KaTeX workaround doesn't work either, as KaTeX doesn't offer upright lowercase Greek.
cderv commentedon May 26, 2023
Not yet. Work still need to be done and format Gitbook tested with Mathjax 3. For now you would need a HTML post processing probably to tweak the HTML and replace the url. Sorry for this.
I'll move this up in the list.
Also possible
bs4_book()format does not have this issue.pglpm commentedon May 26, 2023
Thank you! Great to hear it's on the todo list. I'll take a look at
bs4_bookin the meantime.Thank you again for this great package!
cderv commentedon May 26, 2023
If you are working on a new Book, you could also have a look at the new Quarto publishing system. This could be seen a "the next generation of R Markdown" for R users. It builds on R Markdown experience and opens it to the non-R world for anyone doing scientific publishing no matter the tool (R, Python, Julia, ...)
There is a book format there: https://quarto.org/docs/books/
Just sharing in case you don't know. No need to change if bookdown works for you.
pglpm commentedon May 26, 2023
Hi Christophe, just tried Quarto, and upright lowercase greek works natively. Thank you for the great suggestion.
This reply is a little off-topic, so feel free to delete it.
cderv commentedon May 26, 2023
I'll keep it ! Really great feedback thanks ! :)