
假设有人想要创建多个能够引用其他文档内部引用的 LaTeX 文档。有没有办法做到这一点,同时避免对其他文档的标签进行硬编码?
例如,假设一位教师有两套讲义:A.pdf 和 B.pdf,分别对应他们教授的两门课程“A”和“B”。每次教授课程 A 时,他们都会更新 A.pdf,可能会添加带有标签的新项目(例如,引理 1.2.3 可能会更改为引理 1.2.5)。然后他们应该能够转到 B.pdf,它可能在 A.pdf 中引用引理 1.2.3,重新编译 B.pdf,并将对引理 1.2.3 的原始引用改为引理 1.2.5。他们不必在 .tex 源代码中的任何地方实际写入“1.2.3”,然后在重新编译 B.pdf 之前将其更改为“1.2.5”。当您引用已发布的文档时,这种硬编码是可以的,但在这种情况下有没有一种自然的方法可以避免这种情况?
答案1
您可以使用xr
包来定义对外部文档的引用。因此,如果您有两个文档,A.tex
并且B.tex
在文档中,A
您可以使用
\externaldocument{B}
加载B
(\ref
和\pageref
) 中的引用。请注意,然后可以引用 document或 document\label{xxx}
中的引用。您可以包含多个调用。A
B
\externaldocument
以下是两个完整的 MWE:
文件A.tex
(\setcounter
仅用于有不同的标签)。
\documentclass{article}
\usepackage{xr}
\newtheorem{lemma}{Lemma}
\setcounter{lemma}{100}
\externaldocument{B}
\begin{document}
\begin{lemma}
\label{a:lemma:first}
this is a lemma
\end{lemma}
This is a reference to the lemma in this document \ref{a:lemma:first}
This is a reference to the lemma in the related document \ref{b:lemma:first}
\end{document}
同样,文件B.tex
\documentclass{article}
\usepackage{xr}
\newtheorem{lemma}{Lemma}
\externaldocument{A}
\begin{document}
\begin{lemma}
\label{b:lemma:first}
this is a lemma
\end{lemma}
This is a reference to the lemma in this document \ref{b:lemma:first}
This is a reference to the lemma in the related document \ref{a:lemma:first}
\end{document}
屈服