我正在使用该refcount
包将一些计数器设置为与另一个文件中的计数器相同的值.tex
。另一个文件中的标签.tex
随该包一起导入zref-xr
。
此方法不适用于按章节编号。例如,如果外部的某个图的.tex
编号为“3.2”,则会\setcounterref
将“3”添加到任何计数器,并将“.2”打印到文档中。
我怎样才能仅提取后部(“2”)并将其添加到计数器中?
\documentclass{book}
\usepackage{filecontents}
\begin{filecontents*}{external.aux}
\newlabel{fig:external}{{3.2}{6}{external figure}{figure.3.2}{}}
\end{filecontents*}
\usepackage{refcount}
\setrefcountdefault{1}
\usepackage{zref-xr}
\zxrsetup{toltxlabel}
\zexternaldocument*[external-]{external}
\setcounter{chapter}{1}
\usepackage{hyperref}
\begin{document}
\setcounterref{figure}{external-fig:external}
\thefigure
\end{document}
答案1
假设所有参考编号都用句点分隔,您可以定义一个新命令来访问最后一项。
\begin{filecontents*}{external.aux}
\newlabel{fig:external}{{3.2}{6}{external figure}{figure.3.2}{}}
\newlabel{thm:x}{{2}{8}{Theorem}{theorem.2}{}}
\end{filecontents*}
\documentclass{book}
\usepackage{xparse}
\usepackage{refcount}
\usepackage{zref-xr}
\usepackage{hyperref}
\setrefcountdefault{1}
\zxrsetup{toltxlabel}
\zexternaldocument*[external-]{external}
\setcounter{chapter}{1}
\newtheorem{theorem}{Theorem}
\ExplSyntaxOn
\NewDocumentCommand{\xsetcounterref}{mm}
{% #1 = counter, #2 = label
\seq_set_split:Nnx \l_tmpa_seq { . } { \getrefnumber{#2} }
\setcounter{#1}{\seq_item:Nn \l_tmpa_seq { -1 }}
}
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\ExplSyntaxOff
\begin{document}
\xsetcounterref{figure}{external-fig:external}
\thefigure
\xsetcounterref{theorem}{external-thm:x}
\thetheorem
\end{document}
的扩展值\getrefnumber{<label>}
按句点分割,并检索最后一项(用\seq_item:Nn \l_tmpa_tl { -1 }
)。
输出(你可以检查)将是
1.2
2
答案2
如果数字总是有规律⟨章节编号⟩.⟨图号⟩,不要使用\setcounterref
,而是应用\setcounter
组合\getrefnumber
并删除点以及它前面的所有内容。
\documentclass{book}
\usepackage{filecontents}
\begin{filecontents*}{external.aux}
\newlabel{fig:external}{{3.2}{6}{external figure}{figure.3.2}{}}
\end{filecontents*}
\usepackage{refcount}[2010/12/01 v3.2] % issue warning if refcount
% older than version 3.2 is
% loaded.
\setrefcountdefault{1}
\usepackage{zref-xr}
\zxrsetup{toltxlabel}
\zexternaldocument*[external-]{external}
\setcounter{chapter}{1}
\usepackage{hyperref}
\newcommand\gobbletodot{}\long\def\gobbletodot#1.{}%
\begin{document}
% refcount manual, section 1.2 Expandable commands, states:
% These commands that can be used in expandible contexts
% ...
% Since version 3.2 the expandable macros described before
% in this section are expandable in exact two expansion steps.
%
\setcounter{figure}{\expandafter\expandafter\expandafter\gobbletodot\getrefnumber{external-fig:external}}
\thefigure
\end{document}