在 sphinx-doc 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.tex 与 conf.py 位于同一级别,内容如下所示:
\DeclareRobustCommand{\ojoin}{\rule[-0.12ex]{.3em}{.4pt}\llap{\rule[1.2ex]{.3em}{.4pt}}}
\newcommand{\leftouterjoin}{\mathrel{\ojoin\mkern-6.5mu\Join}}
\newcommand{\rightouterjoin}{\mathrel{\Join\mkern-6.5mu\ojoin}}
\newcommand{\fullouterjoin}{\mathrel{\ojoin\mkern-6.5mu\Join\mkern-6.5mu\ojoin}}
那么,在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])]
自动填充mathjax_config,我们就完成了。
通过上面的例子,我们可以在 sphinx-doc 中使用\leftouterjoin LaTeX 宏。