Creating LaTeX math macros within Sphinx(在 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 数学宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Sphinx 中创建 LaTeX 数学宏
基础教程推荐
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
