在Sphinx中创建LaTeX数学宏 [英] Creating LaTeX math macros within Sphinx
问题描述
我正在用Python编写一些数学代码,并使用Sphinx生成文档.我知道Sphinx可以处理Python文档字符串中的LaTeX代码.参见 https://www .sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathbase .如何创建LaTeX宏(例如\newcommand{\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 \newcommand{\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}\n\usepackage{amssymb}\n'
#####################################################
# 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 + '\n'
# used when building html version
pngmath_latex_preamble += macro + '\n'
#####################################################
这篇关于在Sphinx中创建LaTeX数学宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!