8

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.

dustin
  • 18,617
  • 23
  • 99
  • 204
  • 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 00: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 01: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 01:16
  • 1
    Thanks for the edit though, didn't know how to do that. – Patrick Da Silva Aug 21 '13 at 01:17
  • This is what you are looking for I believe now that I know your intentions better. – dustin Aug 21 '13 at 01:18
  • @dustin : Are the values of my counters stored in the .aux file at the end of compilation? Because if it could store the counters values and I could read them up with a \value or \the-countername- command it would be great. – Patrick Da Silva Aug 21 '13 at 01:24
  • Did you see the link in my previous comment? Martin Scharrer address accessing references in multiple documents that aren't part of one main file by using the package xr. So if you label your theorem in file1, you can \ref{thoerem} in file2.tex. – dustin Aug 21 '13 at 01:26
  • @dustin : Yes, of course I did. I tried to work it out, and it seems to work with labels but not with counters. Is that possible? I am using counters via \newcounter, not \label. If I could assign a counter value to a label that would work too but I don't know how to do it. – Patrick Da Silva Aug 21 '13 at 01:45
  • @dustin : We're almost there. I can use xr to cross-reference labels, and it does exactly what I want it to do. The only problem is that the number I'm trying to transfer from a file to another is not from a \label and outputtable with a \ref ; it is a counter and is outputtable with a \value or something. – Patrick Da Silva Aug 21 '13 at 01:50
  • So either I learn out to put a counter value into a label, or this package has some way of transferring counter values as well as labels. – Patrick Da Silva Aug 21 '13 at 01:50
  • @PatrickDaSilva: That's possible via refcount. – Werner Aug 21 '13 at 01:59

1 Answers1

8

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}

enter image description here

Werner
  • 603,163
  • 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 02:02
  • Why do you need \section{A section}\label{---->file1<----:sec} this part in file1?? – Patrick Da Silva Aug 21 '13 at 02:11
  • @PatrickDaSilva: You don't need file1 in the reference. I just wanted to make it clear from the example that the reference is coming from file1.tex. However, xr also provides the means to supply a prefix for labels, as mentioned in Cross-referencing between different files, via \externaldocument[file1]{<file>}. – Werner Aug 21 '13 at 02:13
  • The command \setcounterref does the opposite of what I want ; since label values can go across files with the package xr, I wanted to assign a counter value to a label so that the counter value goes across the file. Your example shows how to carry a label value across files, that I can do. This is where I am stuck. I have a counter in file1 and I want its value to be carried over to file2. Do you understand my problem better? – Patrick Da Silva Aug 21 '13 at 02:15
  • In respond to your last comment ; yes, good. – Patrick Da Silva Aug 21 '13 at 02:16
  • I feel like I'm doing something wrong here. Am I being out of line by trying to carry that counter value across files? – Patrick Da Silva Aug 21 '13 at 02:19
  • Something like this, but I don't quite understand how to work it out : http://tug.org/pipermail/texhax/2006-July/006599.html – Patrick Da Silva Aug 21 '13 at 02:25
  • @PatrickDaSilva: As I understand it, the principle is the same. I've updated my answer to use physical counters, but it's still using the same packages to extract numbers from references; it just seem like I'm transferring counters. If this does not answer your question, please include a document structure in your original post that explains the situation better... – Werner Aug 21 '13 at 03:21
  • So your home-made \storecounter command is the one that turns my counter values into labels? It looks quite complicated to setup... am I wrong thinking this is a TeX-made command, and not a LaTeX one? I can only do LaTeX so it looks like chinese to me, but it seems to work exactly the way I want it. – Patrick Da Silva Aug 21 '13 at 23:59
  • Actually I like this solution because I use counters to number my environments but it is nice to use \label and \ref to refer to my environments. Great answer! – Patrick Da Silva Aug 22 '13 at 00:07
  • 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 01:25
  • Okay, thanks! I didn't know that. I know now what \makeatletter does but seeing that @ character at first made me think it was TeX because I never do that in LaTeX. – Patrick Da Silva Aug 22 '13 at 14:40
  • 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