Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :ref: labels? The contents seems to pick up headers just fine. I was hoping for something like this: `#polled-data-retrieval`_.

like image 466
caduceus Avatar asked Sep 05 '13 09:09

caduceus


People also ask

What is RST Sphinx?

RST stands for ReStructured Text. It is the standard markup language used for documenting python packages. Sphinx is the Python package that generates an html website from RST files, and it is what we are using to generate this site.

How do you use a sphinx code block?

Literal code blocks (ref) are introduced by ending a paragraph with the special marker :: . The literal block must be indented (and, like all paragraphs, separated from the surrounding ones by blank lines): This is a normal text paragraph.


2 Answers

reStructuredText supports implicit hyperlink targets. From the reStructuredText quick reference:

Section titles, footnotes, and citations automatically generate hyperlink targets (the title text or footnote/citation label is used as the hyperlink name).

So the following text (lifted from the reStructuredText quick reference, spelling mistakes and all):

Titles are targets, too ======================= Implict references, like `Titles are targets, too`_. 

produces HTML similar to the following:

<strong><a name="title">Titles are targets, too</a></strong>  <p>Implict references, like <a href="#title">Titles are targets, too</a>.</p> 
like image 74
Chris Avatar answered Sep 30 '22 21:09

Chris


New, better answer for 2016!

The autosection extension lets you do this easily, with real cross references.

============= Some Document =============   Internal Headline ================= 

then, later...

=============== Some Other Doc ===============   A link-  :ref:`Internal Headline` 

This extension is built-in, so all you need is to edit conf.py

extensions = [     .     . other     . extensions     . already     . listed     .     'sphinx.ext.autosectionlabel', ] 

The only thing you have to be careful of is that now you can't duplicate internal headlines across the doc collection. (Worth it.)

like image 41
Adam Michael Wood Avatar answered Sep 30 '22 20:09

Adam Michael Wood