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

空扰寡人 提交于 2019-12-11 01:19:57

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/22212848/is-it-possible-to-make-automatic-section-header-numbering-in-rest-start-at-subse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!