10

Say my some.rst file looks like this:

============
My Title
============

1. Section
================

2. Section
=================

After compiling, in the resulting some.html there will be a table of contents in the side bar that appears as:

My Title

  1. Section
  2. Section

Is there a simple way to remove 'My Title' from the table of contents in some.html?

Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
b_dev
  • 2,568
  • 6
  • 34
  • 43

6 Answers6

7

I was able to solve this by using the .. raw:: html method as described above, with a slight tweak (that avoided breaking the auto-generated TOC). as described earlier, if your file only contains .. raw:: html headings, it will break the Sphinx auto-generated TOC. however, if you use .. raw:: html and add --------------------- below it, it won't show on the left-hand nav, and won't break the TOC. e.g.

so i finally accidentally figured out how to get headings to not be displayed in the left-hand TOC. if your file only contains .. raw:: html h2 headings, it will break the sphinx auto-generated TOC (as mentioned in the stackoverflow article). however, if you use .. raw:: html and --------------------- below it, it won't show on the left-hand nav, and won't break the TOC :star2: e.g.

.. raw:: html

   <h2>What Can I Do With KSQL?</h2>

---------------------
Joel Hamill
  • 71
  • 1
  • 1
4

The easy way is to use an object type that's ignored by the TOC directive:

.. rubric:: My Title

This creates text that looks somewhat like a heading, but is excluded from the TOC. You can update your CSS file with whatever style you want for the .rubric class, even mimicking h1 style if you like.

See "Non-TOC headings within a Restructuredtext page" for how to define rubric styled classes.

Community
  • 1
  • 1
Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
  • This also breaks the {{ toctree() }} (if you are using it). – natke Aug 22 '17 at 18:07
  • What if we don't have access to the document's content (eg;, `.. include:: x`)? Do you think there is still a workaround. I find it strange that sphinx mixes titles with global toctree. – cglacet Nov 20 '20 at 13:18
2

If you are trying to remove it from all of your documents, try customizing the default template. Otherwise you will need to modify the HTML builder by creating a subclass.

devin_s
  • 3,345
  • 1
  • 27
  • 32
  • Any suggestions on how I could modify `localtoc.html` to remove the title from appearing....I cant seem to figure it out just from the docs...thanks for the help – b_dev Sep 07 '11 at 06:49
  • I can't see how you would do it using localtoc.html. This merely contains a title and a call to {{ toc }} – natke Aug 22 '17 at 17:57
1

Very late to this party, I know. I just had this issue, needed to mimic h2 and was not able to edit the style sheet.

My solution ended up being adding raw html in the some.rst:

:raw-html:`<h1>My Title</h1>`

1. Section
================

2. Section
=================
thimic
  • 189
  • 1
  • 8
0

You can make your own h3 tag.

For your header you use:

|start-h3| My title |end-h3|

Later at the end of the file you write:

.. |start-h3| raw:: html

     <h3>

.. |end-h3| raw:: html

     </h3>
0

It seems you are talking about a local TOC (a "on this page" TOC). Unfortunately, Sphinx always prints a document title as first item (<li>). Actual local TOC (section titles) are all nested <ul> inside the first document title <li>. (Yes, it is annoying.)

You have two options:

If current Sphinx theme has local TOC, you have to tweak produced HTML markup. For example, by hiding document title using CSS. Copy-pasting from https://techwriter.documatt.com/2021/sphinx-localtoc-remove-document-title.html:

.localtoc > ul {
  margin: 0;
}
.localtoc > ul > li {
  list-style-type: none;
}
.localtoc > ul > li > a {
  display: none;
}
.localtoc > ul > li > ul {
  // any additional styles to local toc <ul>
}

Alternative might be to manually print local TOC, e.g. at the beginning of a document with the contents:: directive and its :local: option. Example taken from https://restructuredtext.documatt.com/element/contents.html#no-current-document-title:

#################################
Contents without a document title
#################################

Testing ``contents::`` directive with ``:local:`` flag.

.. contents::
   :local:

**********
Section L2
**********

Section L3
==========
Matt Warrick
  • 1,385
  • 1
  • 13
  • 22