Is it possible to make automatic section header numbering in reST start at subsection level?

StackOverflow https://stackoverflow.com/questions/22212848

  •  10-06-2023
  •  | 
  •  

Question

Is there anyway to get reST to start numbering the sections only, at say, the second level? I am using the auto-numbering setting:

.. section-numbering::

What I would like is for the input:

Section A
=========

Subsection A.1
------------

Subsection A.2
------------

Section B
=========

Subsection B.1
------------

Subsection B.2
------------

to output Section A and B's headers unnumbered, but number the subsections (that is, Subsection A.1, A.2 and B.1, B.2).

Having looked through the documentation, it seems like you can limit how deep the numbering goes, but not at which depth it starts.

Was it helpful?

Solution

Docutils, the reference implementation of reStructuredText and what Pelican is built upon, does not have options for starting section number at arbitrary levels (as far as I can tell). However, rather than relying on the section-numbering role, you could use some simple CSS to style your section headers. This way you would not have the numbers in the generated markup, but you would still get the numbering you are after. A good article demonstrating the use of CSS generated content counters can be found here. The basic CSS is:

body {counter-reset: h2}
h2 {counter-reset: h3}
h3 {counter-reset: h4}
h4 {counter-reset: h5}
h5 {counter-reset: h6}

h2:before {counter-increment: h2; content: counter(h2) ". "}
h3:before {counter-increment: h3; content: counter(h2) "." counter(h3) ". "}
h4:before {counter-increment: h4; content: counter(h2) "." counter(h3) "." counter(h4) ". "}
h5:before {counter-increment: h5; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "}
h6:before {counter-increment: h6; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "}

This starts the section numbering at h2 elements, all the way down to h6 elements. All that you should need to do is add the CSS above to one of your stylesheets. This will give you section headers of the form (although I have cheated and just used Markdown here!):

h1 element

1. h2 element

1.1 h3 element

1.2 h3 element

2. h2 element

2.1 h3 element

2.2 h3 element

2.2.1 h4 element

h1 element

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top