IPython(Jupyter)MathJaX序言 [英] IPython (Jupyter) MathJaX preamble

查看:429
本文介绍了IPython(Jupyter)MathJaX序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



如何在IPython(或Jupyter)笔记本中设置MathJax序言,以便于其他人阅读方式重复使用我的文件(在 http://nbviewer.org 上)并且适用于LaTeX / PDF生成?



背景



我想将IPython(现在的Jupyter)笔记本用于后来通过LaTeX转换为PDF的文档(使用 ipython nbconvert )。问题是如何在几乎每个文档中包含一堆宏定义。类似于:

  \ newcommand {\ vect} [1] {\vec {#1}} 
\ newcommand {\ abs} [1] {\lvert#1 \\\ version}
\DeclareMathOperator {\ erf} {erf}

等。就笔记本电脑而言,一个令人不满意的解决方案就是将它们简单地包含在笔记本顶部的降价单元中,嵌入两个美元符号之间 $$ 所以它是解释为数学。如果这是在一些介绍性文本之后完成的,那么它甚至不会影响输出。



问题在于,当转换为LaTeX(用于PDF导出)时,这些命令嵌入在LaTeX文件的数学环境中。这有几个问题:


  1. \DeclareMathOperator 这样的命令必须在LaTeX中出现文档序言。

  2. 命令定义是等式的本地,在文档的后面部分不可用。 (这可以通过使用 \gdef \ global \def 来克服,但是必须将MathJax引入用 \\\\gdef {\def} 之类的东西来识别这些命令,这些命令在某种程度上是从LaTeX中隐藏的。我发现这项工作的任何方式都是丑陋的hack。)

  3. 有时候命令已在LaTeX中定义,需要 \renewcommand (MathJax不支持,但也可以由 \ class \renewcommand \ newcommand 等提供,这对我来说似乎是合理的,因为MathJax无法知道最终LaTeX可能会使用哪个序言文件)。

可能解决办法是通过添加代码(不确定相当于 \DeclareMathOperator c $ c>这里......)

 < script type =text / x-mathjax-config> 
MathJax.Hub.Config({
TeX:{
宏:{
vect:[{\\vec#1},1],
abs:[{\\lvert#1 \\rvert},1]
}
}
});
< / script>

custom.js 文件然后在转换为PDF时提供LaTeX包。我对这种方法的问题是:如何分发 custom.js 文件和LaTeX样式文件供其他人(协作者和查看者)使用?


  1. 我希望协作者能够编辑和阅读我的文档,而无需在其全局配置中安装自定义扩展。具体来说,我可以要求他们在下载/签出 local 的代码后运行像 python setup.py configure 这样的命令修改项目,如在包含笔记本的所有目录中填充 ipython_notebook_config.py 文件,但我不乐意安装扩展,或修改他们的个人全局 custom.js file。



    我的绊脚石是我不知道如何从本地自定义添加贡献。 js 文件到笔记本链,并怀疑这可能违反了安全政策。



    最好的解决方案不需要我的协作者的任何操作部分。


  2. 我希望我的笔记本能够在 http:/上工作/nbviewer.org ,以便人们能够下载笔记本并制作PDF。 (我认为这排除了使用 custom.js hacks和分布式 * .sty 文件,但我不确定。)


  3. 我希望能够简单地启动一个新的笔记本,然后开始写作而无需插入每个笔记本开头的一堆样板代码,虽然可以通过一个简单的方法使用笔记本扩展或 python_notebook_config.py 中的一些挂钩自动执行此过程。 / p>




参考文献



以下帖子解决其中一些问题,但在大多数方面都做得不够:





讨论 pandoc <的(潜在)问题/ a>从IPython笔记本中生成LaTeX文件:





笔记本中数学的一般性讨论:




解决方案

我认为你可以解决一些问题,但不是全部。



首先,绊脚石。我相信(虽然我可能错了)nbviewer除了笔记本本身之外什么都不看。例如,我看不出它如何运行 ipython_notebook_config.py 存储和你的笔记本一起。因此,排除这种思路,这意味着我认为你必须咬紧牙关并为每个笔记本添加样板。但是你至少可以最小化样板。在那种情况下:



您可以在github上维护您的 custom.js (可能在更具描述性的名称下)或无论如何,然后在你的所有笔记本上添加一行样板来从URL加载该脚本。你仍然需要样板,但它会短得多。



一旦你执行了包含javascript的代码单元,它就会保存在笔记本中,这意味着它会在下次浏览器加载时自动发生,甚至在代码单元执行之前。因此,除非nbviewer阻止javascript的执行,否则它应该正常工作。这也可以让合作者很好地工作,因为他们不必下载额外的文件。



至于你自己的样式文件,我怀疑任何人都很精致到安装ipython和latex,下载你的笔记本,然后运行 nbconvert 就可以下载 .sty 文件。无论如何,我不认为有必要这样做......


Question

How can I setup a MathJax "preamble" for use in IPython (or Jupyter) notebooks for repeated use in a way that is convenient for others to read my documents (on http://nbviewer.org) and that works for LaTeX/PDF generation?

Background

I would like to use IPython (now Jupyter) notebooks for documents that I later convert to PDF via LaTeX (using ipython nbconvert). The problem is how to include a bunch of macro definitions that I use in almost every document. Something like:

\newcommand{\vect}[1]{\vec{#1}}
\newcommand{\abs}[1]{\lvert#1\rvert}
\DeclareMathOperator{\erf}{erf}

etc. As far as the notebooks is concerned, one unsatisfactory solution is to simply include these in a markdown cell at the top of the notebook, embeded between two dollar signs $$ so it is interpreted as math. If this is done after some introductory text, then it does not even affect the output.

The problem is that, when converting to LaTeX (for PDF export), these commands are embedded in a math environment in the LaTeX file. This has several problems:

  1. Commands like \DeclareMathOperator must come in the LaTeX document preamble.
  2. Command definitions are local to the equation and not available later in the document. (This can be overcome by using \gdef or \global\def but then one must trick MathJax into recognising these commands with something like \let\gdef{\def} which is somehow hidden from LaTeX. Any way I have found of making this work amounts to an ugly hack.)
  3. Sometimes commands are already defined in LaTeX and need to have \renewcommand (not supported by MathJax, but again can be provided by \let\renewcommand\newcommand etc. which seems reasonable to me since MathJax can't have some idea of what preamble might be used for the final LaTeX file).

Probably the solution is to provide a set of macros to MathJax by adding code like (not sure the equivalent of \DeclareMathOperator here...)

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
    Macros: {
      vect: ["{\\vec #1}",1],
      abs: ["{\\lvert #1 \\rvert}",1]
    }
  }
});
</script>

to a custom.js file and then providing a LaTeX package for inclusion when converting to PDF. The problem I have with this approach is: How to distribute the custom.js file and LaTeX style file for others (collaborators and viewers) to use?

  1. I want collaborators to be able to edit and read my documents without having to install custom extensions in their global configuration. To be specifiec, I am fine with requiring them to run a command like python setup.py configure once they download/checkout my code which does local modifications to the project like populating ipython_notebook_config.py files in all directories containing notebooks, but am not happy installing extensions, or modifying their personal global custom.js file.

    My stumbling block here is that I don't know how to add contributions from a local custom.js file to the notebook chain, and suspect that this might violate a security policy.

    The best solution would not require any action on my collaborator's part.

  2. I want my notebooks to work on http://nbviewer.org, and for people to be able to download the notebook and produce a PDF. (I think this rules out the possibility of using custom.js hacks and a distributed *.sty file, but am not certain.)

  3. I would prefer to be able to simply start a new notebook and then start writing without having to insert a bunch of boilerplate code at the start of each notebook, though would be amenable to having a simple way of automating this process using an notebook extension or some hooks in python_notebook_config.py.

References

The following posts address some of these issues, but fall short on most fronts:

Discussions about (potential) problems with the pandoc production of LaTeX files from IPython notebooks:

General discussion of math in notebooks:

解决方案

I think you can solve some of your problems, but not all.

First, the stumbling block. I believe (though I might be wrong) that nbviewer doesn't look at anything but the notebook itself. For example, I don't see how it could run an ipython_notebook_config.py stored alongside your notebook. So that rules out that line of thought, meaning that I think you'll have to bite the bullet and add boilerplate to every notebook. But you might at least be able to minimize the boilerplate. In that vein:

You could maintain your custom.js (probably under a more descriptive name) on github or whatever, and then add one line of boilerplate to all your notebooks to load that script from the URL. You would still need boilerplate, but it would be a lot shorter.

Once you have executed the code cell containing the javascript, it is saved in the notebook, which means that it will automatically happen the next time the browser loads it, even before the code cell is executed. So unless nbviewer prevents the javascript's execution, it should work just fine. This would also make things work nicely for collaborators, since they wouldn't have to download additional files.

As for your own style file, I suspect that anyone sophisticated enough to install ipython and latex, download your notebook, and run nbconvert on it would also be sophisticated enough to download the .sty file. Anyway, I don't see any way around the need to do that...

这篇关于IPython(Jupyter)MathJaX序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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