簡體   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