繁体   English   中英

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

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

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

我从这里看到当self_contained = TRUE时 MathJax 可能不起作用,从这里需要 MathJax 来呈现 HTML 中的数学。 事实上, 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则方程式会正确呈现。

所以,我想知道是否有可能在self_contained = TRUE时在 GitBook 样式的 bookdown 文档中正确呈现数学。

我遇到了同样的问题并找到了部分解决方案。 在 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>

现在,html 输出呈现方程,但有两个警告:

  1. 显示方程(带有 $$ . $$)确实可以正常工作,但一些内联方程($ . $)表现出不同的风格:在我的例子中,一些方程出现在警告中(“Could not convert TeX math”)和看起来很正常。 其余的内联方程的外观略有不同。

  2. 在 Firefox 和 Safari 上,方程式可以正确呈现,不幸的是,在 Chrome 上不能(我没有尝试其他浏览器)。

希望那有所帮助!

暂无
暂无

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

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