[Tex/LaTex] Cross-referencing in multiple chapters

cross-referencing

I have a document organized into several chapters. In each chapter, theorems are numbered by section (1.1, 1.2, … even in chapter 5). I often would like to refer to theorems in different chapters. However, if I am in chapter 6 and refer to theorem 1.3 in chapter 1, it is rather ambiguous, and the reader might assume that "theorem 1.3" meant the current chapter. Is there an automatic way of configuring things so that the cross-referencing will say "theorem 1.3" only if it is really the current chapter, but "theorem I.1.3" (or something like that) if the cross-referencing spans chapters?

Best Answer

You can add a conditional in the reference number which checks if the chapter number of the referenced label is identical to the current chapter number and then suppress the chapter number in the output.

Here a way to do it. I used ntheorem and loaded cleveref because you said you are using it. It might need some minor adjustments to work in your document, but you get the idea.

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