在 Sphinx 中创建 LaTeX 数学宏

Creating LaTeX math macros within Sphinx(在 Sphinx 中创建 LaTeX 数学宏)
本文介绍了在 Sphinx 中创建 LaTeX 数学宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在用 Python 编写一些数学代码并使用 Sphinx 来生成文档.我知道 Sphinx 可以处理 Python 文档字符串中的 LaTeX 代码;见 https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathbase.如何创建 LaTeX 宏,例如 ewcommand{cG}{mathcal{G}},以便在 Python 文档字符串中使用?

I'm writing some mathematical code in Python and using Sphinx to produce the documentation. I know that Sphinx can handle LaTeX code in Python docstrings; see https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathbase. How can I create LaTeX macros, such as ewcommand{cG}{mathcal{G}}, to use in the Python docstrings?

推荐答案

啊哈,我找到了一个适用于 Sphinx pngmath 扩展的解决方案.这是 Sage(开源数学软件)使用的技巧;灵感来自 http://www.sagemath.org/doc/reference/sage/misc/latex_macros.html.

Aha, i found a solution that works with the Sphinx pngmath extension. It's the trick that Sage (open source mathematics software) uses; inspiration from http://www.sagemath.org/doc/reference/sage/misc/latex_macros.html.

要将您自己的 Latex 宏添加到 Sphinx 文档:

To add your own Latex macros to a Sphinx document:

1) 创建一个文件,例如latex_macros.sty",其中包含您的宏(每行一个),然后将其放在与您的 Sphinx conf.py 文件相同的目录中;

1) Make a file, say 'latex_macros.sty', containing your macros (one per line), and put it in, say, the same directory as your Sphinx conf.py file;

2) 将以下代码添加到您的 Sphinx conf.py 文件中:

2) Add the following code to your Sphinx conf.py file:

# Additional stuff for the LaTeX preamble.
latex_elements['preamble'] = 'usepackage{amsmath}
usepackage{amssymb}
'

#####################################################
# add LaTeX macros 

f = file('latex_macros.sty')

try:
    pngmath_latex_preamble  # check whether this is already defined
except NameError:
    pngmath_latex_preamble = ""

for macro in f:
    # used when building latex and pdf versions
    latex_elements['preamble'] += macro + '
'
    # used when building html version
    pngmath_latex_preamble += macro + '
'

#####################################################

这篇关于在 Sphinx 中创建 LaTeX 数学宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Python GTK Drag and Drop - Get URL(Python GTK 拖放 - 获取 URL)
Drag n Drop inside QgraphicsView doesn#39;t work (PyQt)(在 QgraphicsView 内拖放不起作用(PyQt))
How to get dropped file names in PyQt/PySide(如何在 PyQt/PySide 中获取删除的文件名)
Drag and drop onto Python script in Windows Explorer(在 Windows 资源管理器中拖放到 Python 脚本)
In Python, how can I include (not import) one file within another file, macro style, without changing namespace?(在 Python 中,如何在不更改命名空间的情况下在另一个文件中包含(不导入)一个文件,宏样式?)
C Preprocessor Macro equivalent for Python(相当于 Python 的 C 预处理器宏)