# (Re)define \ref for chapter that includes the part number

Consider this MWE:

\documentclass{book}
\usepackage{xcolor}
\pagecolor{yellow!10}
\usepackage{trace}

% avoid page breaks of \part, \chapter
\makeatletter
\renewcommand{\part}{\secdef \@part \@spart}
\let\@endpart\relax % suppress page break after \part
\renewcommand{\chapter}{\global \@topnum \z@ \@afterindentfalse \secdef \@chapter \@schapter}
\makeatother

\begin{document}

\def\thechapter{\roman{part}.\alph{chapter}} % this changes printout in chapter header itself, but not in the ref

Citing part \ref{part:one}, chapter \traceon \ref{chapter:one} \traceoff

\part{part one}\label{part:one}
\chapter{chapter one}\label{chapter:one}

\end{document}


... which produces this result:

... where the chapter title is printed as "Chapter i.a", and the \ref to it also prints "i.a" (as expected due to the redefinition of \thechapter as \roman{part}.\alph{chapter})

Here is the thing - I would like to not redefine \thechapter, that is, I'd like to keep the default "Chapter 1" printout for \chapter, and the default "1" for a \ref to it. Instead, I would like to define a new command, say \pcref, which given a chapter label, would print a "I.A" reference like in the example. How could I do that?

The part of the challenge, I guess, is that the printing of \ref (as shown by the trace) reduces to:

%...
\@setref #1#2#3->\ifx #1\relax \protect \G@refundefinedtrue \nfss@text {\reset@
font \bfseries ??}\@latex@warning {Reference #3' on page \thepage \space undef
ined}\else \expandafter #2#1\null \fi
#1<-\r@chapter:one
#2<-\@firstoftwo
#3<-chapter:one
{\ifx}
{false}
{\expandafter}

\r@chapter:one ->{i.a}{1}

\@firstoftwo #1#2->#1
#1<-i.a
#2<-1
{the letter i}

\null ->\hbox {}
{\hbox}
%...


... that is, when \ref is called, the \r@chapter:one command is already set, either to i.a or to 1, depending on \thechapter; that is, if there is no information about part number when \label is called, no \ref redefinition can use it either. So, I guess, the tricky part is doing all this (a new \pcref command with old \thechapter) without having to redefine \label...

• \global\topnum=\z@ will disable top floats forever. Are you really sure that book is the right class for this job? – egreg Nov 19 '14 at 14:51
• Thanks @egreg - I just took the book class because I have that in my original problem, and so I wanted to have \part and \chapter defined like there; I otherwise suppressed all those page breaks just to have a nice screenshot (showing all on one page) for this question :) Cheers! – sdaau Nov 19 '14 at 14:53
• Isn't this possible with cleveref package? – user31729 Nov 19 '14 at 14:59
• Thanks @ChristianHupfer - didn't know about cleveref until now, thanks for the mention; cheers! – sdaau Nov 19 '14 at 15:04
• @ChristianHupfer - just checked cleveref; the best I can figure is to use something like \creflabelformat{chapter}{#2-\thepart-#1-#3}; unfortunately, this does not change the label format when it is placed with \label, but depends on the part where the \cref command will be issued; so I guess cleveref cannot help much here... – sdaau Nov 19 '14 at 15:40

I'm not following you into avoiding page breaks, but I'll just show how to do what you seem to want.

\documentclass{book}

\makeatletter
\renewcommand{\p@chapter}{\thepart.}
\makeatother

\begin{document}
\mainmatter
\part{Some part title}

\chapter{The chapter title}\label{A}

\section{A section title}\label{B}

\ref{A}'' is a reference to a chapter, while \ref{B}'' is a reference to a section

\end{document}


Explanation: \p@chapter is the prefix added to the chapter number when a reference to it is requested. Whenever you do

\newcounter{foo}


LaTeX provides by default an empty \p@foo macro.

If you want to have different ways for referencing a chapter, you can do like this

\documentclass{book}

\makeatletter
\renewcommand{\p@chapter}{\maybe@part{\thepart.}}
\newcommand{\pcref}[1]{\begingroup\let\maybe@part\@iden\ref{#1}\endgroup}
\protected\def\maybe@part#1{}
\makeatother

\begin{document}
\mainmatter
\part{Some part title}

\chapter{The chapter title}\label{A}

\section{A section title}\label{B}

\ref{A}'' is a reference to a chapter, while \ref{B}'' is a reference to a section
and \pcref{A}'' is a reference to a chapter with the part number attached to it.

\end{document}


but I'd avoid it. Chapters should be numbered independently of parts, so the problem doesn't present itself to begin with. Having two or more “Chapter 1” is just confusing.

• Many thanks for that, @egreg, was not aware about the prefix thing! This does almost what I want, except here \ref prints "I.1"; while I wanted to keep the original \ref, which prints "1", and have a new command e.g. \pcref which would print the "I.1". Is that possible? (ps: if I had page breaks, I would have had 5 pages, which as an image would either be too big or too low resolution for posting; without them, I can show all three sections on the same page, so they are readable in the screenshot posted in OP as well). Thanks again - cheers! – sdaau Nov 19 '14 at 15:08
• Thanks for the edit, @egreg, that is fantastic - the \pcref` there is exactly what I wanted! You're right about chapter numbering in general - still, this will be very useful to myself as a reminder while writing (having to refer to chapters in different parts from, say, an introduction). Cheers! – sdaau Nov 19 '14 at 15:35