How to make an internal link to a heading in sphinx restructuredtext without creating arbitrary labels?

后端 未结 4 624
臣服心动
臣服心动 2020-12-29 03:01

I have a document with many headings and sub-headings. Further into the text I want to link back to one of the headings. How can I do this without the redundancy of :r

4条回答
  •  醉梦人生
    2020-12-29 03:32

    Using the headline text isn't a good choice. Headlines may change or might get corrected. There is now easy way to figure out who many and where links are broken after a change.

    Using ref is advised over standard reStructuredText links to sections (like `Section title`_) because it works across files, when section headings are changed, will raise warnings if incorrect, and works for all builders that support cross-references.
    Source: https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-ref

    Using the sphinx.ext.autosectionlabel extension as proposed by Adam Michael Wood is at least a more structured approach then using implicitly defined anchors as proposed by Chris.


    You should use explicit linking with references and symbolic target names (like LaTeX does since ages.)

    1. Use .. _refname: to create a target.
    2. Use :ref:`refname` to reference the target.

    If a target is followed by a headline, this headline will be used als link text.

提交回复
热议问题