Auto-numbering Sections within Chapters?

Hello, Scrivs,

Question about how to compile section numbers within a chapter.

For example:

Chap 1. This is Great
Section 1.1 This is Really Great
Section 1.2 This is Really, Really Great
Chap 2. Did We Mention…?
Section 2.1 Just How Great this is?

Chap N. Oh Yes…
Chap N.1 You’re Great Too
Chap N.2 Sortof

This structure is needed for a business book that I’m writing with the following Scrivener Binder-Manuscript (Draft) design:
Level 1 folders are “Parts”. Parts contain 1, or more…
Level 2 files/folders are “Chapters.” Chapters contain 1, or more…
Level 3 files are “Sections.”

So…in Compile/Formatting is there a way in “Title Settings” for a Section counter (e.g., <$n:sect>) to reference–yet not change–its parent Chapter counter (e.g., <$n:chap>)…AND start over at N.1 whenever a Chapter number does change?

Many thanks!

Actually, I would just recommend using the <$hn> counter for this in your prefix settings. That produces a format that is generally used in technical documentation where numbers represent hierarchy in a 4.23.11 fashion (which would be Chapter 4 Section 23 Sub-section 11).

Tried <$hn> before making original post.

The structure of the manuscript is incompatible with <$hn> because [a] the “Part” number is excluded from the Section number to avoid unnecessary noise (i.e., “Section 1.1” is clear, “Section 1.1.1” would be noisy and unclear) and [b] “Chapter” numbers cross Part boundaries (that is, Chapter numbers do not reset at a Part break).

It would be VERY helpful if there were a way in a Section header to reference the parent Chapter’s <$n> without causing an unwanted increment in the Chapter <$n> (perhaps along the lines of the way “Figures” can reference counters?).

Okay! You’re right there are definitely cases where it doesn’t work and a more complicated solution is necessary. It is indeed possible to reference the parent from a child and use it in a numbering stream. The best thing to do for this is the <$parentposition> token. That prints out the numeric position of the child’s parent in the outline. When used as an ID in a streamed counter, it can effectively refer to the parent’s number in a purely procedural fashion. Here is what you can do for a two-level system in the compile prefix settings:

Level 1: <$n:chapter:<$position>>

Level 2: <$n:chapter:<$parentposition>>.<$n:sec<$parentposition>:<$position>>

Okay, so <$position> prints the outline position of the item and so is unique to it. It is thus useful as an ID in a stream, or as used in the second example, as a stream name. This keeps the technique purely procedural and so useful as a general compile setting template. The level one token is self-explanatory. It increments a numeric chapter stream using the item’s position as its ID.

The level 2 bit contains two pieces of course. The first piece is identical to the level 1 piece when compiled, it just uses the parentposition token instead of the position token—resulting in the same number for a child, and thus recalling the same base underlying ID for that counter stream. The second component uses the parentposition as a stream. So each section within that chapter will use the same numerical stream, and its own position as an ID. When you get to the next chapter, those sections will be using a new parentposition value and thus a new counter stream. I put ‘sec’ in front of the section stream’s name so that you could use a number stream system for something else if desired, too.

So as you can see this technique is also useful for captions that use a 15.3 type format where 15 is the chapter and 3 is the third figure in that chapter.


First, thank you for your willingness to help…it is appreciated.

I have spent HOURS trying to figure out how to get Scrivener to handle what seems like a very simple structure…including many variations on the suggestions you’ve kindly offered (above).

Would you take a look at the attached screenshot? It depicts what I’m trying to do with numbered Sections within Chapters where the section ID re-starts within each chapter.

Thank you very much.

So you want the section number to increment regardless of a chapter change? In other words, having a chapter 4 section 52 as the first section in chapter 4? In that case, just remove the parent position bit from the stream ID and potentially dispense with a specific ID. So something like <$n:section> would do the trick for the second part. That would just count up through the entire project with no resets. Combine that with the first part of my example above to get the persistent chapter number in front of it.


Not sure I understand.

Would the approach in your comment immediately above reset the “Section” counter at the beginning of each “Chapter” counter?

In the screenshot provided, “Section” counters do not reset within Chapters…which is one of the numbering problems I’m trying to solve.

What’s sought is:

Chap 1. Chapter Title
Section 1.1 Section Title
Section 1.2 Another Section Title

