gpt4 book ai didi

latex - Sphinx对包含节号和节标题的其他节的引用

转载 作者:行者123 更新时间:2023-12-04 04:35:26 24 4
gpt4 key购买 nike

我正在使用Sphinx编写包含大量引用的文档:

.. _human-factor:

The Human Factor
================

...

(see :ref:`human-factor` for details)

编译后的文档包含以下内容:

(有关详细信息,请参见人为因素)

相反,我希望将其格式化为:

(有关详细信息,请参见5.1人为因素)

我试图用谷歌的解决方案,我发现, latex hyperref包可以做到这一点,但我不知道如何将其添加到Sphinx构建。

最佳答案

我基本上是从这里使用numsec.py来解决它的:https://github.com/jterrace/sphinxtr

我必须用此函数替换doctree_resolved函数,以获取节号和标题(例如“5.1 The Human Factor”)。

def doctree_resolved(app, doctree, docname):
secnums = app.builder.env.toc_secnumbers
for node in doctree.traverse(nodes.reference):
if 'refdocname' in node:
refdocname = node['refdocname']
if refdocname in secnums:
secnum = secnums[refdocname]
emphnode = node.children[0]
textnode = emphnode.children[0]

toclist = app.builder.env.tocs[refdocname]
anchorname = None
for refnode in toclist.traverse(nodes.reference):
if refnode.astext() == textnode.astext():
anchorname = refnode['anchorname']
if anchorname is None:
continue
linktext = '.'.join(map(str, secnum[anchorname]))
node.replace(emphnode, nodes.Text(linktext
+ ' ' + textnode))

为了使其工作,需要在conf.py中包括numsec扩展名,并且还需要在toctree中添加:numbered:,如下所示:
.. toctree::
:maxdepth: 1
:numbered:

关于latex - Sphinx对包含节号和节标题的其他节的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com