我使用xr-hyper
包,因为它可以创建指向交叉引用的外部文档的超链接。我使用“xr-hyper”包,因为我想制作多卷书,可以收集我在不同工程项目中的笔记。
我把问题简化为仅包含两个文件的一个框架。
- 谷物0050a.tex
- 谷物0050b.tex
我在文件中用 \captionof 命令包含图片并引用 \label{fig:048}(见下面的 MNWE)谷物0050a.tex。grain0050b.tex 文件中使用了交叉引用。但是它不起作用。我得出的结论是,存在对包的干扰siunitx
。在我看来,代码 (T_1 = \SI{300}{\kelvin}) 被用作相对路径(下面还包括了grain0050a.aux 文件)。不幸的是,我在那里没有看到任何奇怪的东西。我不知道下一步该往哪儿挖。
图1:编译文件grain0050b.tex。注意 - 必须先编译grain0050a.tex 才能创建辅助文件。
MWE:grain0050a.tex(编译无错误)
\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}
我也尝试使用@UlrikeFischer 破解\protect
脆弱命令,但没有成功。
{ \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}
现在我可以看到 AUX 文件已被修改,并且关键字 \protect 显示在那里。
\newlabel{fig:048}{{1}{1}{Maxwell's velocity distribution \(T_1 = \protect \SI {300}{\kelvin }\). \relax }{figure.caption.1}{}}
但是当我尝试重新编译grain0050b.tex(当然grain0050a.tex首先被重新编译)时pdflatex抱怨:
! 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(无法编译 - 参见错误日志截图 - 图 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}
编译方案
- pdflatex Grain0050a.tex
- pdflatex Grain0050b.tex
- pdflatex Grain0050a.tex
Grain0050.tex的Aux文件内容为:
\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}
答案1
根据 Ulrike Fischer 的评论,保护措施应如下所示:
\captionof{figure}{Maxwell's velocity distribution \(T_1 = \protect\SI{300}{\protect\kelvin}\)
必须双重保护。