Chap 2. Another Chapter Title
Section 2.1 A Reset ‘1’ Section Counter within an Incremented Chapter Counter
Section 2.2 Another Incremented Section Counter…without incrementing the Chapter Counter
Section 2.3 And so on…

Chap 3. A Third Chapter Title
Section 3.1 Another Reset ‘1’ Section Counter
Section 3.2 …
Section 3.3 …
Section 3.4 …

Chap 4.
Section 4.1
Section 4.2

and so on…

Chap N.
Section N.1
Section N.2
Section N.3
etc., etc.

Your comments are welcomed by this throughly confused writer; thanks!

Right, your typed out example is what I think my first example would address: the ability to have a section count that resets at each chapter, and a printed section number that includes the current chapter number. This should, as I have tested it anyway, result in a numbering scheme like what you demonstrated in the last message. I suppose where I am confused is in the screenshot you sent, which seems to depict sections numbers increment regardless of the chapter number.


Regrets, I tried for nearly 2 hours and cannot get your “streams” code example (above) to work.

The closest I got is the screenshot…which shows Section counters do not reset when a Chapter number changes (which is the problem…I need Section counters to reset when a Chapter counter changes).

Please note the screenshot caption reads “All Sections within Chapters should reset at ‘1’.” In the screenshot, the Section numbers do not reset…they continue to increment across Chapter boundaries.

With respect to your “streams” code example, is there more to it (and I’ve read the Manual many, many times trying to learn more)? Or, is it possible there’s a bug? Or…?

Take a look at this example. It just uses the previous mentioned stream tokens in the Formatting pane, for the Prefix on folder and file types. When I compile this as a test to either RTF or print, it numbers the chapters sequentially by chapters (Red Book, Black Book) and the sub-sections beneath them in their own reset number schemes, referencing the relevant chapter numbers from whence they came.
counters 2012-02-24 (107 KB)


Is there any way to modify the approach you have demonstrated to work when the nesting of documents is deeper? I am trying to number figures with the format Chapter.Figure_Number (1.1, 1.2 in chapter 1, etc). Your approach works successfully when the figure is in a subheading of the chapter, but once it is in a sub-subheading of course the parent is no longer the chapter but the subheading, and the first number resets. I guess what would be needed is a counter that points explicitly to the chapter rather than to the parent. Is that possible?

Yes, I understand what you mean and I cannot think of a good way to do this automatically. The key thing here is that the second piece of information, the counter stream, is static throughout the chapter. In this case I used the parent’s information to create a static bit of data. You could perhaps set up a new macro in Replacements for each chapter you work in. It makes the text inside the chapter less flexible (you’d need to update the captions if you move an image to another chapter), but still pretty efficient from a writing standpoint.

Hello guys, new Scrivener user here.
nitram, I’m having the same problem and I managed to solve it by using a custom meta-data called “parent-chapter” and setting it’s value for each sub-document.

Chapter 1

Chapter 2
- Section 2.1
- Section 2.1.2

In the section 2.1, there are two pictures, as folow:
Figure <$parentposition>.<$n:figure:space-shuttle>
=> Compiled as: “Figure 2.1”

Figure <$parentposition>.<$n:figure:space-shuttle-front>
=> Compiled as: “Figure 2.2”

Section 2.1.2 has two pictures as well and here is the trick.
Instead of using <$parentposition>, because it’s in a deeper level, I set the previously created “parent-chapter” meta-data with a value of “2” in this document inspector.

So the result is:
Figure <$custom:parent-chapter>.<$n:figure:apple>
=> Compiled as: “Figure 2.3”

Figure <$custom:parent-chapter>.<$n:figure:orange>
=> Compiled as: “Figure 2.4”

If I move the document in the Binder, all I have to do is update the meta-data value.

What do you guys think? Is this a good method?

@sitram: wow! :open_mouth:
this looks like it works like a charm.
however, people here still seem to be struck on the chapter-numbering itself.

How’d you get Scrivener to number the documents and sub-, and sub-sub-documents in a hierarchical fashion?!

Going all the way back to the top, unless you have a Part - Chapter - Sections format where the chapter number needs to be continuous throughout the book, then the <$hn> token should handle all of this for you. You really only have to get tricky with tags if you need a numbering order that is not strictly parent reset based. So a flat list of chapters in a book with a descending hierarchy of sections would work well with <$hn>.