简体   繁体   English

当 self_contained = TRUE 时,如何在 bookdown::gitbook 中正确呈现数学方程?

[英]How to have correctly rendered mathematical equations in bookdown::gitbook when self_contained = TRUE?

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).我正在编写一个由多个 Rmd 文件组成的 GitBook 样式的 bookdown 文档,其中我使用选项self_contained = TRUE来制作自包含的 HTML 页面(以便以后我可以将它们作为 HTML 文件分发,而不是将多个文件与 HTML 页面一起分发与显示在其上的图片分开)。 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.当我尝试使用 $ $ 标签放置数学方程时,我得到类似[WARNING] Could not convert TeX math '\\frac{1}{\\sum_{i=1}^{S} p_{i}^2}', rendering as TeX并且方程未正确渲染。

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.我从这里看到当self_contained = TRUE时 MathJax 可能不起作用,从这里需要 MathJax 来呈现 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 .事实上, MathJax doesn't work with self_contained when not using the rmarkdown "default" template ,我总是收到类似MathJax doesn't work with self_contained when not using the rmarkdown "default" template的警告,并且如果我使用self_contained = FALSE则方程式会正确呈现。

So, I wonder if it is possible to render math correctly in GitBook style bookdown document while self_contained = TRUE .所以,我想知道是否有可能在self_contained = TRUE时在 GitBook 样式的 bookdown 文档中正确呈现数学。

I had the same issue and found a partial solution.我遇到了同样的问题并找到了部分解决方案。 Right in the index.Rmd after the yaml header, I included this chunk:在 yaml 标头之后的 index.Rmd 中,我包含了这个块:

<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:现在,html 输出呈现方程,但有两个警告:

  1. 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.显示方程(带有 $$ . $$)确实可以正常工作,但一些内联方程($ . $)表现出不同的风格:在我的例子中,一些方程出现在警告中(“Could not convert TeX math”)和看起来很正常。 The remaining inline equations have a slightly different look.其余的内联方程的外观略有不同。

  2. On Firefox and Safari the equations render correctly, on Chrome unfortunately not (I did not try other browsers).在 Firefox 和 Safari 上,方程式可以正确呈现,不幸的是,在 Chrome 上不能(我没有尝试其他浏览器)。

Hope that helps a bit!希望那有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM