【问题标题】:Can I reference a header from another rst document and maintain the header while staying in the same file page?我可以从另一个 rst 文档中引用一个标题并在同一文件页面中保留标题吗?
【发布时间】:2020-04-13 18:54:18
【问题描述】:

我正在使用 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

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

以下是两张图片,当使用:ref: 时,它会将页面加载为一个独立的 rst 文件。

我希望链接像在文档中一样向下滚动。

【问题讨论】:

  • “转到隔离页面”是什么意思?
  • 嗨@mzjn,我继续更新了这个问题。我所说的隔离页面的意思是,当我使用.. include:: 时,它在第一个文件中包含另一个文档,但是当我说隔离页面时,而不是在单击链接时向下滚动。它将页面作为单独的文档/视图加载。而不是在整个文档中向下滚动。

标签: python-sphinx restructuredtext


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果我明白你想要什么,你可以将目标及其标题从包含的文件移动到主文件中。

    .. 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 错误。

    【讨论】:

    • 感谢您的回答,但我希望能够将信息分开。我继续并更新了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2012-05-28
    • 2013-05-29
    • 2012-03-19
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多