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
  • 1
    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):


  \edef\@currentlabel{\the\value{#1}}% Store current counter value in \@currentlabel
  \label{#2}% Store label
Some text

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



\usepackage{refcount,xr}% http://ctan.org/pkg/{refcount,xr}
  \setcounterref{#1}{#2}% Retrieve label value and store it in a counter
Counter: \getcounter{mycntr}{file1:first}\themycntr \par
Counter: \getcounter{mycntr}{file1:second}\themycntr

enter image description here

  • 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
  • 2
    @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
  • 1
    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
  • 1
    @egreg: Exactly... older days; learned since then... thanks. – Werner Jun 29 '16 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.