【问题标题】:How to access MathJax extensions (like siunitx) from Rmarkdown via Pandoc & Knitr?如何通过 Pandoc & Knitr 从 Rmarkdown 访问 MathJax 扩展(如 siunitx)?
【发布时间】:2017-11-15 05:24:11
【问题描述】:

Rstudio 中使用Rmarkdown,使用pandocknitr,我的目标是通过LaTeX 输出PDF,并使用MathJax 输出HTML。我想使用一些可用的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 中。我有pandocrawgit.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 扩展示例的标题的修改版本,来自 @ 987654321@)

这表明我可以修改 pandoc 的 HTML 模板以反映这些更改,并且基本上可以工作。

但是,仍然存在以下问题:

  • 以这种方式更改 HTML 模板是否是修复 HTML 输出的正确方法?这些是现在要使用的 URL,cdn.mathjax.org 正在下降,还是我应该使用更好的 URL?
  • 为什么我仍然收到关于 siunitx.js 的警告?
  • 需要做什么才能让Rstudio 了解其预览中的siunitx 内容?是否已经有办法启用此功能(例如,说服它使用 siunitx 扩展,假设它是基于 MathJax 构建的),或者这是一个功能请求..?

确实,如果有一种简单的方法可以开箱即用地访问MathJax 扩展,而不必麻烦编辑模板等,并且在@ 中进行适当处理,那就太好了987654361@图形用户界面。我可以想象可能会有Rstudio 用户从额外的功能中受益,但不想/不能跳过这些障碍来访问它。

更新由于 MathJax CDN 的更改,我在加载有关 siunitx.js 的“工作”HTML 时看到的警告消息似乎是当前版本 siunitx.js 的一个普遍问题,请参阅此处提出的问题:https://github.com/burnpanck/MathJax-third-party-extensions/issues/5

【问题讨论】:

  • 您可能想要添加一个显示您描述的“警告”的实时示例,以便人们可以看到您所看到的。
  • 由于 MathJax CDN 问题,“警告”似乎是当前版本的 siunitx 软件包的普遍问题;在此处查看问题:github.com/burnpanck/MathJax-third-party-extensions/issues/5
  • 感谢您链接到错误报告。我在那里评论过。我不能对 rstudio 发表评论,但(从 MathJax 结束)这很难的主要原因是 siunitx 是第三方扩展而不是核心的一部分。

标签: rstudio knitr r-markdown pandoc mathjax


【解决方案1】:

我正在使用包含 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>

它可以工作,但速度很慢。

约翰

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2017-10-22
    • 2013-10-03
    • 2015-10-04
    • 2016-07-06
    • 2014-02-14
    • 1970-01-01
    • 2018-11-05
    • 2013-06-05
    相关资源
    最近更新 更多