多个章节中的交叉引用

多个章节中的交叉引用

我有一份分为几章的文档。在每一章中,定理都按节编号(1.1、1.2,甚至第 5 章)。我经常想引用不同章节中的定理。但是,如果我在第 6 章中引用第 1 章中的定理 1.3,则会产生歧义,读者可能会认为“定理 1.3”是指当前章节。是否有一种自动配置方法,以便交叉引用仅在确实是当前章节时才显示“定理 1.3”,而如果交叉引用跨越章节,则显示“定理 I.1.3”(或类似内容)?

答案1

您可以在参考编号中添加一个条件,检查引用标签的章节号是否与当前章节号相同,然后在输出中隐藏该章节号。

这是一种方法。我使用ntheorem并加载是cleveref因为你说你正在使用它。它可能需要一些小的调整才能在你的文档中工作,但你明白我的意思了。

\documentclass{book}
\usepackage{ntheorem}
\usepackage{cleveref}

% From your post I assume you have settings like this:
\renewcommand*\thechapter{\Roman{chapter}}
\newtheorem{mytheo}{MyTheo}[chapter]

% Redefine the format of the theorem number:
\renewcommand*\themytheo{%
   \protect\maybechapter{\arabic{chapter}}\arabic{section}.\arabic{mytheo}%
}

% May print the chapter number
\newcommand*\maybechapter[1]{%
   % Tests if current chapter is equal to the chapter number of label)
   \ifnum\value{chapter}=0#1\relax
     % Print nothing if so
   \else
     % Set 'chapter' locally (=> no \setcounter) to the label chapter and
     % print it in the usual format followed by a dot
     {\value{chapter}=#1\relax\thechapter.}%
   \fi
}

% Test document:
\begin{document}
\chapter{One}
Here \ref{theo:11}  % prints "1.1"
and there \ref{theo:21}. % print "II.1.1"

\section{S1}
\begin{mytheo}
    Theo1-1\label{theo:11}
\end{mytheo}

\section{S2}
\begin{mytheo}
    Theo1-2\label{theo:12}
\end{mytheo}

\chapter{Two}
There \ref{theo:11}  % prints "I.1.1"
and here \ref{theo:21}. % print "1.1"

\section{S1}
\begin{mytheo}
    Theo2-1\label{theo:21}
\end{mytheo}

\end{document}

相关内容