Search code examples
python-sphinxrestructuredtext

Can I reference a header from another rst document and maintain the header while staying in the same file page?


I'm using sphinx and I created multiple rst files to organize my documents. I used .. include:: <filepath/filename.rst> to include multiple rst files into one config file but when using :ref:`<reference>` this maintains the file name header label but when I click the link it isolates the page but I want it to scroll to the reference in the same page. When I use <reference>_ this will scroll to the area within the same document but no longer keeps the header label. Is there a way I can keep the reference header label and scroll within the same page while still keeping the the docs in different files?

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

As you see below the first link(:ref:) maintains the header given but if you click it, it will go to an isolate page. The second link doesn't maintain the header give but uses the actual reference but if clicked it will stay on the same page and would move within the document.

Below are two images, when using :ref: it loads the page as an isolated rst file.

enter image description here

I want the link to scroll down as if within the document. enter image description here


Solution

  • I found the answer I wanted on a different stack overflow question here. I don't have to create a reference if it's in the same file using include I can reference the title itself. Refer to the link and see @Baleb answer.

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