2

I am writing a document with the amsbook class. I have defined a macro

\newcommand{\thmref}[1]{Theorem~\ref{#1}}

I would like to include the chapter number as follows: "(4, Theorem 2.7.6)", not just "Theorem 2.7.6".

So I need some macro like

\newcommand{\thmref}[1]{(\chapterNumber{#1}, Theorem~\ref{#1})}

with some definition of \chapterNumber. This command should accept a reference key like thm:very impportant theorem and print out the number of the chapter which contains it. I'm sure this must be very simple, but I don't know it. Can you help? Thank you!

--

Here is a working example:

\documentclass{amsbook}

\newtheorem{thm}[subsubsection]{Theorem}
\newcommand{\thmref}[1]{Theorem~\ref{#1}}

\begin{document}

\chapter{First}
\section{Alpha}
\subsection{Beta}

\begin{thm} \label{thm:important theorem}
Blah blah
\end{thm}

\chapter{Second}

\section{Gamma}
\subsection{Phi}

\begin{thm}
Foo bar
\end{thm}

This follows from \thmref{thm:important theorem}.

\end{document}
Werner
  • 603,163
  • Welcome to TeX.SX! Please provide a compilable document. On the hand, isn't a section number like 4.2.7.6, i.e. with leading chapter number much easier? ;-) –  Jul 12 '16 at 23:30
  • @ChristianHupfer Thanks! I added an example. Yes, probably you're right, but I think its gets confusing with too many numbers :) – texnoobgirl Jul 12 '16 at 23:40
  • The problem is that the thm. number 1.1.1 is not coupled to the chapter number here! I've got another proposition: Use a different counter output for the theorem, i.e. don't use the full 1.1.1 scheme for section.subsection.thm number, but just chapter.thm etc. –  Jul 12 '16 at 23:46

1 Answers1

1

The following uses the functionality of zref to store the chapter counter value with every call to \label. It is extracted as part of a call to \thmref, setting the chapter number together with the local reference:

enter image description here

\documentclass{amsbook}

\usepackage[user]{zref}

\newtheorem{thm}[subsubsection]{Theorem}
\newcommand{\thmref}[1]{(\chapterNumber{#1}, Theorem~\ref{#1})}

% https://tex.stackexchange.com/a/57831/5764
\makeatletter
\let\oldlabel\label
\renewcommand{\label}[1]{%
  \zref@labelbylist{#1}{special}% Special label
  \oldlabel{#1}% Old label
}
\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

% Extract chapter number; defaults to -1 if it doesn't exist (or on first compile)
\newcommand{\chapterNumber}[1]{\zref@extractdefault{#1}{chapter}{-1}}
\makeatother

\begin{document}

\chapter{First}
\section{Alpha}
\subsection{Beta}

\begin{thm} \label{thm:important theorem}
Blah blah
\end{thm}

\chapter{Second}
\section{Gamma}
\subsection{Phi}

\begin{thm}
Foo bar
\end{thm}

This follows from \thmref{thm:important theorem}.

\end{document}

Construction of the above details stem from Refer to theorems in previous chapter where one can also condition on whether or not to add the current chapter number to a reference if the reference occurs within the same chapter as the label.

Werner
  • 603,163