Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have separated my thesis in separated latex files representing the chapters of my text. When I refer to theorems in previous chapters I only get the exact number of the theorem, hence one does not know to which chapter it belongs. Is it possible to get the chapter number in roman style before the theorem number? Hopefully still using the command \ref{} ...

Example, in section 2 of chapter 1 I have a theorem with label X and number 2.1 (the 2 stands for the section number and 1 for the theorem number in that section). I refer to it in chapter 5, e.g. "by theorem \ref{X}, we obtain ...". The result then is "by theorem 2.1, we obtain ...".

share|improve this question
1  
Please edit your question and add a short example which reproduces the problem; otherwise it will be difficult for anyone to help you. –  Ian Thompson May 29 '12 at 20:02
    
So your theorems are numbered on their own and do not have the chapter number added by default? –  Werner May 29 '12 at 20:02
    
Hmm. Now that I reread the question I am not sure if yo are asking to append the chapter number only to the string produced by \ref. I'll keep my answer in wait for clarification. –  Gonzalo Medina May 29 '12 at 20:19
    
@Werner: indeed, though I want to chapter number to be added only when referring to it from another chapter. –  Nadori May 29 '12 at 20:39
3  
+1 for an interesting question. However, think about it from the reader's perspective- personally I'd find it a lot easier to navigate by adopting the tried and tested Theorem 1.3.6 where 1 is the chapter number, 3 is the section number, and 6 is the theorem number. Making the reader read, 'see Theorem 3.6 in chapter 1' is a lot more work than making them read 'see Theorem 1.3.6'. Over the course of a big document (and it sounds like it's pretty big), this might get annoying... just an opinion –  cmhughes May 29 '12 at 22:21

1 Answer 1

The following minimal working example creates a document with the following structure:

Chapter 1
  Section 5
    Theorem 5.4
Chapter 7
  Section 10
    Theorem 10.2

with some dummy text from lipsum. The chapter/section/theorem counters have been set explicitly just for this example.

References using \ref{<label>} return 5.4 and 10.2 regardless of the location where it's called, while \thmref{<label>} conditionally prepends the reference with <chapter>. if the target it not within the chapter. The entire functionality rests on Heiko Oberdiek's (awesome) zref package, and is very much related to Extract Section number from Equation reference. For every label made with \label, a "special" label is created that stores the chapter for that label, since the traditional \label-\ref system only allows for storing two elements: the current label/default and the page. So, chapter numbers as an add-on doesn't easily fit within this realm without some help.

\documentclass{report}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{amsthm}% http://ctan.org/pkg/amsthm
\usepackage{zref}% http://ctan.org/pkg/zref

\makeatletter
\let\oldlabel\label
\renewcommand{\label}[1]{%
  \zref@labelbylist{#1}{special}% Special label
  \oldlabel{#1}% Old label
}
\newcounter{splabel}
\zref@newlist{special}% Create a new property list called special
\zref@newprop{chapter}{\arabic{chapter}}% Section property holds \arabic{chapter}
\zref@addprop{special}{chapter}% Add a chapter property to special
\newcommand*{\thmref}[1]{%
  \stepcounter{splabel}% Increment local "special label" counter
  \zref@labelbylist{#1-\thesplabel}{special}% Create label
  \edef\targetchap{\zref@extractdefault{#1}{chapter}{-1}}% Extract target chapter
  \edef\sourcechap{\zref@extractdefault{#1-\thesplabel}{chapter}{-1}}% Extract source chapter
  \ifnum\targetchap=\sourcechap\else\targetchap.\fi%
  \ref{#1}%
}
\newtheorem{theorem}{Theorem}
\renewcommand{\thetheorem}{\arabic{section}.\arabic{theorem}}% Theorem <section>.<theorem>
\@addtoreset{theorem}{chapter}% Theorems reset at start of \chapter
\makeatother

\begin{document}

\chapter{First chapter}\lipsum[1]% Chapter 1
See Theorem~\ref{thm:first} or~\ref{thm:second}.
See Theorem~\thmref{thm:first} or~\thmref{thm:second}.
\setcounter{section}{4}% Just for this example
\section{A section}% Section 5
\setcounter{theorem}{3}% Just for this example
\begin{theorem}\label{thm:first}\lipsum[2]\end{theorem}% Theorem 5.4

\setcounter{chapter}{6}% Just for this example
\chapter{Second chapter}\lipsum[3]% Chapter 7
\setcounter{section}{9}% Just for this example
\section{A section}% Section 10
\setcounter{theorem}{1}% Just for this example
\begin{theorem}\label{thm:second}\lipsum[4]\end{theorem}% Theorem 10.2

\end{document}

enter image description here

The basic idea is the following: Whenever you call \thmref{<label>}, an additional "source" label <label>-# (where # is a counter). The chapter number of the "source" and "target" labels are compared and target chapter is printed conditionally if "source" doesn't equal "target".

Functionality compatible with hyperref is also possible, if needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.