How to create a link to a header in restructuredtext?

白昼怎懂夜的黑 提交于 2019-12-02 03:14:08

问题


I have a document in restructuredtest like:

Header 1
========

and from some any other point (might be the same 'rst' file or a different one) I want to create a hyperlink to that header. So that when a user clicks on it, he gets to the page with the header Header 1

How to do that?

I tried to put the following line in the other document (according to this documentation):

see :ref:`Header 1`

but what I get is the following:

see Header 1

without any link...

I also tried to follow this documentation:

What I put in to the rst file is the following

see `Header 1`_

and what I see is the following link:

see `Header 1`_

which does not look very nice ...


回答1:


Your first link was almost correct. You need to add a label preceding the section header, separated by a blank line. See Inline markup, Cross-referencing arbitrary locations, using the :ref: directive.

In your case:

.. _header-1-label-name:

Header 1
========

Some text

Here is a section reference: :ref:`header-1-label-name`.

Here is a section reference with a title: :ref:`Header 1 with a title <header-1-label-name>`.



回答2:


In addition to the accepted answer, the label you add (in this case .. _header-1-label-name:) is required to have a dash. So a simple .. _label: won't do. Took me a while to figure that out.



来源:https://stackoverflow.com/questions/48888604/how-to-create-a-link-to-a-header-in-restructuredtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!