在Rstudio
中使用Rmarkdown
,使用pandoc
和knitr
,我的目标是通过LaTeX输出PDF和HTML输出与MathJax
。我想使用一些可用的MathJax
扩展名,以便为PDF目标提供更丰富的LaTeX。具体来说,我现在正在尝试使用siunitx
扩展,尽管我也对其他人感兴趣(例如physics
)。
使用siunitx
可以很好地与 LaTeX 一起进行 PDF 输出,但我很难让它与 HTML 输出一起使用。
下面是一个示例 Rmarkdown 文件:
---
title: "siunitx test"
author: "chriss"
date: "June 13, 2017"
output:
html_document:
mathjax: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML
number_sections: yes
pdf_document:
keep_tex: yes
latex_engine: xelatex
number_sections: yes
header-includes: usepackage{siunitx}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# The Problem
I would like to be able to use `siunitx` latex macros from `Rmarkdown`,
targetting PDF output via latex and html with MathJax. It should get me proper
formatting of things like $SI{120}{Wpersquarem}$ and $SI{0.8}{AperW}$,
as long as I put them in a latex math environment, so that MathJax picks them
up.
The PDF output is OK when I add the `header-includes: usepackage{siunitx}` to
the `YAML` header, but how can I access the MathJax `siunitx` extension via the
knitr -> pandoc -> mathjax/html route?
Check: is MathJax working in general: $frac{1}{r^2}$
这很好地编织到 PDF,但$SI{}{}$
是逐字输出的,并在 HTML 输出和RStudio
中突出显示红色。我pandoc
从rawgit.org
获得MathJax
,因为cdn.mathjax.org
的默认值即将失效,并且似乎不再具有扩展的Contrib
路径。
我尝试添加MathJax
的$require{siunitx}$
,并在siunitx
扩展的路径上发生变化,但无济于事。这会导致 HTML 查找siunitx
扩展名,但显然在错误的位置:https://cdn.rawgit.com/mathjax/MathJax/2.7.1/extensions/TeX/siunitx.js?V=2.7.1
,这是一个404
。
如果我删除require{}
并删除输出HTML文件中动态加载MathJax
的部分(标记为<!-- dynamically load mathjax for compatibility with self-contained -->
),并手动添加:
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [["$","$"],["\(","\)"]]},
errorSettings: {message: undefined},
TeX: { extensions: ["[burnpanck]/siunitx/unpacked/siunitx.js"] }
};
MathJax.Ajax.config.path['burnpanck'] =
'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
</script>
<script type="text/javascript"
src="https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-
MML_HTMLorMML"></script>
对于 HTML 文件的标头,然后它会短暂地弹出有关siunitx.js
某些问题的投诉,但会产生正确的输出(这是siunitx
MathJax 扩展示例中标头的修改版本,从这里开始)
这表明我可以修改 HTML 模板以反映这些更改pandoc
,并且基本上可以正常工作。
但是,仍然存在以下问题:
- 以这种方式更改 HTML 模板是修复 HTML 输出的正确方法吗?这些是打算在
cdn.mathjax.org
下降时使用的 URL,还是我应该使用更好的 URL? - 为什么我仍然收到有关
siunitx.js
的警告? - 需要做些什么才能
Rstudio
了解预览中的siunitx
内容?是否已经有一种方法可以启用此功能(例如,说服它使用siunitx
扩展,假设它是建立在MathJax
上),或者这是一个功能请求..?
事实上,如果有一种简单的方法可以开箱即用地访问MathJax
扩展,而不必在Rstudio
GUI中进行适当的处理来编辑模板等麻烦,那就太好了。我可以想象可能有Rstudio
用户会从额外的功能中受益,但不想/无法跳过这些箍来访问它。
更新我在加载有关siunitx.js
的"工作"HTML时看到的警告消息似乎是当前版本的siunitx.js
的普遍问题,由于MathJax CDN的更改,请参阅此处提出的问题:https://github.com/burnpanck/MathJax-third-party-extensions/issues/5
我正在使用包含in_header来解决问题。
---
title: "doku1"
output:
html_document:
includes:
in_header: header.html
pdf_document:
keep_tex: yes
latex_engine: pdflatex
number_sections: no
header-includes: usepackage{mhchem, siunitx}
---
标题.html看起来像这样
<script type="text/x-mathjax-config">
MathJax.Ajax.config.path["mhchem"] = "https://cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.3.2";
MathJax.Ajax.config.path['myExt'] = 'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
MathJax.Hub.Config({
TeX: { extensions: ["AMSmath.js","AMSsymbols.js","[myExt]/siunitx/unpacked/siunitx.js","[mhchem]/mhchem.js", "color.js"] }
});
</script>
它有效,但相当慢。
John