8

When using the subfiles package, each subfile starts numbering sections with 1. How can I get consecutive numbering for the entire document?

user38559
  • 81
  • 2
  • 2
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Mar 07 '15 at 15:49
  • 1
    I think you may want to use either input or include instead of subfiles: http://tex.stackexchange.com/questions/246/when-should-i-use-input-vs-include – umarcor Mar 07 '15 at 23:13
  • We are still waiting for a MWE ;-) –  Mar 09 '15 at 22:32

2 Answers2

5

Using the refcount and xr (or xr-hyper if you are using hyperref) packages in the main file, you could automatically retrieve the correct chapter (or whatever counter you want) number from the main document.

main.tex:

\documentclass{book}

\usepackage{refcount} \usepackage{xr} \usepackage{subfiles}

\begin{document}

\chapter{title}

\chapter{title}

\subfile{document}

\end{document}

document.tex:

\documentclass[main]{subfiles}

\ifSubfilesClassLoaded{ \externaldocument[main-]{main} \setcounterref{chapter}{main-chap:bla} \addtocounter{chapter}{-1} }{}

\begin{document}

\chapter{bla} \label{chap:bla}

...

\end{document}

  • Could you elaborate how this works? I assume it somehow extracts the chapter counters from the single-file main build and injects them in the multi-file build per chapter? I am not sure where the main-chap:bla reference gets generated or comes from? It appears to be undefined for me. And why do we need to decrement the chapter counter? – Flow Feb 23 '23 at 12:48
  • @Flow I'm sorry, it seems I forgot to mention the xr package in answer. I now added a complete example including both the main and subfile. The counter needs to be decremented because the chapter in the subfile will increase it again, so it needs to start out as one less as the counter you want to see in the subfile. (alternatively you could get the counter from the previous chapter, but then you have to be careful in case you rearrange the order of the chapters, so this approach seemed safer.) – samcarter_is_at_topanswers.xyz Feb 23 '23 at 13:30
  • Thanks, I was no able to apply your solution and took the liberty to edit your answer a bit, in an attempt to improve it even further. I hope you do not mind. – Flow Feb 26 '23 at 10:08
0

This method is not fully advisable as it can break things.

You can specify what numbering you want to use within your subfile by using \setcounter{chapter}{*insert-number-here*}

For example, as you say, the default is for subfiles to always start with 1. If you want it to start the numbering at 2, then before \chapter{Your Chapter} write \setcounter{chapter}{1}.

For more info on this see this post - Chapter number starts at 2 not 1

Milo
  • 9,440
  • Generally, you shouldn't manually adjust the counters unless you need to. Otherwise, when you change things, your numbering will be all wrong. Since the original didn't include a MWE, we can't really figure out why the section numbering restarts at 1, which shouldn't happen by default. – Teepeemm Jun 17 '17 at 23:35
  • Yes I agree, it is unclear from the question what the problem really was. However, I think it was this. Say I am using subfiles, when I separately compile an individual subfile (a single chapter say), the default chapter numbering is 1. Obviously, when I go back to main file and compile all the subfiles together, the chapter numbering is in the correct order. But if I am just working with an individual subfile the numbering always starts at 1. Therefore, sometimes it is useful to have a way to force the subfile numbering to change to what you want. – Milo Mar 05 '18 at 16:15
  • 1
    There is likely to be a better solution to this problem somewhere, but so far I haven't found it! – Milo Mar 05 '18 at 16:16
  • I'm not terribly experienced with using subfiles, but my impression is that it's designed for when you want to tweak a small section. If you're wanting section and other numbering to stay correct while you work, you might be better off using \include and \includeonly. But without the MWE, who knows. – Teepeemm Mar 05 '18 at 19:56