Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak handling of mathjax argument in gitbook template #937

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

cderv
Copy link
Collaborator

@cderv cderv commented Aug 26, 2020

This could resolve #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:

  • 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 PR
pkgload::load_all()
#> Loading bookdown

temp_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 now
  rmarkdown::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 when self_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 of src 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. See src 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 the mathjax-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.

  • choose what to do when custom mathjax AND template are used
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 = local
render_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 seems html_chapter uses default.html that does use the math pandoc variable. I suppose because self_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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next to consider for next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom MathJax URL not working
2 participants