在Sphinx中创建LaTeX数学宏



我正在用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文档字符串?

如果您使用的是MathJax,这里有一个可能的解决方案。我仍在寻找一个更好的解决方案,但如果你需要快速破解,它可能会有所帮助。

  1. html_static_path配置选项(通常为_static)中指定的目录下创建一个文件,例如mathconf.js。这将包含MathJax的JS配置。例如(来自MathJax文档):

    MathJax.Hub.Config({
      TeX: {
        Macros: {
          RR: '{\bf R}',
          bold: ['{\bf #1}', 1]
        }
      }
    });
    

    您可以按照上面的语法添加更多命令。显示的内容定义了宏RRbold{#1},最后一个接受一个参数。

  2. _templates目录中添加一个layout.html文件。这个想法是扩展当前主题,因此它搜索以前的MathJax配置文件。因此,内容是:

    {% extends "!layout.html" %}
    {% set script_files = script_files + ["_static/mathconf.js"] %}
    

    请注意,在这种情况下,它_static目录,因为在这种情况中,它指的是在编译之后搜索的位置。Sphinx将文件从html_static_path移动到构建目录下的_static目录。

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

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

1) 制作一个文件,比如"latex_macros.sty",包含您的宏(每行一个),并将其放在与Sphinx conf.py文件相同的目录中;

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

# Additional stuff for the LaTeX preamble.
latex_elements['preamble'] = 'usepackage{amsmath}nusepackage{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'
#####################################################

如果您使用pngmath扩展,您可以通过将其插入conf.py脚本将其放在序言中:

pngmath_latex_preamble = r"newcommand{cG}{mathcal{G}}"

添加到@Keta自2018年8月以来的回答和此提交(https://github.com/sphinx-doc/sphinx/pull/5230/files)您可以根据文档在conf.py中使用mathjax_config(http://www.sphinx-doc.org/en/master/usage/extensions/math.html?#confval-mathjax_config)

例如,可以添加以下内容,

mathjax_config = {                  
    "TeX": {                        
        "Macros": {                 
            "RR": '{\bf R}',       
            "bold": ['{\bf #1}',1] 
            }                       
        }                           
    }                               

在斯芬克斯文件2.4.3(例如sphinx-quickstart --version)上测试的拟议溶液

Sphinx-doc允许通过MathJax_config对MathJax进行额外调整。最终目标是我们希望在conf.py中实现以下内容:

mathjax_config = {
    'TeX': {
        'Macros': {
            # Math notation
            "Z": "\mathbb{Z}",                                    # set of integers
            # MoA notations
            "minus": "{}^{\boldsymbol{\mbox{-}}\!}",            # scalar negation operator
        }
   }
}

我们可以像上面那样手动完成。但是,我们可以通过解析包含所有宏命令的单独.tex文件来自动填充mathjax_config,从而做得更好。

例如,我的mathsymbols.texconf.py位于同一级别,内容如下:

DeclareRobustCommand{ojoin}{rule[-0.12ex]{.3em}{.4pt}llap{rule[1.2ex]{.3em}{.4pt}}}
newcommand{leftouterjoin}{mathrel{ojoinmkern-6.5muJoin}}
newcommand{rightouterjoin}{mathrel{Joinmkern-6.5muojoin}}
newcommand{fullouterjoin}{mathrel{ojoinmkern-6.5muJoinmkern-6.5muojoin}}

然后,在conf.py中,我们可以写:

mathjax_config = { 'TeX': {'Macros': {}}}
with open('mathsymbols.tex', 'r') as f:
    for line in f:
        macros = re.findall(r'\(DeclareRobustCommand|newcommand){\(.*?)}([(d)])?{(.+)}', line)
        for macro in macros:
            if len(macro[2]) == 0:
                mathjax_config['TeX']['Macros'][macro[1]] = "{"+macro[4]+"}"
            else:
                mathjax_config['TeX']['Macros'][macro[1]] = ["{"+macro[4]+"}", int(macro[3])]

自动填充CCD_ 20,我们就完成了。

通过上面的例子,我们可以在sphinx-doc中使用leftouterjoinLaTeX宏。

最新更新