16. Sections

Sections partition the document into a content hierarchy. In AsciiDoc, sections are defined using section titles.

A section title represents the heading for a section. Section title levels are specified by two to six equal signs. The number of equal signs in front of the title represents the nesting level (using a 0-based index) of the section.

Section titles available in an article doctype
= Document Title (Level 0)

== Level 1 Section Title

=== Level 2 Section Title

==== Level 3 Section Title

===== Level 4 Section Title

====== Level 5 Section Title

== Another Level 1 Section Title
Render section titles

Document Title (Level 0)

Level 1 Section Title

Level 2 Section Title

Level 3 Section Title

Level 4 Section Title
Level 5 Section Title

Another Level 1 Section Title

In addition to the equals sign marker used for defining single-line section titles, Asciidoctor recognizes the hash symbol (#) from Markdown. That means the outline of a Markdown document will be converted just fine as an AsciiDoc document.

Section levels must be nested logically. There are two rules you must follow:

  1. A document can only have multiple level 0 sections if the doctype is set to book.

    • The first level 0 section is the document title; subsequent level 0 sections represent parts.

  2. Section levels cannot be skipped when nesting sections (e.g., you can’t nest a level 5 section directly inside a level 3 section; an intermediary level 4 section is required).

For example, the following syntax is illegal:

= Document Title

= Illegal Level 0 Section (violates rule #1)

== First Section

==== Illegal Nested Section (violates rule #2)

Content above the first section title is designated as the document’s preamble. Once the first section title is reached, content is associated with the section it is nested in.

== First Section

Content of first section

=== Nested Section

Content of nested section

== Second Section

Content of second section

16.1. Titles as HTML Headings

When the document is converted to HTML 5 (using the built-in html5 backend), each section title becomes a heading element where the heading level matches the number of equal signs. For example, a level 1 section (==) maps to an <h2> element.

16.2. Auto-generated IDs

If you don’t specify a custom ID for a section, the processor automatically generates an ID from the section’s title and assigns it to the section by default.

The ID is derived from the section title as follows:

  • Inline formatting is applied (in title substitution order).

  • All characters are converted to lowercase.

  • The value of the idprefix attribute (_ by default) is prepended.

  • Character references, HTML/XML tags, and invalid ID characters are removed.

    • Refer to the NT-Name section of the XML specification for a list of valid ID characters.

    • Prior to 1.5.7, HTML/XML tags were not removed and character references and invalid ID characters were replaced with the value of the idseparator attribute (_ by default).

  • Spaces, hyphens, and periods are replaced with the value of the idseparator attribute (_ by default)

  • Repeating separator characters are condensed.

  • If necessary, a sequence number is appended until the ID is unique within the document.

For example, the section title Wiley & Sons, Inc. produces the ID _wiley_sons_inc. You can customize the replacement characters or toggle ID auto-generation using document attributes.

You can change the ID prefix by setting the idprefix attribute. The value of the idprefix attribute must begin with a valid ID start character and can have any number of additional valid ID characters.

:idprefix: id_

If you want to remove the prefix, set the attribute to an empty value.

:idprefix:
If you set the idprefix to blank, you could end up generating IDs that are invalid in DocBook output (e.g., an ID that begins with a number) or that match a built-in ID in the HTML output (e.g., header). In this case, we recommend either using a non-empty value of idprefix or assigning explicit IDs to your sections.

You can change the word separator using the idseparator attribute. Unless empty, the value of the idseparator must be exactly one valid ID character.

:idseparator: -

If you don’t want to use a separator, set the attribute to an empty value.

:idseparator:
When a document is rendered on GitHub, the idprefix is set to an empty value and the idseparator is set to -. These settings are used to ensure that the IDs generated by GitHub match the IDs generated by Asciidoctor.

To disable the auto-generation of section IDs, unset the sectids attribute.

:sectids!:

Custom IDs, covered next, are used even if auto-generation of section IDs is disabled.

If you disable auto-generated section IDs, and you don’t assign a custom ID to a section, you won’t be able to link to (i.e., reference) that section.

16.3. Custom IDs

You can assign a custom ID and optional reference text (i.e., label) to a section (see Defining an Anchor). The custom ID is used in place of the auto-generated ID. This can be useful when you want to define a stable anchor for linking to a section using a cross reference. The reference text is used when referencing the ID without specifying explicit text. This is a good way to centrally manage the automatic reference text that is used to refer to a section.

Here’s an example of a section with a custom ID:

[#tiger-subspecies]
=== Subspecies of Tiger

Here’s an example of a section with a custom ID and reference text:

[[tiger-subspecies,Tigers]]
=== Subspecies of Tiger

The ID can be written using the following shorthand (though there’s no shorthand yet for the reference text):

[#tiger-subspecies,reftext=Tigers]
=== Subspecies of Tiger

The value of the reftext attribute must be quoted if it contains spaces or commas.

Asciidoctor recognizes all valid UTF-8 characters in section IDs. When using the double square bracket form, the ID must conform to the XML Name rules, which means the ID must start with a letter, an underscore, or a colon. If you’re generating a PDF from AsciiDoc using a2x and dblatex, see Using UTF-8 titles with a2x to learn about the required latex.encoding=utf8 switch to activate this portability.

16.4. Multiple Anchors

While a section title can only have a single primary ID, as of Asciidoctor 1.5.7, it’s possible to register additional anchor points on a section title using inline anchors. This feature works regardless of whether you assign a custom ID.

Here’s how to register additional anchor points when using an auto-generated ID:

Register additional leading anchor points
== [[secondary-id]][[tertiary-id]]Section Title
Register additional trailing anchor points
== Section Title[[secondary-id]][[tertiary-id]]

You can place the anchors anywhere you want within the section title line. Where you place them is where the anchor will end up in the output, so it’s best to put them either at the beginning or the end. The beginning is the preferred location.

These additional anchor points don’t interfere with defining a custom ID.

Register additional leading anchor points on a section title with a custom ID
[#primary-id]
== [[secondary-id]][[tertiary-id]]Section Title

Alternately, you can move the primary ID inline by placing it after the section title, offset by a single space.

Set the custom ID within the section title
== Section Title[[secondary-id]][[tertiary-id]] [[primary-id]]

However, we don’t recommend defining the primary ID inline as it’s more difficult to read.

To turn section titles into links, enable the sectlinks attribute. The default Asciidoctor stylesheet displays linked section titles with the same color and styles as unlinked section titles.

16.6. Anchors

When the sectanchors attribute is enabled on a document, an anchor (empty link) is added before the section title. The default Asciidoctor stylesheet renders the anchor as a section entity (§) that floats to the left of the section title.

16.7. Numbering

Asciidoctor allows section numbering to be toggled on and off throughout a document. You can enable section numbers using the sectnums attribute.

:sectnums:
Asciidoctor still supports the attribute name numbered to number sections for backward compatibility with AsciiDoc Python, but the name sectnums is preferred.

You can also use this attribute entry above any section title in the document to toggle the auto-numbering setting. When you want to turn off the numbering, add an exclamation point to the end of the attribute name:

:sectnums!:

== Unnumbered Section

For regions of the document where section numbering is turned off, the section numbering will not be incremented.

Given:

= Document Title

:sectnums!:

== Colophon Section

== Another Colophon Section

== Last Colophon Section

:sectnums:

== Section One

== Section Two

== Section Three

The sections will be numbered as follows:

Colophon Section

Another Colophon Section

Last Colophon Section

1. Section One

2. Section Two

3. Section Three

Asciidoctor will always curtail incrementing the section number in regions of the document where section numbers are off.

If sectnums is set on the command line (or API), that overrides the value set in the document header, but it does not prevent the document from toggling the value for regions of the document.

If sectnums! is set on the command line (or API), then the numbers are disabled regardless of the setting within the document.

Flexible attributes
The sectnums attribute is referred to as a “flexible attribute”, which means it can always be changed midstream in the document, even if it is enabled through the API or CLI. At the time of writing, sectnums is unique in this way, though other attributes may receive this special status in the future.

16.7.1. Numbering depth

You can restrict section numbering depth by defining the sectnumlevels (document header-only) attribute.

:sectnumlevels: 2 (1)
1 When the sectnumlevels attribute is assigned a value of 2, section titles with levels 3 through 5 are not numbered (i.e., not prefixed with a number).

When the doctype is book, recall that level-1 sections become chapters. Therefore, a sectnumlevels of 4 translates to 3 levels of numbered sections inside each chapter.

Assigning sectnumlevels a value of 0 is effectively the same as disabling section numbering (i.e., sectnums!).

16.8. Discrete Headings (aka Floating Titles)

To make a discrete heading, you add the discrete attribute to any section title. A discrete heading is styled in a manner similar to a section title, but is not part of the section hierarchy (i.e., and thus excluded from section nesting requirements), it does not enclose subsequent blocks, and it is not included in the table of contents. The discrete style effectively demotes the section title to a normal heading.

Alternately, you may use the float attribute to identify a discrete heading. In this context, the term “float” does not imply layout, meaning it does not float to the left or right of other content blocks. It just means it’s not anchored to the section hierarchy.

Here’s an example of a discrete heading in use.

**** (1)
Discrete headings are useful for making headings inside of other blocks, like this sidebar.

[discrete] (2)
== Discrete Heading (3)

Discrete headings can be used where sections are not permitted.
****
1 A delimiter line that indicates the start of a sidebar block.
2 Set the discrete attribute above the section title to demote it to a discrete heading.
3 The discrete heading title is designated by one to six equal signs, just like a section title.

16.9. Section Styles

Asciidoctor provides styles for the frontmatter and backmatter sections commonly found in journal articles, academic papers, and books. The styles are:

  • colophon

  • abstract

  • preface

  • dedication

  • part introduction

  • appendix

  • glossary

  • bibliography

  • index

These styles are available to the article and book document types, with the exception of the part introduction style which is exclusive to books.

In general, the section style attribute is set above a level 1 section title or block of text. For instance, the example below shows how to label a section as an abstract.

[abstract]
== Documentation Abstract

Documentation is a distillation of many long, squiggly adventures.

The structure and usage rules for each section style is explained in Structuring, Navigating, and Referencing Your Content.

16.10. Sections Summary

Section attributes and values
Attribute Value(s) Example Syntax Comments

sectids

n/a

:sectids!:

Set to auto-generate section IDs by default.

idprefix

_, or user defined text

:idprefix: id_

Set to prepend string to generated section ID.

idseparator

_, or user defined character

:idseparator: -

Set to insert character between words in generated section ID.

sectanchors

n/a

:sectanchors:

Asciidoctor only

sectlinks

n/a

:sectlinks:

Asciidoctor only

sectnums

n/a

:sectnums:

Section numbering is off by default. Can be toggled on or off in the flow of the document.

sectnumlevels

0-5

:sectnumlevels: 3

Set to autogenerate section numbers up to level three by default. Setting a value of 0 has the same effect as setting sectnums!.