Internal hyperlink in reStructuredText with customized text
Asked Answered
K

5

22

I know how to create an external hyperlink with customized text.

`My cool link <http://www.asdf.com>`_

But I want to link to an internal reference.

.. _foo:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

So I'd like to do something like

`My cool internal link <foo>`_

But this doesn't work.

Kulp answered 11/4, 2016 at 4:12 Comment(0)
C
4

When using reStructuredText with Sphinx, cross-reference other locations in the documentation with the ref role:

:ref:`link text <link-target>`

The custom link text is optional if the referenced object defines a title or caption, as is the case for documents, sections, figures, etc. The target's title will then be used as the link text, but the angle brackets must be left out: :ref:`link-target` . The link text is mandatory when referencing paragraphs or images, as they don't have titles/captions associated with them.

The link target has to be defined somewhere in the documentation, possibly in other documents, i.e. .rst files. For sections, the extension Autosectionlabel would do this automatically and the section's title then becomes the target, and thus also link text. In all other cases, internal hyperlink targets must be declared manually, for example like so:

.. _paragraph-to-be-linked-to:

This paragraph will be referenced from elsewhere in the documentation
with :ref:`that paragraph <paragraph-to-be-linked-to>`.

Note the leading underscore in the target definition. It can be thought of as a right-pointing arrow, here pointing inward, toward the link target. We may also link to specific positions inside (long) paragraphs with inline internal targets:

We want to point to _`this position` inside the paragraph
with :ref:`that paragraph <this position>` from elsewhere.

When using reStructuredText with Docutils alone, the ref role is not available, as it is a syntax extension added by Sphinx. Instead, we need the following syntax to create hyperlinks:

`link text <link-target_>`_

Note the two trailing underscores, one after the link target and another after the full hyperlink. Here the imaginary arrows point outward.

Docutils only processes standalone files. It is used by Sphinx as a back-end to parse individual reStructuredText source files. In Sphinx documentation, the latter syntax will therefore only work for internal links within the same document, but not across documents, i.e. a hyperlink defined in one .rst file referring to a target in another file.

Instead of the ref role, one may find the any role provided by Sphinx even more convenient. It will additionally find link targets automatically created by extensions, such as for source code objects documented with Autodoc. When declared as the default role in conf.py, with default_role = 'any', we could then write `link text <link-target>` or even just `link-target` to create internal hyperlinks. Except for the absence of trailing underscores, the former looks much like the syntax for single-document reStructuredText as recognized by Docutils, but is processed by Sphinx in a cross-document manner.

Changeover answered 14/12, 2021 at 20:46 Comment(0)
K
17

Solution:

`My cool internal link <foo_>`_
Kulp answered 11/4, 2016 at 4:16 Comment(1)
For some reason it doesn't find my label with this solution (ERROR: Unknown target name)Toshiatoshiko
W
12

Use 2 underscores on text and 1 undescore on link target, example:

`My cool internal link <foo_>`__

... somewhere lower ...

.. _foo:

This staff is referenced by "My cool internal link"

When I use one underscore instead of 2, rst2pdf throws an error.

Woodworth answered 11/11, 2016 at 19:51 Comment(1)
This doesn't work.Mirna
C
4

When using reStructuredText with Sphinx, cross-reference other locations in the documentation with the ref role:

:ref:`link text <link-target>`

The custom link text is optional if the referenced object defines a title or caption, as is the case for documents, sections, figures, etc. The target's title will then be used as the link text, but the angle brackets must be left out: :ref:`link-target` . The link text is mandatory when referencing paragraphs or images, as they don't have titles/captions associated with them.

The link target has to be defined somewhere in the documentation, possibly in other documents, i.e. .rst files. For sections, the extension Autosectionlabel would do this automatically and the section's title then becomes the target, and thus also link text. In all other cases, internal hyperlink targets must be declared manually, for example like so:

.. _paragraph-to-be-linked-to:

This paragraph will be referenced from elsewhere in the documentation
with :ref:`that paragraph <paragraph-to-be-linked-to>`.

Note the leading underscore in the target definition. It can be thought of as a right-pointing arrow, here pointing inward, toward the link target. We may also link to specific positions inside (long) paragraphs with inline internal targets:

We want to point to _`this position` inside the paragraph
with :ref:`that paragraph <this position>` from elsewhere.

When using reStructuredText with Docutils alone, the ref role is not available, as it is a syntax extension added by Sphinx. Instead, we need the following syntax to create hyperlinks:

`link text <link-target_>`_

Note the two trailing underscores, one after the link target and another after the full hyperlink. Here the imaginary arrows point outward.

Docutils only processes standalone files. It is used by Sphinx as a back-end to parse individual reStructuredText source files. In Sphinx documentation, the latter syntax will therefore only work for internal links within the same document, but not across documents, i.e. a hyperlink defined in one .rst file referring to a target in another file.

Instead of the ref role, one may find the any role provided by Sphinx even more convenient. It will additionally find link targets automatically created by extensions, such as for source code objects documented with Autodoc. When declared as the default role in conf.py, with default_role = 'any', we could then write `link text <link-target>` or even just `link-target` to create internal hyperlinks. Except for the absence of trailing underscores, the former looks much like the syntax for single-document reStructuredText as recognized by Docutils, but is processed by Sphinx in a cross-document manner.

Changeover answered 14/12, 2021 at 20:46 Comment(0)
T
3

If John's solution doesn't work for you:

`My cool internal link<foo>`
Toshiatoshiko answered 12/4, 2017 at 10:18 Comment(1)
That doesn't seem to work. I have to use my link <target_> with a _ after the last backtick. Also the link target doesn't work for headings unfortunately, only anchors. As a workaround I put #my-heading-text as the target and that works for HTML.Cornflakes
B
2

You just needed to remove the space after your custom title, and before the angle brackets, and use the :ref: directive:

This works:

:ref:`My cool internal link with no space before bracket<foo>`

This doesn't:

:ref:`My cool internal link with space <foo>`
Buckbuckaroo answered 4/11, 2019 at 10:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.