使用Rmarkdown
和Rstudio
在pandoc
中使用knitr
,我使用MathJax
通过LaTeX和HTML输出来定位PDF输出。我想使用一些可用的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}{\W\per\square\m}$ and $\SI{0.8}{\A\per\W}$,
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
从MathJax
获得rawgit.org
,因为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{}
并删除动态加载MathJax
的输出HTML文件的部分(标记为<!-- 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
的here MathJax扩展的示例的标题的修改版本)
这表明我可以修改pandoc
的HTML模板来反映这些变化,事情基本上可行。
但是,仍存在以下问题:
cdn.mathjax.org
的网址,还是我应该使用的更好的网址?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}
---
header.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>
它有效,但速度很慢。
约翰