# Carrying counters over to another file

I have two files, say file1.tex and file2.tex. file1.tex is the first of a series of files, and it begins with counters (say chapter 1 for instance, although my counters are not used for chapters). Then at the end of file1, the counter for chapters is at say 7 ; I want to carry the value of the counter to file2.tex so that I can use a counter which will have the value 7 in file2.tex. In this way I could possibly carry on the counter in file2.tex to a counter in some file3.tex and so on.

I assume I would need to compile file1.tex first, and then file2.tex, and then file3.tex... of course.

Is that possible? And if so, how? I couldn't think of anything.

• You can create a main file and \include or \input the files depending on your needs. The main file will keep track of the counter. Here is a thread on when to use either input or include: Include vs Input – dustin Aug 21 '13 at 0:54
• @dustin : I'm not exactly sure if this will work ; I don't want to put in file2 some code sitting inside file1... I want to put in file2 the value of some counter. I'm going to read up on \input, but I don't see how that would work... \input and \include seem to put one file into another or something, which I don't want. – Patrick Da Silva Aug 21 '13 at 1:08
• @dustin : It is not a book. I write many papers, and inside a paper, I want to refer to a theorem in another paper. Both may get modified over time, so I want something that can survive modifications. Usually it's notes or something. Anyway, I don't think \input or \include answers my question. – Patrick Da Silva Aug 21 '13 at 1:16
• Thanks for the edit though, didn't know how to do that. – Patrick Da Silva Aug 21 '13 at 1:17
• This is what you are looking for I believe now that I know your intentions better. – dustin Aug 21 '13 at 1:18

You can use a combination of xr to retrieve labels from other documents and refcount to assign references (of labels) to counters (as reference, see Cross-referencing between different files):

### file1.tex:

\documentclass{article}
\makeatletter
\newcommand*{\storecounter}[2]{%
\edef\@currentlabel{\the\value{#1}}% Store current counter value in \@currentlabel
\label{#2}% Store label
}
\makeatother
\newcounter{mycntr}
\begin{document}
Some text
\setcounter{mycntr}{12}\storecounter{mycntr}{first}
\setcounter{mycntr}{99}\storecounter{mycntr}{second}
\end{document}


This creates file1.aux which has the crucial bit of information regarding the label(s) and its associated value:

\newlabel{first}{{12}{1}}
\newlabel{second}{{99}{1}}


### file2.tex:

\documentclass{article}
\usepackage{refcount,xr}% http://ctan.org/pkg/{refcount,xr}
\newcommand*{\getcounter}[2]{%
\setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}
\externaldocument[file1:]{file1}
\newcounter{mycntr}
\begin{document}
Counter: \getcounter{mycntr}{file1:first}\themycntr \par
Counter: \getcounter{mycntr}{file1:second}\themycntr
\end{document}


• I am looking at refcount to better understand your answer. Thanks for taking the time, I'll come back to you in a few minutes! – Patrick Da Silva Aug 21 '13 at 2:02
• Why do you need \section{A section}\label{---->file1<----:sec} this part in file1?? – Patrick Da Silva Aug 21 '13 at 2:11
• @PatrickDaSilva: No, this is a LaTeX approach. If you search for the definition of \refstepcounter in latex.ltx you'll "see" how it stores the current counter value in \@currentlabel, which is then written to the .aux when you issue \label (defined just above \refstepcounter). So I'm just using an alternative tool to do the same thing you're after. – Werner Aug 22 '13 at 1:25
• Using \csname the#1\endcsname is wrong. It should be \the\value{#1}. If the counter's representation is not in arabic numbers, you'll get into big troubles with \setcounterref. – egreg Jun 29 '16 at 20:18
• @egreg: Exactly... older days; learned since then... thanks. – Werner Jun 29 '16 at 20:23