2

I'm using the refcount package to set some counters to the same values as counters from another .tex file. The labels from the other .tex file are imported with the zref-xr package.

This method fails for chapter-wise numbering. For example if a figure in the external .tex is numbered "3.2", the \setcounterref will add "3" to whatever counter and print ".2" into the document.

How can I extract only the rear part ("2") and add this to a counter?


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

enter image description here

2 Answers 2

2

Assuming that all reference numbers are divided by periods, you can define a new command for accessing the last item.

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

The expanded value of \getrefnumber{<label>} is split at periods and the last item is retrieved (with \seq_item:Nn \l_tmpa_tl { -1 }).

The output (you can check) will be

1.2
2

2
  • Oh, that works even better as there is not so much code-clutter inside the document. I saw that you moved filecontents before the document; is this better?
    – TeXisFun
    Jun 1, 2018 at 17:28
  • @NoMWENoAnswer I prefer it that way, generally.
    – egreg
    Jun 1, 2018 at 17:35
3

In case numbers always are of pattern ⟨chapter number⟩.⟨figure number⟩, don't use \setcounterref but apply \setcounter on a combination of \getrefnumber and removal of the dot and everything in front of it.

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

enter image description here

1
  • You are my hero!
    – TeXisFun
    Jun 1, 2018 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .