Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree doesn't include subsections for self in toctree #2103

Open
AndreiPashkin opened this issue Oct 31, 2015 · 8 comments
Open

Tree doesn't include subsections for self in toctree #2103

AndreiPashkin opened this issue Oct 31, 2015 · 8 comments
Labels
type:enhancement enhance or introduce a new feature

Comments

@AndreiPashkin
Copy link

Here is the example:
https://raw.githubusercontent.com/dateutil/dateutil/master/docs/index.rst

And the result is:
https://dateutil.readthedocs.org/en/latest/

Notice - subsections in a tree side-bar are absent.

Similar issues on SO:
http://stackoverflow.com/questions/27338257/using-self-in-sphinx-toctree-doesnt-include-sub-sections
http://stackoverflow.com/questions/20648956/adding-subsections-of-self-in-the-table-of-contents

@tk0miya
Copy link
Member

tk0miya commented Mar 9, 2016

Since the appearance of self in 38e766c, it always only includes the title of the current file. not whole of ToC of the file.

@birkenfeld What is the purpose of the keyword? It seems not enough to use as "sitemap". So I don't know how to use this feature.
Could you tell me your idea?

@tk0miya tk0miya added the type:enhancement enhance or introduce a new feature label Mar 9, 2016
@birkenfeld
Copy link
Member

TBH, I don't know anymore :)

@lukasgraf
Copy link

I stumbled across the same issue - including self in the toctree only inserts a single link to the same document (irrespective of maxdepth), so I don't see how this could be used to generate a sitemap or a document-local ToC (which is what I'm after).

Using the plain reStructuredText .. contents:: directive is somewhat of an alternative, but doesn't have the same features as Sphinx's toctree (e.g. no numbering support).

@arximboldi
Copy link

Workaround: add the index page explicitly to the TOC. E.g. If your master document is called index, do something like:

.. toctree::
    :maxdepth: 2

    index
    tutorial
    ...

Note that without :maxdepth: the TOC entries itself are shown inside the TOC itself recursively once. Also, there are warnings on the console:

/home/raskolnikov/dev/immer/doc/index.md:: WARNING: circular toctree references detected, ignoring: index <- index

@arximboldi
Copy link

arximboldi commented Nov 8, 2016

Oh no! My workaround has one mayor drawback: in the sidebar TOC, when you are in a page other than the index, the Index page entry itself is "opened" and the part of the document where the toc is included is highlighted. Example:

screenshot from 2016-11-09 00-30-03

hagenw added a commit to sfstoolbox/theory that referenced this issue Jan 17, 2018
This is a workaround for the contents.rst file, which we have to use even for a
single file, e.g. sphinx-doc/sphinx#2103

This will be automatically solved by version 3 as we use multiple pages then.
hagenw added a commit to sfstoolbox/theory that referenced this issue Feb 26, 2018
This is a workaround for the contents.rst file, which we have to use even for a
single file, e.g. sphinx-doc/sphinx#2103

This will be automatically solved by version 3 as we use multiple pages then.
hagenw added a commit to sfstoolbox/theory that referenced this issue Mar 14, 2018
This is a workaround for the contents.rst file, which we have to use even for a
single file, e.g. sphinx-doc/sphinx#2103

This will be automatically solved by version 3 as we use multiple pages then.
KostyaEsmukov added a commit to geopy/geopy that referenced this issue Jul 7, 2018
I wanted to have all geocoders listed in the sidebar toctree, because
viewing docs for a specific geocoder is quite a common use-case.
Apparently without introducing level 3 headers that's impossible [1].

sphinx-rtd-theme for some reason cannot render third level of headings
correctly in the sidebar without a `toctree` present in the doc:
they appear at the second level along with the other level two headers.
Hence the need to have `toctree` in the doc. It could've been hidden,
but I figured it might be helpful to have it visible in the doc.
Will see how it goes.

The toctree references the same document, which causes a warning
regarding a circular reference, yet it's the only option I could
find to have the third level of headings be correctly rendered in
the sidebar [2].

[1]: https://stackoverflow.com/a/18002745
[2]: sphinx-doc/sphinx#2103
nginx-hg-mirror pushed a commit to nginx/unit-docs that referenced this issue Jan 11, 2019
The "contents" page contains the global ToC tree, but actually it is not
needed as a separate page.  Unfortunately, there is no easy way to avoid
rendering of this page in Sphinx.  As a workaround, this page is skipped
during deployment.  But Sphinx still includes it in the global ToC and
generates a "prev" link to it in the "index" page.

Alternative solution could be removing the "contents" page and putting
global ToC into the "index" page.  But there is a problem with self
referencing in Sphinx: sphinx-doc/sphinx#2103

So, as a workaround, now the layout is altered to explicitly avoid "prev"
link from the "index" page.
@leotrs
Copy link

leotrs commented Apr 19, 2022

Same issue - would be great if self behaved in the same way as other elements in the toctree.

@uberfastman
Copy link

So I have been wrestling with this issue myself as I wanted index.rst to display an external README.md being pulled from a parent directory with the .. include:: directive, and that README.md has various section headers that I wanted to show up as subsections within the toctree, but simply don't get rendered there when using the self keyword.

Per @arximboldi's workaround, you can reference index itself within the actual index.rst, which will then render the subsections in index.rst in the toctree as desired, but it will also have the unwanted side effect of duplicating any toctree files besides the index itself within the toctree (as noted in the comment on this reply to a stackoverflow post for this very issue here). It is worth noting that for me, using :maxdepth: 2 did not remove the recursive index entries from the other files added to index.rst, but the below improvement sorts that out.

Improvement to Workaround

After much tinkering, I realized that until self actually renders subsections (if ever), an alternative approach can be used to avoid the toctree file duplicates while still rendering subsections using the circular index keyword. Since sphinx supports the use of CUSTOM CSS, you can use the above approach with the index keyword, and then simply add a custom CSS file to your conf.py (add your custom css to the _static directory and make sure to include html_static_path = ["_static"] in conf.py), and within that custom CSS file you can target the duplicate elements in the toctree (both in the sidebar and the main content) like so:

/* necessary to remove the duplicated toctree entries created by referencing index in the sphinx index.rst */
.wy-menu-vertical > ul > li:nth-child(1),
.wy-menu-vertical > ul > li:nth-child(2),
.wy-nav-content .toctree-wrapper > ul > li:nth-child(1),
.wy-nav-content .toctree-wrapper > ul > li:nth-child(2) {
    display: none;
}

Of course this addition to the "solution" is still quite hacky, but it does result in a nice looking toctree with all subsections rendered and no duplicate toctree entries from other files in index.rst, so until there is a true solution implemented, hopefully this approach helps anyone trying to solve this issue.

@gilir-fcat
Copy link

Any news on this? It seems like a very common use case that is unfortunately not supported well currently (and the workaround didn't work for me for some reason).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

9 participants