0

I'm using xr-hyper package, because it can create hyperlinks to the external documents that are cross-referenced. I am using 'xr-hyper' package, beacuse I would like to produce multi-volume book which can collect my notes from different engineering projects.

I slashed the problem to a skeleton that consists of only two files.

  • grain0050a.tex
  • grain0050b.tex

I'm include figure with \captionof command and reference \label{fig:048} (see MNWE bellow) in file grain0050a.tex. Cross-reference is used in file grain0050b.tex. But it doesn't work. I come to the conclusion that, there is interference with package siunitx. It seems to me that code (T_1 = \SI{300}{\kelvin}) is used as a relative path (also grain0050a.aux file is included below). Unfortunately, I don't see anything strange there. I have no idea where to dig next.

enter image description here

Figure 1: compilation of the file grain0050b.tex. Note - grain0050a.tex have to be compiled first to create aux file.

MWE: grain0050a.tex (compiled without error)

\documentclass{article} 
\usepackage{xr-hyper} 
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{caption}

\usepackage{hyperref} 

\externaldocument[B-]{grain0050b}[grain0050b.pdf]% <- full or relative path 

\begin{document}
    This is a test for math.
    \begin{equation}
        E=mc^2 \label{eq:1}
    \end{equation}
    This is a second test for math.
    \begin{equation}
        r = \sqrt{x^2 + y^2} \label{eq:2}
    \end{equation}
    In document B Eq.~~(\ref{B-eq:x}) 

  { \centering
    \captionsetup{type=figure}
    \includegraphics[width=\textwidth]{example-image-a}
    \captionof{figure}{Maxwell's velocity distribution \(T_1 = \SI{300}{\kelvin}\).
    \label{fig:048}}
  \par}
\end{document}

I also try @UlrikeFischer hack \protect for fragile commands, but without success.

  { \centering
    \captionsetup{type=figure}
    \includegraphics[width=\textwidth]{example-image-a}
    \captionof{figure}{Maxwell's velocity distribution \(T_1 = \protect\SI{300}{\kelvin}\).
    \label{fig:048}}
  \par}

Now I can see that AUX file is modified and key word \protect is shown there.

\newlabel{fig:048}{{1}{1}{Maxwell's velocity distribution \(T_1 = \protect \SI {300}{\kelvin }\). \relax }{figure.caption.1}{}}

But when I try to recompile grain0050b.tex (ofcourse grain0050a.tex was recompiled first) the pdflatex complains:

! Undefined control sequence.
<argument> ... \(T_1 = \protect \SI {300}{\kelvin
                                                  }\). \relax
l.5 ...naldocument[A-]{grain0050a}[grain0050a.pdf]
                                                  % <- full or relative path
?

MNWE: grain0050b.tex (can't be compiled - see error log screenshot - figure 1)

\documentclass{article}
\usepackage{xr-hyper}
\usepackage{hyperref}

\externaldocument[A-]{grain0050a}[grain0050a.pdf]% <- full or relative path

\begin{document}
As was shown in Eq.~(\ref{A-eq:1}) is it
  ... or in Eq.~(\ref{A-eq:2}) is ...
  \begin{equation}
    \mathrm{e}^{i\pi}-1=0 \label{eq:x}
  \end{equation}

  Maxwell's velocity distribution is shown in \ref{A-fig:048}
\end{document}

Compilation scheme

  1. pdflatex grain0050a.tex
  2. pdflatex grain0050b.tex
  3. pdflatex grain0050a.tex

Aux file content of the grain0050.tex is:

\relax 
\providecommand\hyper@newdestlabel[2]{}
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
\global\let\oldcontentsline\contentsline
\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}}
\global\let\oldnewlabel\newlabel
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
\AtEndDocument{\ifx\hyper@anchor\@undefined
\let\contentsline\oldcontentsline
\let\newlabel\oldnewlabel
\fi}
\fi}
\global\let\hyper@last\relax 
\gdef\HyperFirstAtBeginDocument#1{#1}
\providecommand\HyField@AuxAddToFields[1]{}
\providecommand\HyField@AuxAddToCoFields[2]{}
\newlabel{eq:1}{{1}{1}{}{equation.0.1}{}}
\newlabel{eq:2}{{2}{1}{}{equation.0.2}{}}
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Maxwell's velocity distribution \(T_1 = \SI {300}{\kelvin }\). \relax }}{1}{figure.caption.1}\protected@file@percent }
\providecommand*\caption@xref[2]{\@setref\relax\@undefined{#1}}
\newlabel{fig:048}{{1}{1}{Maxwell's velocity distribution \(T_1 = \SI {300}{\kelvin }\). \relax }{figure.caption.1}{}}
\gdef \@abspage@last{1}{}}
    \gdef \@abspage@last{1}
7
  • 1
    You can use \captionof{figure}{Maxwell's velocity distribution \(T_1 = \protect\SI{300}{\protect\kelvin}\). I'm not sure if there is a better solution, nameref is a bit of a problem here. Commented Apr 12, 2021 at 8:19
  • @UlrikeFischer: I try it, but it doesn't help. By using "\protect", nothing is changed in aux file.
    – JardaFait
    Commented Apr 12, 2021 at 9:41
  • I tried and it worked, but you have naturally to recompile the a-file. Commented Apr 12, 2021 at 10:09
  • It is strange. 1. I also did the Miktex update. The packages siuintx, mathtools, biblatex-ieee and latexindent were updated. 2. I did the deletion of all generated files. 3. pdflatex grain0050a.tex. 4. Check the aux file: "\newlabel{fig:048}{{1}{1}{Maxwell's velocity distribution (T_1 = \protect \SI {300}{\kelvin }). \relax }{figure.caption.1}{}}". It seems to be correct now. 5. pdflatex grain0050b.tex. 6. compilation error: "! Undefined control sequence... (T_1 = \protect \SI {300}{\kelvin ... <- full or rel"
    – JardaFait
    Commented Apr 12, 2021 at 11:18
  • 1
    you added only one \protect, but I used two, \kelvin needs protection too. Commented Apr 12, 2021 at 12:13

1 Answer 1

0

According to the Ulrike Fischer's comment, the protection should looks like following:

\captionof{figure}{Maxwell's velocity distribution \(T_1 = \protect\SI{300}{\protect\kelvin}\)

It must be protected twice.

You must log in to answer this question.

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