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
- Main file
- 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
\andrea@test@countand\@ifundefinedyou'd have to surround the code with\makeatletterand\makeatotherwhich 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}}}\makeatotherin your preamble. Then each of you can use\MyNewCounterin 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 07:52\input(file1.tex)and the content of file1 is what i wrote after the comment – Finn Mar 27 '18 at 08:12\newcounter{FinnLines}and he uses something like\newcounter{OtherGuyLines}) problem solved. – Skillmon Mar 27 '18 at 08:30