使用 zref 将计数器转移到另一个文件

使用 zref 将计数器转移到另一个文件

在我的上次在不同文件之间交叉引用总数的经验xr,将计数器转移到另一个文件时出现问题。不知何故,当我使用这些目的和refcountWerner 提供的包时出现错误,请参阅,这里

错误出现在\externaldocument[file1:]{myfile1}写入的行上myfile1/userpackages.texIncomplete \ifx; all text was ignored after line 27. \input{myfile1/userpackages}

我猜,原因可能是因为使用了\input命令,或者可能是因为与其他包发生了一些意外冲突。因此,我需要一种替代方法将计数器转移到另一个文件,如所示这里

问题是如何获取\zref计数器中mycntr生成的计数器值file2.tex

这是我的 MWE:

file1.tex(服务器):

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

file2.tex(客户):

\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}

带有突出显示部分文本的屏幕截图file2.pdf,这应该是正确的。

在此处输入图片描述

未指定译文: 沃纳的回答使用 MWE 运行良好!为了避免 tex 文件中出现计数错误:Missing number, treated as zero. ...ycntr}{\zref@extract{file1:LastPage}{abspage}}和程序包计算错误:\let is invalid at this point. ...ycntr}{\zref@extract{file1:LastPage}{abspage}}应按以下方式用\makeatletter和命令包装 Werner 的提示:\makeatother

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

答案1

\zref[<prop>]{<ref>}不可扩展。但是,\zref@extract{<ref>}{<prop>}是:

在此处输入图片描述

\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}

相关内容