Search code examples
asciidocasciidoctor

Asciidoc: Is there any way to access current section number


I would like to add the current section number in front of the table numbering in the caption of my tables.

Example:

Tables under heading 1.1 should have the caption "Table 1.1-1: Any Caption", "Table 1.1-2: Another Caption" etc. Or tables under heading 3.2.1 the caption should be "Table 3.2.1-1: ...", "Table 3.2.1-2: ..." etc.

My current approach unfortunately increments all other headings and does not display the numbering of the headings.

[caption=,title="Dataset {counter:chapter-number}-{counter:section-number}-{counter:table-number}: {table-caption}"]

Is there any way to access current section number?


Solution

  • When I need such a complex feature that is more than the basic capabilities of Asciidoc I usually use the Jamal preprocessor, which I created years ago.

    https://github.com/verhas/jamal

    With the macro possibilities of the macro preprocessor, you can number the sections freely. There is a macro supporting that in the snippet library.

    As an example, you can see any new articled from 2023 on my blog: https://javax0.github.io/

    This blog is open source, in the sense that you can see the source created using Jamal, Asciidoc and Jekyll at https://github.com/javax0/javax0.github.io

    When you will be interested in is the https://github.com/javax0/javax0.github.io/blob/master/javax0.jim file defining

    {@counter:define id=:scount}
    {@counter:define id=:sscount}
    {@counter:define id=:ssscount}
    {@define section($x)=== {scount}. $x{@counter:define id=:sscount}}
    {@define subsection($x)==== {scount last}.{sscount}. $x{@counter:define id=:ssscount}}
    {@define subsubsection($x)===== {scount last}.{sscount last}.{ssscount}. $x}
    

    This will, of course, need an extra step when you convert your Asciidoc files in your build pipe. When you edit your files, the Asciidoctor plugin version of Jamal will work automatically. For example, if you install it into IntelliJ, you get all the WYSIWYG editing with Jamal macros.