I'm writing a GitBook style bookdown document consisting of several Rmd files, in which I use the option self_contained = TRUE
to make self-contained HTML pages (so that later I could distribute them as HTML files instead of multiple files with the HTML pages separate from the pictures displayed on them). When I tried to put mathematical equations using the $ $ tags, I got something like [WARNING] Could not convert TeX math '\\frac{1}{\\sum_{i=1}^{S} p_{i}^2}', rendering as TeX
and the equations were not rendered correctly.
I saw from here that MathJax may not work when self_contained = TRUE
, and from here that MathJax is needed to render math in HTML. Indeed, I always get a warning like MathJax doesn't work with self_contained when not using the rmarkdown "default" template
, and the equations were rendered correctly if I use self_contained = FALSE
.
So, I wonder if it is possible to render math correctly in GitBook style bookdown document while self_contained = TRUE
.
I had the same issue and found a partial solution. Right in the index.Rmd after the yaml header, I included this chunk:
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
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>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$', '$']],
displayMath: [['$$', '$$']],
}
});
</script>
Now, the html-output renders the equations but there are two caveats:
Display equations (with $$ . $$) do work fine, but some of the inline equations ($ . $) exhibit differing styles: In my case, some equations show up in the warnings ("Could not convert TeX math") and look normal. The remaining inline equations have a slightly different look.
On Firefox and Safari the equations render correctly, on Chrome unfortunately not (I did not try other browsers).
Hope that helps a bit!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.