我可以在另一个文件页面中引用另一个头文件的标题并保持标题吗?

时间:2019-12-21 19:14:00

标签: python-sphinx restructuredtext

我正在使用sphinx,并创建了多个rst文件来组织我的文档。我使用.. include:: <filepath/filename.rst>将多个rst文件包含到一个配置文件中,但是当使用:ref:`<reference>`时,这会保留文件名标头标签,但是当我单击链接时,它会将页面隔离开,但是我希望它滚动到引用在同一页面上。当我使用<reference>_时,它将滚动到同一文档中的该区域,但不再保留标题标签。有没有办法让参考标头标签保持不变并在同一页面内滚动,同时仍将文档保留在不同文件中?

index.rst

Welcome to testing's documentation!
===================================

.. toctree::
    :maxdepth: 2
    :caption: Contents:

    test/config

test / config.rst

.. title:

Hello moto
==========

Using ref maintains header

* :ref:`ref-nested`

Using underscore doesn't maintain header

- nested_

.. include:: nested_test/file.rst

.. include:: nested_test/anotherfile.rst

test / anotherdir / file.rst

.. _nested:

I'm a nested header
-------------------

Hi I'm the created nested header

test / anotherdir / anotherfile.rst

.. _ref-nested:

I'm the ref nested header
-------------------------

I'm the ref nested header

enter image description here

正如您在下面看到的那样,第一个链接(:ref :)维护给定的标题,但是如果单击它,它将进入隔离页面。第二个链接不保留标题给定,而是使用实际引用,但是如果单击该链接,它将保留在同一页面上并在文档内移动。

下面是两个图像,当使用:ref:时,它将页面作为独立的第一个文件加载。

enter image description here

我希望链接向下滚动,就像在文档中一样。 enter image description here

2 个答案:

答案 0 :(得分:1)

我在这里找到了另一个堆栈溢出问题所需的答案。如果引用包含在同一文件中,则不必创建引用,我可以引用标题本身。请参阅链接并查看@Baleb答案。

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

答案 1 :(得分:0)

如果我了解您想要的内容,则可以将目标及其标头从包含的文件移动到主文件中。

.. title:

Hello moto
==========

Using ref maintains header

* :ref:`ref-nested`

Using underscore doesn't maintain header

- nested_

.. _nested:

I'm a nested header
-------------------

.. include:: nested_test/file.rst

.. _ref-nested:

I'm the ref nested header
-------------------------

.. include:: nested_test/anotherfile.rst

这还有一个好处,如果将包含的文件包含在多个文件中,则可以指定唯一的目标并避免Sphinx错误。