4

i want to do the same like in this answered question:

Renumbering chapter after part, correct ref, ToC and chap header aspect

which gives the following code to add:

\usepackage{chngcntr}
\counterwithin{chapter}{part}

\makeatletter
\renewcommand{\thechapter}{\@arabic\c@chapter}
\renewcommand{\p@chapter}{\thepart.}
\makeatother

with one difference:

Is it possible to add the part-"part" after the chapter-"part" inside the cross-reference?

What I mean is, is it possible to set a suffix?

The result should for:

Comparison between \autoref{part_1_chapter_1} and \autoref{part_2_chapter_1}

should look somehow like:

Comparison between Chapter 1/Part I and Chapter 1/Part II

UPDATE:

I want to be able to ref to any command like chapter, section, subsection, subsubsection and have the normal output of autoref and after this there should be added the part from this ref.

For example:

Chapter 1/Part 1 or Section 2/Part 1

6
  • You mention \autoref -- would you be OK with \cref (from the cleveref package) as well?
    – Mico
    Commented Feb 28, 2017 at 19:24
  • Related: tex.stackexchange.com/questions/347266/… and tex.stackexchange.com/questions/312060/…
    – user31729
    Commented Feb 28, 2017 at 20:05
  • Well Cleverref seem to be able to do the same,
    – duichwer
    Commented Feb 28, 2017 at 21:25
  • @duichwer: Do you need the names with the first character in upper case letter, i.e. Chapter, Section, Equation etc.?
    – user31729
    Commented Feb 28, 2017 at 22:02
  • @duichwer: See the update please!
    – user31729
    Commented Feb 28, 2017 at 22:32

2 Answers 2

4

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}

enter image description here

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}

enter image description here

9
  • I also can't upload images due to the imgur problem at the moment
    – user31729
    Commented Feb 28, 2017 at 20:03
  • I think this approach is going in the right direction, but my question, wasn't perfect asked. I want the normal behaviour for ref a any command, like chapter, section, etc. and insert after any of these commands the part.
    – duichwer
    Commented Feb 28, 2017 at 21:26
  • 1
    @duichwer: In fact, it was not asked correctly. I think the answer can be changed, however.
    – user31729
    Commented Feb 28, 2017 at 21:49
  • Thanks, the result, looks ecactly what I expected. How can i get the text of \zref@extract{#1}{counter} translated into the german Options like Kapitel odr Abschnitt?
    – duichwer
    Commented Mar 1, 2017 at 10:38
  • 1
    @duichwer: Bitte das Update am Ende der Antwort beachten, die Verallgemeinerung auf andere Zählernamen ist recht einfach, siehe \StoreCounterNameMap, allerdings ist der equation - Zähler mit zref problematisch, dafür benötigt man "Gewalt" und einen eigenen Befehl. Der Link springt für Gleichungen etwas tiefer als die Gleichungsnummer.
    – user31729
    Commented Mar 1, 2017 at 12:59
2

Something like this?

enter image description here

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}
7
  • 1
    Aside: I seem to be unable at the moment to upload a screenshot; I'll try again in a little while.
    – Mico
    Commented Feb 28, 2017 at 19:47
  • Who downvoted this?
    – user31729
    Commented Feb 28, 2017 at 19:57
  • @ChristianHupfer - I have a hunch it's the very same person who downvoted two of my answers yesterday -- see tex.stackexchange.com/a/355744/5001 and tex.stackexchange.com/a/355858/5001. That person must be getting a real rush of satisfaction from performing these oh-so-courageous downvotes...
    – Mico
    Commented Feb 28, 2017 at 20:08
  • One downvote was 'equalized' already, I voted for both ... yes, we have courageous downvoters here....
    – user31729
    Commented Feb 28, 2017 at 20:10
  • By the way, that answer on the cross-referencing packages is really great (that one you got a badge today for ;-)
    – user31729
    Commented Feb 28, 2017 at 20:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .