如何通过Pandoc&从Rmarkdown访问MathJax扩展(例如siunitx).针织衫? [英] How to access MathJax extensions (like siunitx) from Rmarkdown via Pandoc & Knitr?

查看:100
本文介绍了如何通过Pandoc&从Rmarkdown访问MathJax扩展(例如siunitx).针织衫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pandocknitrRstudio中的Rmarkdown,我将通过LaTeX的PDF输出和通过MathJax的HTML输出作为目标.我想使用一些可用的MathJax扩展名,以便为PDF目标提供更丰富的LaTeX.具体来说,尽管我也对其他人(例如physics)感兴趣,但我现在正尝试使用siunitx扩展名.

Working with Rmarkdown in Rstudio, using pandoc and knitr, I am targetting PDF output via LaTeX and HTML output with MathJax. I would like to use some of the MathJax extensions that are available, to allow richer LaTeX for the PDF target. Specifically, I am trying to use the siunitx extension right now, although I am also interested in others (e.g. physics).

使用siunitx可以与LaTeX一起用于PDF输出,但是我很难将其与HTML输出一起使用.

Using siunitx works fine with LaTeX for PDF output, but I've had a hard time getting it working with HTML output.

这里有一个示例Rmarkdown文件:

Here an example Rmarkdown file:

---
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中以红色突出显示.我正在从rawgit.org中获取pandoc,因为cdn.mathjax.org的默认设置即将失效,而且看来,该扩展名不再具有Contrib路径.

This knits fine to PDF, but the $\SI{}{}$ are output verbatim and hilighted red in the HTML output, and in RStudio. I'm having pandoc get MathJax from rawgit.org, since the default of cdn.mathjax.org is soon-to-be defunct, and it seems, no longer has a Contrib path with the extensions.

我尝试添加MathJax$\require{siunitx}$,但在siunitx扩展名路径上的变化却无济于事.这会导致HTML查找siunitx扩展名,但显然在错误的位置:https://cdn.rawgit.com/mathjax/MathJax/2.7.1/extensions/TeX/siunitx.js?V=2.7.1,它是404.

I have tried adding MathJax's $\require{siunitx}$ with variations on the path to the siunitx extension, to no avail. This causes the HTML to look for the siunitx extension, but apparently in the wrong place: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/extensions/TeX/siunitx.js?V=2.7.1, which is a 404.

如果我删除\require{}并删除输出HTML文件中动态加载MathJax的部分(标记为<!-- dynamically load mathjax for compatibility with self-contained -->),然后手动添加:

If I remove the \require{} and remove the part of the output HTML file that loads MathJax dynamically (labelled <!-- dynamically load mathjax for compatibility with self-contained -->), and manually add:

<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扩展名示例的标头的修改版本,从此处)

To header of the HTML file, then it briefly pops up a complaint about some issue with siunitx.js but produces correct output (this is a modified version of the header from the example for the siunitx MathJax extension, from here )

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

This suggests that I could modify the HTML template for pandoc to reflect those changes, and things would basically work.

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

However, the following questions remain:

  • 以这种方式更改HTML模板是否是修复HTML输出的正确方法?在cdn.mathjax.org下降之后,现在打算使用这些URL,还是应该使用更好的URL?
  • 为什么我仍然收到有关siunitx.js的警告?
  • 要使Rstudio理解其预览中的siunitx内容,需要做些什么?
  • 是否已经有一种方法可以启用此功能(例如,说服它使用siunitx扩展名(假设它是基于MathJax构建的))?
  • Is changing the HTML template in this way the proper way to fix the HTML output? Are these the URLs that are intended to be used now that cdn.mathjax.org is going down, or are there better ones that I should use instead?
  • Why do I still get the warning about siunitx.js?
  • What would need to be done to have Rstudio understand the siunitx content in its preview? Is there already a way to enable this (e.g. convince it to use siunitx extension, assuming it's built on MathJax), or would this be a feature request..?

的确,如果有一种简便的方法可以直接使用MathJax扩展名,而不必麻烦编辑模板等,并且在Rstudio GUI.我可以想象可能会有Rstudio个用户将从这些附加功能中受益,但又不想/无法跳过这些障碍来访问它.

Indeed, it would be nice if there was an easy way to access the MathJax extensions out-of-the-box, without having to go to the trouble of editing templates and the like, with proper handling in the Rstudio GUI. I can imagine there may be Rstudio users who would benefit from the extra functionality but don't want to / aren't able to jump through these kind of hoops to access it.

更新,由于对MathJax CDN的更改,我在加载有关siunitx.js的有效" HTML时看到的警告消息似乎是当前版本的siunitx.js的普遍问题,看到这里提出的问题: https://github.com/burnpanck/MathJax -third-party-extensions/问题/5

UPDATE The warning message I see when loading the 'working' HTML about siunitx.js seems to be a general issue with the current version of siunitx.js, due to changes to the MathJax CDN, see issue raised here: https://github.com/burnpanck/MathJax-third-party-extensions/issues/5

推荐答案

我正在使用include in_header来解决问题.

I'm using includes in_header to solve the problem.

---
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看起来像这样

header.html looks like this

<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>

它可以工作,但是速度很慢.

It works, but is rather slow.

约翰

这篇关于如何通过Pandoc&amp;从Rmarkdown访问MathJax扩展(例如siunitx).针织衫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