更改 R 笔记本中的 mathjax 渲染器("self_contained: false")
change mathjax renderer in R notebooks (with "self_contained: false")
我正在创建包含方程式的 R 笔记本。我在 Windows 10、R 3.5.1 和 rmarkdown 2.1 上使用 RStudio 1.2.5033。当我的 R 笔记本呈现为 HTML 时,MathJax (v2.7.2) 使用 "HTML-CSS" 输出处理器来呈现方程。但我认为 "CommonHTML" 输出处理器的输出看起来更好。所以我想在我的 R 笔记本中包含一个指令,强制 MathJax 使用 CommonHTML 输出处理器。我该怎么做?
如果我正在渲染输出格式为 html_document
的普通 R Markdown 文档,我可以通过 YAML header 中的 mathjax
选项解决问题。例如,当以下文件呈现为 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}
但是当我将 output
格式从 html_document
更改为 html_notebook
时,此解决方案效果不佳。在那种情况下,我得到如下所示的输出:
公式是用 CommonHTML 呈现的,但页面顶部有很多废话(注意四个要点),默认的 R Notebook CSS 没有好像没有实现。
这个问题似乎是使用 self_contained: FALSE
渲染 R 笔记本的普遍问题,如 R notebooks don't render properly when "self_contained" is FALSE because the "files" directory is deleted after rendering 中所建议的那样。但是我找不到解决该问题的好方法。
死胡同
MathJax documentation 似乎表明我可以通过在对 MathJax.Hub.Config()
的调用中添加 jax
数组来指定输出处理器。但是当我这样做时,我的方程式仍然通过 HTML-CSS 输出处理器显示。以下是展示问题的 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}
对MathJax.Hub.Config()
的调用似乎在这里什么都不做。在 Chrome 和 Edge 中,方程都是通过 HTML-CSS 呈现的,而不是 CommonHTML。如何将渲染更改为 Common HTML?
相关帖子
- 一个 year-old post、Is there a way in markdown to override default mathjax renderer? 是关于 Jupyter notebooks 的,但它是相关的。尚未收到答复。
- 适配this post from the MathJax Google Group中的脚本——主要是把"HTML-CSS"改成"CommonHTML"——好像没什么效果
解决方案是简单地省略 YAML header 中的 self_contained
行,或者等效地,将 self_contained
设置为 true
。这是用户选择了 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$.
当文件呈现为 HTML 时,方程式显示为 CommonHTML,而不是 HTML-CSS。 Mathjax 脚本包含在生成的 "nb.html" 文件中。
令我感到惊讶的是,这会起作用,因为 rmarkdown::html_document()
的文档说 "even for self contained documents MathJax is still loaded externally (this is necessary because of its size)." 但是 Section 3.1.8 of the R Markdown book 表明该限制仅在从本地文件加载 Mathjax 时适用。所以也许这不应该是一个惊喜。
旁注:rmarkdown 包使用的默认 Mathjax 配置由 rmarkdown:::mathjax_config()
给出。从 rmarkdown v2.1 开始,函数 returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".
我正在创建包含方程式的 R 笔记本。我在 Windows 10、R 3.5.1 和 rmarkdown 2.1 上使用 RStudio 1.2.5033。当我的 R 笔记本呈现为 HTML 时,MathJax (v2.7.2) 使用 "HTML-CSS" 输出处理器来呈现方程。但我认为 "CommonHTML" 输出处理器的输出看起来更好。所以我想在我的 R 笔记本中包含一个指令,强制 MathJax 使用 CommonHTML 输出处理器。我该怎么做?
如果我正在渲染输出格式为 html_document
的普通 R Markdown 文档,我可以通过 YAML header 中的 mathjax
选项解决问题。例如,当以下文件呈现为 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}
但是当我将 output
格式从 html_document
更改为 html_notebook
时,此解决方案效果不佳。在那种情况下,我得到如下所示的输出:
公式是用 CommonHTML 呈现的,但页面顶部有很多废话(注意四个要点),默认的 R Notebook CSS 没有好像没有实现。
这个问题似乎是使用 self_contained: FALSE
渲染 R 笔记本的普遍问题,如 R notebooks don't render properly when "self_contained" is FALSE because the "files" directory is deleted after rendering 中所建议的那样。但是我找不到解决该问题的好方法。
死胡同
MathJax documentation 似乎表明我可以通过在对 MathJax.Hub.Config()
的调用中添加 jax
数组来指定输出处理器。但是当我这样做时,我的方程式仍然通过 HTML-CSS 输出处理器显示。以下是展示问题的 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}
对MathJax.Hub.Config()
的调用似乎在这里什么都不做。在 Chrome 和 Edge 中,方程都是通过 HTML-CSS 呈现的,而不是 CommonHTML。如何将渲染更改为 Common HTML?
相关帖子
- 一个 year-old post、Is there a way in markdown to override default mathjax renderer? 是关于 Jupyter notebooks 的,但它是相关的。尚未收到答复。
- 适配this post from the MathJax Google Group中的脚本——主要是把"HTML-CSS"改成"CommonHTML"——好像没什么效果
解决方案是简单地省略 YAML header 中的 self_contained
行,或者等效地,将 self_contained
设置为 true
。这是用户选择了 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$.
当文件呈现为 HTML 时,方程式显示为 CommonHTML,而不是 HTML-CSS。 Mathjax 脚本包含在生成的 "nb.html" 文件中。
令我感到惊讶的是,这会起作用,因为 rmarkdown::html_document()
的文档说 "even for self contained documents MathJax is still loaded externally (this is necessary because of its size)." 但是 Section 3.1.8 of the R Markdown book 表明该限制仅在从本地文件加载 Mathjax 时适用。所以也许这不应该是一个惊喜。
旁注:rmarkdown 包使用的默认 Mathjax 配置由 rmarkdown:::mathjax_config()
给出。从 rmarkdown v2.1 开始,函数 returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".