【问题标题】:Sphinx references to other sections containing section number and section title狮身人面像对包含章节编号和章节标题的其他章节的引用
【发布时间】:2012-11-30 15:54:41
【问题描述】:

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

.. _human-factor:

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

...

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

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

(详见人的因素)

相反,我希望它的格式如下:

(详见5.1 人为因素)

我尝试用谷歌搜索解决方案,发现乳胶 hyperref 包可以做到这一点,但我不知道如何将它添加到 Sphinx 构建中。

【问题讨论】:

    标签: latex python-sphinx restructuredtext


    【解决方案1】:

    我基本上是从这里使用 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 扩展名,并在目录树中添加 :numbered: ,如下所示:

    .. toctree::
       :maxdepth: 1
       :numbered:
    

    【讨论】:

    • 我发现这个解决方案只适用于不使用部件的文档。 secnums 中暴露的内部部分编号随着每个部分重新开始:-(
    猜你喜欢
    • 2021-08-26
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2021-06-07
    相关资源
    最近更新 更多