# How do I keep section numbering isolated when including standalone files?

Suppose I have a file test2.tex which includes the contents of another file, test1.tex.

Both of these files are standalone documents (ie. I want to be able to compile test.tex on its own).

test1.tex has the following content:

\documentclass{article}
\begin{document}
\section{Section X}
\end{document}


When compiled, it produces

1 Section X

test2.tex has the following content:

\documentclass{article}
\usepackage{standalone}

\begin{document}
\section{Section 1}
\input{test1}
\section{Section 2}
\end{document}


When compiled, it produces:

1 Section 1

2 Section X

3 Section 2

But what I want is:

1 Section 1

1 Section X

2 Section 2

How do I confine the included file to its own "environment" (I don't know what this is called in latex - I'm just learning). Just to reiterate, I want to make sure that test1.tex remains a compilable file in its own right, and I'd prefer not to modify it if possible.

• Is there any feedback on this? – user31729 May 6 '16 at 20:38
• @ChristianHupfer I am away from my desk until Monday but I'll be sure to respond as soon as possible! – quant May 6 '16 at 22:34

As far as I know standalone does not preserve counter values, it just glues the documents together (with some precautions of course due to packages,\begin{document}...\end{document} etc.)

A special environment would not help here, since LaTeX counters are not group safe.

The package xassoccnt provides the \BackupCounterValues{section,...} command,that stores the values of the counters given in the list and resets the counters. Afterwards use \RestoreAllCounterValues to restore the values.

(I am the author of xassoccnt and I want to provide 'backup' lists, i.e. predefined list of counters, that can be handed over to the \BackupCounterValues macro. In the current version, this is not possible yet)

test1.tex

\documentclass{article}
\begin{document}
\section{Section X}
\end{document}


Driver file

\documentclass{article}
\usepackage{standalone}
\usepackage{xassoccnt}

\begin{document}
\section{Section 1}
\BackupCounterValues{section,figure,table} % and perhaps other counters
\input{test1}
\RestoreAllCounterValues
\section{Section 2}
\end{document}