This couldresolve#915
It is only a suggestion for modificaiton inbookdown
only. There are other solution to deal with that more broadly for all the formats:
- tweak rmarkdown
pandoc_mathjax_args
in pre processor - (maybe) Use a lua filter to change the Meta information if possible (just an idea for now)
But on this PR, here is what has changed based on the analysis in#915 (comment)
# on the branch custom-mathjax from this PRpkgload::load_all()#> Loading bookdowntemp_file<- tempfile(fileext=".Rmd")xfun::write_utf8(c("---","title: Test","---","","# test","","$1+1$"),temp_file)render_gitbook<-function(...) {# we insure pandoc 2.7.3 for nowrmarkdown::find_pandoc(version="2.7.3")res<-xfun::in_dir(tempdir(),rmarkdown::render(temp_file,"bookdown::gitbook",output_options=list(...),quiet=TRUE) )html<-xfun::read_utf8(res)i<- grep("<!-- dynamically load mathjax for compatibility with self-contained -->",html)if (!length(i))return("no Mathjax")start_s<- grep("<script>",html)end_s<- grep("</script>",html)xfun::raw_string(html[start_s[start_s>i][1]:end_s[end_s>i][1]])}custom_mathjax<-"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
This PR changes nothing at the fact gitbook and mathjax does not work whenself_contained = TRUE
render_gitbook(self_contained=TRUE)#> Warning: MathJax doesn't work with self_contained when not using the rmarkdown#> "default" template.#> [1] "no Mathjax"render_gitbook(self_contained=TRUE,mathjax=custom_mathjax)#> Warning: MathJax doesn't work with self_contained when not using the rmarkdown#> "default" template.#> [1] "no Mathjax"
Custom url now works. See the value ofsrc
below
render_gitbook(mathjax=custom_mathjax)#> <script>#> (function () {#> var script = document.createElement("script");#> script.type = "text/javascript";#> var src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";#> if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";#> if (location.protocol !== "file:")#> if (/^https?:/.test(src))#> src = src.replace(/^https?:/, '');#> script.src = src;#> document.getElementsByTagName("head")[0].appendChild(script);#> })();#> </script>
it works also with mathjax local now. Seesrc
below
render_gitbook(mathjax="local")#> <script>#> (function () {#> var script = document.createElement("script");#> script.type = "text/javascript";#> var src = "libs/mathjax-local/MathJax.js?config=TeX-AMS-MML_HTMLorMML";#> if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";#> if (location.protocol !== "file:")#> if (/^https?:/.test(src))#> src = src.replace(/^https?:/, '');#> script.src = src;#> document.getElementsByTagName("head")[0].appendChild(script);#> })();#> </script>
with no mathjax asked, it is still working - no mathjax is used.
render_gitbook(mathjax=NULL)#> [1] "no Mathjax"
with custom template, we can decide to pass themathjax-url
variable for use in custom template
library(htmltools)html_content<- div(# hack to work with previous function render_gitbook HTML("<!-- dynamically load mathjax for compatibility with self-contained -->"),tags$script( p("this is mathjax variable: $mathjax$"), br(), p("this is mathjaxurl variable: $mathjaxurl$"), br(), p("this is mathjax-url variable: $mathjax-url$"), br(), p("this is math variable: $math$") ))template<- tempfile(fileext=".html")xfun::write_utf8(as.character(html_content),template)
custom templates can use pandoc mathjax-url variable, that corresponds
but it is not used by pandoc - So I guess we should choice what should happen here.
render_gitbook(mathjax=custom_mathjax,template=template)#> <script>#> <p>this is mathjax variable: true</p>#> <br/>#> <p>this is mathjaxurl variable: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js</p>#> <br/>#> <p>this is mathjax-url variable: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js</p>#> <br/>#> <p>this is math variable: <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script></p># same for mathjax = localrender_gitbook(mathjax="local",template=template)#> <script>#> <p>this is mathjax variable: true</p>#> <br/>#> <p>this is mathjaxurl variable: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js</p>#> <br/>#> <p>this is mathjax-url variable: libs/mathjax-local/MathJax.js?config=TeX-AMS-MML_HTMLorMML</p>#> <br/>#> <p>this is math variable: <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script></p>
still does work with self_contained = TRUE
render_gitbook(mathjax=custom_mathjax,template=template,self_contained=TRUE)#> Warning: MathJax doesn't work with self_contained when not using the rmarkdown#> "default" template.#> <script>#> <p>this is mathjax variable: </p>#> <br/>#> <p>this is mathjaxurl variable: </p>#> <br/>#> <p>this is mathjax-url variable: </p>#> <br/>#> <p>this is math variable: </p>#> </script>
with default, the pandoc mathjax default and custom template one are used.
render_gitbook(template=template)#> <script>#> <p>this is mathjax variable: true</p>#> <br/>#> <p>this is mathjaxurl variable: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js</p>#> <br/>#> <p>this is mathjax-url variable: </p>#> <br/>#> <p>this is math variable: <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script></p>
we see that with a custom template it does not change a lot
About other format
It seemshtml_chapter
usesdefault.html
that does use themath
pandoc variable. I suppose becauseself_contained
is forced to FALSE.
This is one option for gitbook format too if we don't want to support self_contained gitbook with mathjax (as it is not working anyway for now it seems)
@yihui I am waiting on your thoughts on all that to see what we do
Uh oh!
There was an error while loading.Please reload this page.
This couldresolve#915
It is only a suggestion for modificaiton in
bookdown
only. There are other solution to deal with that more broadly for all the formats:pandoc_mathjax_args
in pre processorBut on this PR, here is what has changed based on the analysis in#915 (comment)
This PR changes nothing at the fact gitbook and mathjax does not work when
self_contained = TRUE
Custom url now works. See the value of
src
belowit works also with mathjax local now. See
src
belowwith no mathjax asked, it is still working - no mathjax is used.
with custom template, we can decide to pass the
mathjax-url
variable for use in custom templatecustom templates can use pandoc mathjax-url variable, that corresponds
but it is not used by pandoc - So I guess we should choice what should happen here.
still does work with self_contained = TRUE
with default, the pandoc mathjax default and custom template one are used.
we see that with a custom template it does not change a lot
About other format
It seems
html_chapter
usesdefault.html
that does use themath
pandoc variable. I suppose becauseself_contained
is forced to FALSE.This is one option for gitbook format too if we don't want to support self_contained gitbook with mathjax (as it is not working anyway for now it seems)
@yihui I am waiting on your thoughts on all that to see what we do