在 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 数学宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)