Automatic Header Numbering with Table Of Contents

penguin.pingu's Avatar

penguin.pingu

25 Jan, 2016 10:44 PM

Is it possible to have headers automatically number based on position and level, and also have these numbers reflected in the table of contents?

I am able to number headers via .css, however these are not reflected in the TOC.

  1. Support Staff 1 Posted by Brett on 26 Jan, 2016 12:00 AM

    Brett's Avatar

    It's not really possible unless you replicate the CSS technique using
    the #generated_toc ul and li levels.

    -Brett

  2. 2 Posted by penguin.pingu on 26 Jan, 2016 02:47 PM

    penguin.pingu's Avatar

    Thanks for the reply Brett,

    > unless you replicate the CSS technique using
    > the #generated_toc ul and li levels

    Do you know if there is an example?

    Dave

  3. Support Staff 3 Posted by Brett on 26 Jan, 2016 03:32 PM

    Brett's Avatar

    To accomplish it with CSS, use the following in the Preferences->Style->Additional CSS (Edit CSS button). This assumes that chapters are h1, subchapters h2, etc., and a well structured document. It will then apply the chapter and sub-item numbering in decimal format. For a document with a different structure, you'd need to adjust all of the selectors to match, so it's not a great universal solution.

    body {
        counter-reset: chapter; /* create a chapter counter scope */
    }
    h1:before {
        content: "Chapter " counter(chapter) ". ";
        counter-increment: chapter; /* add 1 to chapter */
    }
    h1 {
        counter-reset: subchapter; /* set section to 0 */
    }
    h2:before {
        content: counter(chapter) "." counter(subchapter) " ";
        counter-increment: subchapter;
    }
    h2 {
        counter-reset: section;
    }
    h3:before {
        content: counter(chapter) "." counter(subchapter) "." counter(section) " ";
        counter-increment: section;
    }
    h3 {
        counter-reset: subsection;
    }
    h4:before {
        content: counter(chapter) "." counter(subchapter) "." counter(section) "." counter(subsection) " ";
        counter-increment: subsection;
    }
    h4 {
        counter-reset: subsubsection;
    }
    h5:before {
        content: counter(chapter) "." counter(subchapter) "." counter(section) "." counter(subsection) "." counter(subsubsection) " ";
        counter-increment: subsubsection;
    }
    h5 {
        counter-reset: subsubsubsection;
    }
    h6:before {
        content: counter(chapter) "." counter(subchapter) "." counter(section) "." counter(subsection) "." counter(subsubsection) "." counter(subsubsubsection) " ";
        counter-increment: subsubsubsection;
    }
    
    
    #mkreplaced-toc {
        counter-reset: tocchapter; /* create a chapter counter scope */
    }
    
    #mkreplaced-toc li {
        list-style-type: none!important;
    }
    #mkreplaced-toc li:before {
        content: "Chapter " counter(tocchapter) ". "!important;
        counter-increment: tocchapter; /* add 1 to chapter */
    }
    #mkreplaced-toc ul {
        counter-reset: tocsubchapter; /* set section to 0 */
    }
    #mkreplaced-toc ul li:before {
        content: counter(tocchapter) "." counter(tocsubchapter) " "!important;
        counter-increment: tocsubchapter;
    }
    #mkreplaced-toc ul ul {
        counter-reset: tocsection;
    }
    #mkreplaced-toc ul ul li:before {
        content: counter(tocchapter) "." counter(tocsubchapter) "." counter(tocsection) " "!important;
        counter-increment: tocsection;
    }
    #mkreplaced-toc ul ul ul {
        counter-reset: tocsubsection;
    }
    #mkreplaced-toc ul ul ul li:before {
        content: counter(tocchapter) "." counter(tocsubchapter) "." counter(tocsection) "." counter(tocsubsection) " "!important;
        counter-increment: tocsubsection;
    }
    #mkreplaced-toc ul ul ul ul {
        counter-reset: tocsubsubsection;
    }
    #mkreplaced-toc ul ul ul ul li:before {
        content: counter(tocchapter) "." counter(tocsubchapter) "." counter(tocsection) "." counter(tocsubsection) "." counter(tocsubsubsection) " "!important;
        counter-increment: tocsubsubsection;
    }
    #mkreplaced-toc ul ul ul ul ul {
        counter-reset: tocsubsubsubsection;
    }
    #mkreplaced-toc ul ul ul ul ul li:before {
        content: counter(tocchapter) "." counter(tocsubchapter) "." counter(tocsection) "." counter(tocsubsection) "." counter(tocsubsubsection) "." counter(tocsubsubsubsection) " "!important;
        counter-increment: tocsubsubsubsection;
    }
    
  4. 4 Posted by penguin.pingu on 26 Jan, 2016 08:41 PM

    penguin.pingu's Avatar

    That's great, thanks very much Brett!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac