更改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输出处理器.我该怎么办?
I am creating R notebooks that contain equations. I am using RStudio 1.2.5033 on Windows 10, R 3.5.1, and rmarkdown 2.1. When my R notebooks are rendered as HTML, MathJax (v2.7.2) uses the "HTML-CSS" output processor to render the equations. But I think that the output from the "CommonHTML" output processor looks better. So I want to include a directive, in my R notebooks, that forces MathJax to use the CommonHTML output processor. How may I do this?
如果我要渲染输出格式为html_document
的普通R Markdown文档,则可以通过YAML标头中的mathjax
选项解决该问题.例如,当以下文件呈现为HTML时,MathJax将使用CommonHTML输出处理器:
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. For example, when the following file is rendered to HTML, MathJax will use the CommonHTML output processor:
---
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
时,此解决方案不能很好地工作.在这种情况下,我得到的输出看起来像这样:
But this solution doesn't work well when I change the output
format from html_document
to html_notebook
. In that case, I get output that looks like this:
方程式是用CommonHTML呈现的,但是页面顶部有很多问题(请注意四个要点),并且默认的R Notebook CSS似乎没有实现.
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.
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. But I can't see a good workaround for that problem.
MathJax文档似乎表明我可以通过在对MathJax.Hub.Config()
的调用中添加jax
数组来指定输出处理器.但是,当我完成此操作后,仍然可以通过HTML-CSS输出处理器显示我的方程式.这是展示问题的R Markdown文档的最小示例:
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()
. But when I've done that, my equations are still displayed via the HTML-CSS output processor. Here is a minimal example of an R Markdown document that exhibits the problem:
---
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呈现的.如何将呈现方式更改为通用HTML"?
The call to MathJax.Hub.Config()
seems to do nothing here. In both Chrome and Edge, the equation is rendered via HTML-CSS, not CommonHTML. How can I change the rendering to Common HTML?
- 一篇历史悠久的帖子 Markdown中有没有办法覆盖默认的mathjax渲染器?是关于Jupyter笔记本的,但这很重要.还没有收到答案.
- 在这篇来自Google MathJax小组的帖子中修改脚本 a>-主要是将"HTML-CSS"更改为"CommonHTML"-似乎没有任何效果.
- One year-old post, Is there a way in markdown to override default mathjax renderer?, is about Jupyter notebooks, but it's relevant. It hasn't received an answer.
- Adapting the script in this post from the MathJax Google Group –– mainly by changing "HTML-CSS" to "CommonHTML" –– doesn't seem to have any effect.
推荐答案
解决方案是简单地省略YAML标头中的self_contained
行,或者等效地将self_contained
设置为true
.这是用户选择了mathjax渲染器的R笔记本的最小示例:
The solution is simply to omit the self_contained
line in the YAML header or, equivalently, to set self_contained
to true
. Here is a minimal example of an R notebook for which the user has chosen the mathjax renderer:
---
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"文件中.
When the file is rendered to HTML, the equation is displayed with CommonHTML, not with HTML-CSS. And the Mathjax script is contained within the "nb.html" file that is produced.
我对此感到惊讶,因为rmarkdown::html_document()
的文档说即使对于自包含文档,MathJax仍在外部加载(由于其大小,这是必需的").但是 R Markdown书籍的第3.1.8节表明,该限制仅适用从本地文件加载Mathjax时.因此,也许这并不奇怪.
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)." But Section 3.1.8 of the R Markdown book indicates that the restriction applies only when Mathjax is loaded from a local file. So perhaps it shouldn't be a surprise.
侧面说明:rmarkdown软件包使用的默认Mathjax配置由rmarkdown:::mathjax_config()
给出.从rmarkdown v2.1开始,该函数返回"MathJax.js?config = TeX-AMS-MML_HTMLorMML".
Side note: the default Mathjax configuration used by the rmarkdown package is given by rmarkdown:::mathjax_config()
. As of rmarkdown v2.1, the function returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".
这篇关于更改R笔记本中的mathjax渲染器("self_contained:false")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!