Renumbering chapter after part, include part number as suffix in cross-reference to chapter number
Here's a an approach (similar to Mico's answer) with zref
and storing the part information with \zlabel
instead of \label
.
The solution stores anchors and the type of the counter being referenced. It retrieves the counter name, changes the name to have the first letter being upper case and types this new name together with the part info.
If the part counter is used or the reference is not made with zlabel
, the \pref
command falls back to \Cref
.
The precise format of the output can be configured within \partreferenceformat
.
\documentclass{report}
\usepackage{etoolbox}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \str_head:n {x,o,V}
\cs_generate_variant:Nn \str_tail:n {x,o,V}
\cs_generate_variant:Nn \str_uppercase:n {x,o,V}
\cs_generate_variant:Nn \str_set:Nn {Nx}
\newcommand{\FirstUpcase}[1]{%
\str_set:Nx \l_tmpa_str {#1}%
\str_uppercase:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}%
}
\ExplSyntaxOff
\makeatletter
\newif\if@hyperrefloaded
\AtEndPreamble{%
\@ifpackageloaded{hyperref}{%
\global\@hyperrefloadedtrue
}{
\global\@hyperrefloadedfalse
}
}
\zref@newprop{partinfo}{\thepart}
\zref@addprop{main}{partinfo}
\newcommand{\partreferenceformat}[1]{%
\FirstUpcase{\zref@extract{#1}{counter}}\ \zref@extract{#1}{default}/Part \zref@extract{#1}{partinfo}%
}
\newcommand{\pref}[1]{%
\begingroup
\edef\temp@@a{part}%
\zref@ifrefundefined{#1}{%
\Cref{#1}%
}{%
% Checking whether we have a reference to the part counter -> use the usual \Cref for this
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\Cref{#1}%
\else
\if@hyperrefloaded
\hyperlink{\zref@extract{#1}{anchor}}{\partreferenceformat{#1}}%
\else
\partreferenceformat{#1}%
\fi
\fi
}%
\endgroup
}
\makeatother
\usepackage{blindtext}
\begin{document}
\tableofcontents
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\section{Foo section} \zlabel{sec:foo}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\blindtext[5]
\clearpage
A comparison between \pref{chap:uno} and \pref{chap:due} or \pref{sec:foo} \dots
On the other hand \pref{part:bbb} produces something different!
\end{document}
Old solution
The command \chappartref
retrieves the label and checks whether it is a label belonging to the chapter counter, otherwise it falls back to \ref
.
\documentclass{report}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\makeatletter
\zref@newprop{partinfo}{\thepart}
\zref@addprop{main}{partinfo}
\newcommand{\chappartref}[1]{%
\begingroup
\edef\temp@@a{chapter}%
\zref@ifrefundefined{#1}{%
\ref{#1}%
}{%
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\hyperlink{\zref@extract{#1}{anchor}}{Chapter \zref@extract{#1}{default}/Part \zref@extract{#1}{partinfo}}%%
\else
\ref{#1}%
\fi
}%
\endgroup
}
\makeatother
\begin{document}
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\clearpage
A comparison between \chappartref{chap:uno} and \chappartref{chap:due} \dots
On the other hand \chappartref{part:bbb} produces something different!
\end{document}
Next update With German translations for some counters
\documentclass{report}
\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{letltxmacro}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink,ngerman]{cleveref}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_zaehlernamen_deutsch_prop
\cs_generate_variant:Nn \str_head:n {x,o,V}
\cs_generate_variant:Nn \str_tail:n {x,o,V}
\cs_generate_variant:Nn \str_uppercase:n {x,o,V}
\cs_generate_variant:Nn \str_set:Nn {Nx}
\cs_generate_variant:Nn \prop_item:Nn {Nx}
\cs_generate_variant:Nn \prop_gput:Nnn {Nxx}
\newcommand{\FirstUpcase}[1]{%
\str_set:Nx \l_tmpa_str {#1}%
\str_uppercase:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}%
}
\NewDocumentCommand{\StoreCounterNameMap}{+m}{%
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
\seq_map_inline:Nn \l_tmpa_seq {
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}
\prop_gput:Nxx \g_zaehlernamen_deutsch_prop { \seq_item:Nn \l_tmpb_seq {1}} { \seq_item:Nn \l_tmpb_seq {2}}
}
}
\cs_new:Npn \GetNameFromCounter #1{%
\prop_item:Nx \g_zaehlernamen_deutsch_prop {#1}
}
\ExplSyntaxOff
\makeatletter
\newif\if@hyperrefloaded
\AtEndPreamble{%
\@ifpackageloaded{hyperref}{%
\global\@hyperrefloadedtrue
}{
\global\@hyperrefloadedfalse
}
}
\zref@newprop{partinfo}{\thepart}
\zref@newprop{equationinfo}{\theequation}
\zref@addprops{main}{partinfo,equationinfo}
\newcommand{\partreferenceformat}[1]{%
\GetNameFromCounter{\zref@extract{#1}{counter}}\ \zref@extract{#1}{default}/\GetNameFromCounter{part} \zref@extract{#1}{partinfo}%
}
\newcommand{\pref}[1]{%
\begingroup
\edef\temp@@a{part}%
\zref@ifrefundefined{#1}{%
\Cref{#1}%
}{%
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\Cref{#1}%
\else
\if@hyperrefloaded
\hyperlink{\zref@extract{#1}{anchor}}{\partreferenceformat{#1}}%
\else
\partreferenceformat{#1}%
\fi
\fi
}%
\endgroup
}
%Special command for zequationlabel
\newcommand{\zequationlabel}[1]{%
\label{#1}%
\IfRefUndefinedExpandable{#1}{}{%
\zref@setcurrent{anchor}{\getrefbykeydefault{#1}{anchor}\relax}%
}%
\zref@setcurrent{counter}{equation}%
\zref@setcurrent{default}{\getrefnumber{#1}}%
\zlabel{#1}%
}
\makeatother
\usepackage{blindtext}
\StoreCounterNameMap{
part/Teil,
chapter/Kapitel,
section/Abschnitt,
subsection/Unterabschnitt,
subsubsection/Unterunterabschnitt, % ;-)
paragraph/Paragraf,
subparagraph/Kleiner Paragraf,
figure/Abbildung,
table/Tabelle,
equation/Gleichung
}
\begin{document}
\tableofcontents
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\begin{align}
E&=mc^{2} \zequationlabel{Einstein} \\%
E^{2} &= m^{2} c^{4} + p^{2} c^{2} \zequationlabel{Einstein2}
\end{align}
\section{Foo section} \zlabel{sec:foo}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\blindtext[5]
\begin{align}
E&=mc^{2} \zequationlabel{Einstein3}%
\end{align}
\getrefbykeydefault{Einstein}{anchor}
\clearpage
Hier ist ein kleiner Vergleich zwischen \pref{chap:uno} und \pref{chap:due}, oder zum Beispiel \pref{sec:foo}, bzw. \pref{Einstein} bzw. \pref{Einstein2}\dots
Wird nicht \verb!\zlabel! verwendet, dann liefert \verb!\pref! ganz einfach den cleveref-Namen, z.B: \pref{part:bbb}.
Und weil es so schön ist: \pref{Einstein3}
\end{document}
Something like this?
Note that the \mychref
macro takes two arguments: here, they're labels of parts and chapters, but they could be anything, really.
\documentclass{report} % use a document class that provides "\chapter" macro
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
% Custom macro for composite cross-references
% Input ordering: part label, chapter label
% Output ordering: "Chapter" <number> "slash" "Part" <number>
\newcommand\mychref[2]{\Cref{#2}/\Cref{#1}}
\begin{document}
\part{AAA} \label{part:aaa}
\chapter{Uno} \label{chap:uno}
\part{BBB} \label{part:bbb}
\chapter{Due} \label{chap:due}
\clearpage
A comparison between \mychref{part:aaa}{chap:uno} and \mychref{part:bbb}{chap:due} \dots
\end{document}