# Compile section of large document while maintaining numbering

I have a large document, and to minimize compilation time, I would like to recompile only the last section of it as it is edited. My trouble with standard solutions is that they destroy the numbering: I would like theorems, definitions, etc. in the final section to be numbered as they would in the entire document, rather than starting at 1.

Is there a standard solution for this? Ideally, I'm hoping for an environment that works like a comment except that it adjusts counters. For example, it would have the following behavior:

\begin{specialenvironment}

\begin{theorem} This theorem will not show up at all. \end{theorem}

\end{specialenvironment}

\begin{theorem} This will show up as Theorem 2. \end{theorem}

The \include and \includeonly macros are for this, where each \include is the name of a file that makes up a portion of the document. Briefly

% MWE.tex
\documentclass{article}
\includeonly{lastsection}
\begin{document}
\include{mostsections}
\include{lastsection}
\end{document}


where mostsections.tex is a file containing all but the last section and lastsection.tex is a file containing the last section. If you compile MWE without the \includeonly you will get the complete document. If you then compile it with the \includeonly you will just get the last section typeset with all numbering correct. Edit lastsection.tex and recompile you will get a revised last section with all numbering correct. If there is an \includeonly LaTeX will only process the listed files, ignoring everything else, but retaining the overall numbering system.

• Thanks! This worked great. Small detail for posterity: it seems you need to compile the entire document first (without the includeonly line), and then add in the includeonly for a secondary compile, and it works from then on.
– GMB
Feb 1 at 19:04