Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tweak handling of mathjax argument in gitbook template#937

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
cderv wants to merge4 commits intorstudio:main
base:main
Choose a base branch
Loading
fromcderv:custom-mathjax

Conversation

cderv
Copy link
Collaborator

@cdervcderv commentedAug 26, 2020
edited
Loading

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 rmarkdownpandoc_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.

  • 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 = 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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
nextto consider for next release
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Custom MathJax URL not working
2 participants
@cderv@yihui

[8]ページ先頭

©2009-2025 Movatter.jp