# Do a setcounter only for an included file

I have currently the following situation: I am including a Latex-File in my main file. This file already has several \section{}-parts. Now I want to let their counting begin at 1 again, but without messing up the global counting in my master file. I tried the following:

\subsection{First}
\setcounter{section}{0}
\input{first_file}
\subsection{Second}
\setcounter{section}{0}
\input{second_file}


That works for resetting the counter, but that also messes my global counting up. Is there a way to reset the counter without messing up the global document? I don't want to change the included files, and preferably I want to have a portable solution.
Edit: Explanation for messing up: What I get is:

1
2
3
\begin{input}
1
2
\end{input}
3
4
5


What I want:

1
2
3
\begin{input}
1
2
\end{input}
4
5
6

• How is it messing up your global document? Wrong hyperlinks? – user31729 Apr 24 '15 at 9:45
• @ChristianHupfer: Question edited, I hope that explains it – arc_lupus Apr 24 '15 at 9:49
• \input is no environment, so \begin{input}...\end{input} is effectively wrong – user31729 Apr 24 '15 at 9:54
• @ChristianHupfer: I know, it was just an explanation. It should mark the beginning and the end of the included text. – arc_lupus Apr 24 '15 at 9:55

Define an auxiliary counter and use it for temporary storage of the section number.

Then use \inputreset{file} for inputting a file where you want to reset the equation number.

However, you'll have big problems as soon as you try loading hyperref.

\newcounter{storedsection}

\newcommand{\inputreset}[1]{%
\setcounter{storedsection}{\value{section}}%
\setcounter{section}{0}%
\input{#1}%
\setcounter{section}{\value{storedsection}}%
}

• hypertexnames=false will generate unique names not based on counters, so the resetting of counter values should not have no effect on the links and bookmarks (at least for small MWE -- I could not test for any eventuality) – user31729 Apr 24 '15 at 15:03

This uses a temporary counter to store the section counter value before the first \input is used. In addition, the \input command is changed to \refstepcounter a input counter, which then resets the section counter in turn.

Edit Automated storing of the section counter value for first \input.

Some drawback: Each time if \input is used the section counter is reset. If this is a problem, one can remove the resetting after some threshold.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xpatch}

\usepackage[hypertexnames=false]{hyperref}
\newcounter{inputfilecounter}

\newcounter{storesection}

\xpretocmd{\input}{%
\ifnumequal{\value{inputfilecounter}}{0}{%
\setcounter{storesection}{\value{section}}%
}{}%
\refstepcounter{inputfilecounter}
}{}{}

\newcommand{\RestoreSectionCounter}{%
\setcounter{section}{\value{storesection}}%
}

\makeatletter
\makeatother

\begin{document}

\tableofcontents

\section{Regular}
\clearpage

\input{firstfile}
\clearpage

\input{secondfile}

\RestoreSectionCounter
\clearpage
\section{Continued regular sections}
\end{document}