在 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 + '
'
#####################################################
相关文章