# Carrying counters over to another file using zref

During my last experiences with cross-referencing on total counts between different files, a problem of carrying counters over to another file appears. Somehow there is an error when I use for these purposes xr and refcount packages given by Werner, see, please, here

The error appears on the line \externaldocument[file1:]{myfile1} which is written in myfile1/userpackages.tex: Incomplete \ifx; all text was ignored after line 27. \input{myfile1/userpackages}.

I guess, that the reason can be because of using \input command or perhaps because of some unexpected conflicts with other packages. So, I need an alternative way of carrying counters over to another file, as presented here.

The question is how to get a counter value generated by \zref in a counter mycntr in file2.tex?

Here is my MWE:

file1.tex (server):

\documentclass{article}
\usepackage{zref-abspage, zref-lastpage}
\makeatletter
\zref@newprop{msection}[0]{\thesection}
\zref@newprop{mequation}[0]{\theequation}
\makeatother
\newcounter{mycntr}
\begin{document}
\section{my section1}
$$\label{eq1}$$
\newpage
\section{my section2}
Some text
\newpage
Some text
\end{document}


file2.tex (client):

\documentclass{article}
\usepackage{refcount,zref-xr,zref-user}
\newcommand*{\getcounter}[3]{%
\setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}
\zexternaldocument[file1:]{file1}
\newcounter{mycntr}
\begin{document}
We can use zref counters directly: file1.tex has \zref[abspage]{file1:LastPage} pages and
\zref[mequation]{file1:LastPage} equations and
\zref[msection]{file1:LastPage} sections.

%get function takes wrong value from \zref
My counter reflects pages value: \getcounter{mycntr}{\zref[abs]{file1:LastPage}}\themycntr \par
My counter reflects equations value: \getcounter{mycntr}{\zref[mequation]{file1:LastPage}}\themycntr \par
My counter reflects sections value: \getcounter{mycntr}{\zref[mequation]{file1:LastPage}}\themycntr
\end{document}


A screenshot of file2.pdf with highlighted part of text, which should be correct.

UPT: Werner's answer is working fine with MWE! To avoid two errors in tex-files with counting: Missing number, treated as zero. ...ycntr}{\zref@extract{file1:LastPage}{abspage}} and package calc error: \let is invalid at this point. ...ycntr}{\zref@extract{file1:LastPage}{abspage}} one should wrap Werner's hint by \makeatletter and \makeatother commands in the following way:

    \makeatletter
\setcounter{mycntr}{\zref@extract{file1:LastPage}{abspage}}
\makeatother


\zref[<prop>]{<ref>} is not expandable. However, \zref@extract{<ref>}{<prop>} is:

\documentclass{article}

\usepackage{zref-xr,zref-user}

\zexternaldocument[file1:]{file1}
\newcounter{mycntr}

\begin{document}

We can use \verb|zref| counters directly:

\verb|file1.tex| has \zref[abspage]{file1:LastPage} pages \par
\verb|file1.tex| has \zref[mequation]{file1:LastPage} equations \par
\verb|file1.tex| has \zref[msection]{file1:LastPage} sections

\makeatletter
My counter reflects pages value: \setcounter{mycntr}{\zref@extract{file1:LastPage}{abspage}}\themycntr \par
My counter reflects equations value: \setcounter{mycntr}{\zref@extract{file1:LastPage}{mequation}}\themycntr \par
My counter reflects sections value: \setcounter{mycntr}{\zref@extract{file1:LastPage}{msection}}\themycntr
\makeatother

\end{document}

• @ Werner, thank you for the answer! It works with MWE. To avoid two errors in tex-files with counting: Missing number, treated as zero. ...ycntr}{\zref@extract{file1:LastPage}{abspage}} and package calc error: \let is invalid at this point. ...ycntr}{\zref@extract{file1:LastPage}{abspage}} one should wrap your hint by \makeatletter and \makeatother commands. The question is updated. – Vladimir Parkhomenko Nov 5 '16 at 9:24