All you need is to define a number range for your examples, the same way figures or illustrations are numbered in captions.
In the revised sample, I created two paragraph styles to avoid direct formatting you applied on your examples. Also, creating sections for them is not necessary here because you don’t modify substantially the page layout. Background colour can instead be applied to the paragraph styles and as long as indents are consistent, background areas merge seamlessly.
-
Example Heading is based on Heading, the ancestor of all headings.
By the way, never format paragraphs with Heading which is intended to set common attributes shared by all Heading n and other “outline” styles.
I attached it to outline level 2 (in Outline & List
tab), left-indented it 1.5cm (in Indents & Spacing
tab) and assigned it a light-gray background (in Area
tab).
This solves the look of the example heading and its inclusion and the TOC.
-
Example Text is based on Text Body.
I gave it the same indent and background as Example Heading.
This style will format example text.
You can, of course, set other attributes to your liking.
The remaining issue is numbering the examples. Although I could have used an existing number range such as Drawing or Illustration, I chose to create a dedicated one so that there is no conflict with captions in case you have figures, drawings, illustrations, …
The dedicated number range is created on first use:
- enter "Example " in an Example Heading paragraph
-
Insert
>Field
>More Fields
and go to Variables
tab
- in Type select Number range, in Name (below the Type list), enter “Example” and from Numbering by Chapter set Level to
1
- Insert
The other number insertions are simpler:
-
Insert
>Field
>More Fields
and go to Variables
tab
- in Type select Number range, in Select click on Example
- Insert
By requesting numbering by chapter, the number automatically resets when you add a heading at the designated level.
Your document belongs in the category “complex”. Consequently, follow a very strict procedure banning any direct formatting otherwise tuning your formatting will become a nightmare. I highly recommend you implement hierarchical styles (i.e. derived styles inherits their attributes from an ancestor and you only override the attributes needed to be different). So, when you modify attributes in the ancestor, this automatically cascade to descendant styles where the attribute is not overriden. This is very handy to assign a different font face in headings: you change it in Heading and all Heading n and Example Heading are updated.
Revised sample file (18.2 KB)