简体   繁体   English

更改 R 笔记本中的 mathjax 渲染器(使用“self_contained: false”)

[英]change mathjax renderer in R notebooks (with "self_contained: false")

I am creating R notebooks that contain equations.我正在创建包含方程的 R 笔记本。 I am using RStudio 1.2.5033 on Windows 10, R 3.5.1, and rmarkdown 2.1.我在 Windows 10、R 3.5.1 和 rmarkdown 2.1 上使用 RStudio 1.2.5033。 When my R notebooks are rendered as HTML, MathJax (v2.7.2) uses the "HTML-CSS" output processor to render the equations.当我的 R 笔记本呈现为 HTML 时,MathJax (v2.7.2) 使用“HTML-CSS”输出处理器来呈现方程。 But I think that the output from the "CommonHTML" output processor looks better.但我认为“CommonHTML”输出处理器的输出看起来更好。 So I want to include a directive, in my R notebooks, that forces MathJax to use the CommonHTML output processor.所以我想在我的 R 笔记本中包含一个指令,强制 MathJax 使用 CommonHTML 输出处理器。 How may I do this?我该怎么做?

If I were rendering an ordinary R Markdown document with output format html_document , I could solve the problem via the mathjax option in my YAML header.如果我要渲染输出格式为html_document的普通 R Markdown 文档,我可以通过 YAML 标头中的mathjax选项解决问题。 For example, when the following file is rendered to HTML, MathJax will use the CommonHTML output processor:例如,当以下文件呈现为 HTML 时,MathJax 将使用 CommonHTML 输出处理器:

---
title: "Trouble with MathJax"
output: 
  html_document:
    mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
    self_contained: false    
---

\begin{equation}
  R_3 = \alpha
\end{equation}

But this solution doesn't work well when I change the output format from html_document to html_notebook .但是当我将output格式从html_documenthtml_notebook时,此解决方案html_document html_notebook In that case, I get output that looks like this:在这种情况下,我得到的输出如下所示:

显示 html_notebook 呈现问题的屏幕截图

The equation is rendered with CommonHTML, but there is a lot of cruft at the top of the page (note the four bullet points), and the default R Notebook CSS doesn't seem to be implemented.等式是用 CommonHTML 呈现的,但页面顶部有很多杂物(注意四个要点),而且默认的 R Notebook CSS 似乎没有实现。

The problem seems to be general to rendering R notebooks with self_contained: FALSE , as suggested in R notebooks don't render properly when "self_contained" is FALSE because the "files" directory is deleted after rendering .这个问题似乎对使用self_contained: FALSE渲染 R 笔记本来说很普遍,正如在“self_contained”为 FALSE 时 R 笔记本中所建议的那样, 因为“文件”目录在渲染后被删除 But I can't see a good workaround for that problem.但我看不到解决这个问题的好方法。


Dead Ends死胡同

The MathJax documentation seems to indicate that I can specify the output processor by adding the jax array in a call to MathJax.Hub.Config() . MathJax 文档似乎表明我可以通过在对MathJax.Hub.Config()的调用中添加jax数组来指定输出处理器。 But when I've done that, my equations are still displayed via the HTML-CSS output processor.但是当我这样做时,我的方程仍然通过 HTML-CSS 输出处理器显示。 Here is a minimal example of an R Markdown document that exhibits the problem:这是一个显示问题的 R Markdown 文档的最小示例:

---
title: 'Trouble with MathJax'
output: html_notebook
---

<script type="text/x-mathjax-config"> 
  MathJax.Hub.Config({ 
    jax: ["input/TeX", "output/CommonHTML"],
  });
</script>

\begin{equation}
  R_3 = \alpha
\end{equation}

The call to MathJax.Hub.Config() seems to do nothing here.MathJax.Hub.Config()的调用在这里似乎没有任何作用。 In both Chrome and Edge, the equation is rendered via HTML-CSS, not CommonHTML.在 Chrome 和 Edge 中,等式是通过 HTML-CSS 呈现的,而不是 CommonHTML。 How can I change the rendering to Common HTML?如何将渲染更改为通用 HTML?


Related Posts相关文章

The solution is simply to omit the self_contained line in the YAML header or, equivalently, to set self_contained to true .解决方案是简单地省略 YAML 标头中的self_contained行,或者等效地将self_contained设置为true Here is a minimal example of an R notebook for which the user has chosen the mathjax renderer:这是用户选择了 mathjax 渲染器的 R 笔记本的最小示例:

---
title: "Self-contained notebook with non-default Mathjax config"
output:
  html_notebook:
    mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
---

$R_3 = 2$.

When the file is rendered to HTML, the equation is displayed with CommonHTML, not with HTML-CSS.当文件被渲染为 HTML 时,方程显示为 CommonHTML,而不是 HTML-CSS。 And the Mathjax script is contained within the "nb.html" file that is produced. Mathjax 脚本包含在生成的“nb.html”文件中。

I was surprised that this works, because the documentation for rmarkdown::html_document() says that "even for self contained documents MathJax is still loaded externally (this is necessary because of its size)."我很惊讶这有效,因为rmarkdown::html_document()的文档说“即使对于自包含的文档,MathJax 仍然是从外部加载的(这是必要的,因为它的大小)。” But Section 3.1.8 of the R Markdown book indicates that the restriction applies only when Mathjax is loaded from a local file.但是R Markdown 书的第 3.1.8 节指出该限制仅适用于从本地文件加载 Mathjax 时。 So perhaps it shouldn't be a surprise.所以也许这不应该是一个惊喜。

Side note: the default Mathjax configuration used by the rmarkdown package is given by rmarkdown:::mathjax_config() .旁注:rmarkdown 包使用的默认 Mathjax 配置由rmarkdown:::mathjax_config() As of rmarkdown v2.1, the function returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".从 rmarkdown v2.1 开始,该函数返回“MathJax.js?config=TeX-AMS-MML_HTMLorMML”。

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

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