# Creating the same counter in multiple files

i am trying to create a counter (or better multiple independent counters) for a project with multiple people working on it over a long period of time. The tex files are structured as

1. Main file
2. multiple Subfiles that may or may not are included and may or may not needs a counter

For the example we can assume there are 2 files and both are included and need a counter.

\documentclass{article}

%ideally the main should not be altered, but if has to be done

\begin{document}

%main file include following text from file 1
Lorem
\newcounter{mycounter}
\setcounter{mycounter}{1}
\themycounter\stepcounter{mycounter}
Ipsum

%main file include following text from file 2
Dolor
\newcounter{mycounter}
\setcounter{mycounter}{1}
\themycounter\stepcounter{mycounter}
Sit
\themycounter\stepcounter{mycounter}
Amet

\end{document}


This will create an error as the second \newcounter{mycounter} tries to create what already exists. What i tried so far:

• Putting the counter in the main-file, which works but is for now the least favorable solution
• Deleting the counter at the end of the file, but didnt find code to delete counters.
• Tried to adapt the solution from this question but failed misearbly as i dont understand how the @works

\newcommand\andrea@test@count[1]{%
\@ifundefined{c@#1}
{% the counter doesn't exist
\newcounter{#1}\setcounter{#1}{1}%
}
{% the counter exists
\stepcounter{#1}%
}%
}


Does anyone has an idea on how to create the counter in each file? An additional but small questions is there an easier way to increase and use a counter than \themycounter\stepcounter{mycounter}?

Thank you

• For \andrea@test@count and \@ifundefined you'd have to surround the code with \makeatletter and \makeatother which changes the category code of the @ symbol to 11 (letters) and back to 12 (others). Best you put something like \makeatletter\newcommand\MyNewCounter[1]{\@ifundefined{c@#1}{\newcounter{#1}\setcounter{#1}{1}}{\stepcounter{#1}}}\makeatother in your preamble. Then each of you can use \MyNewCounter in the files to create as many counters as the used TeX engine allows you without worrying to define one two times. – Skillmon Mar 27 '18 at 7:52
• – Skillmon Mar 27 '18 at 7:53
• What shall the counter count here actually? Why do you need the same counter name again? Counters should be defined in the preamble of a document ideally, not somewhere in between, if possible. The title is incomplete, as well – user31729 Mar 27 '18 at 7:55
• it is very hard to understand your question as you refer to including files but your example just consists of a single document, – David Carlisle Mar 27 '18 at 8:05
• Thank you @Skillmon! The counter is needed to count some lines in a file. if it is a different name each time there is would be no issue, but it cant be guaranteed that there is a unique name each time. I felt like it would be easier to understand it this way than to write 3 different files. In the main file there is an \input(file1.tex) and the content of file1 is what i wrote after the comment – Finn Mar 27 '18 at 8:12

Something like this?

This uses a modified \include - macro, prepending it with a test whether the counter is defined at all and defines it accordingly. At the end, the counter macro \c@mycounter is 'undefined'.

The prepending could be done with \g@addto@macro, but there is no easy way to put something at the end of the macro other than wrapping it in a macro, which is basically done here.

The macro \showstepcounter is meant for showing the counter value and stepping it afterwards.

Code for fileone.tex and filetwo.tex is equal

fileone.tex

Lorem

\showstepcounter{mycounter}

Ipsum


Main document:

The code for the modified \include and \showstepcounter should be placed in separated .sty file to make it available for all users that need this particular structure.

\documentclass{article}

\makeatletter
\let\l@tex@@include\include

\renewcommand{\include}[1]{%
\@ifundefined{c@mycounter}{%
\newcounter{mycounter}%
\setcounter{mycounter}{1}%
}{%
\setcounter{mycounter}{1}%
}
\l@tex@@include{#1}%
\let\c@mycounter\relax
}

\makeatother

\newcommand{\showstepcounter}[1]{%
\csname the#1\endcsname\stepcounter{#1}%
}

\begin{document}

\include{fileone}

\include{filetwo}

\end{document}

• Best regards and my compliments.- +1. – Sebastiano Mar 27 '18 at 8:18
• Great! Thank you for also adding the showstepcounter, which i find is a good way to make the code more readable – Finn Mar 27 '18 at 8:22
• @Finn: I will post another version later one, which will save the counter value before \include and then restores the value back at the end. That's perhaps better than redefining counters all the while – user31729 Mar 27 '18 at 8:25
• Sorry, I upvoted it earlier. Done! Thank you! – Finn Mar 27 '18 at 12:59