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

duichwer
  • 119
  • You mention \autoref -- would you be OK with \cref (from the cleveref package) as well? – Mico Feb 28 '17 at 19:24
  • Related: http://tex.stackexchange.com/questions/347266/cross-reference-differently-depending-on-whether-we-are-in-the-same-part-chapter/347271?s=19|0.0000#347271 and http://tex.stackexchange.com/questions/312060/how-to-reference-sections-in-other-parts-mentioning-the-part/347161?s=20|0.0000#347161 –  Feb 28 '17 at 20:05
  • Well Cleverref seem to be able to do the same, – duichwer Feb 28 '17 at 21:25
  • @duichwer: Do you need the names with the first character in upper case letter, i.e. Chapter, Section, Equation etc.? –  Feb 28 '17 at 22:02
  • @duichwer: See the update please! –  Feb 28 '17 at 22:32
  • A similar approach can be found in my answer to this question here, although the background is different: http://tex.stackexchange.com/questions/353491/how-to-show-also-the-name-of-the-parent-counter-with-cleveref –  Feb 28 '17 at 22:39

2 Answers2

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

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • I also can't upload images due to the imgur problem at the moment –  Feb 28 '17 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 Feb 28 '17 at 21:26
  • 1
    @duichwer: In fact, it was not asked correctly. I think the answer can be changed, however. –  Feb 28 '17 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 Mar 01 '17 at 10:38
  • @duichwer: Nicht ohne viel Mühe ;-) Dafür existiert keine direkte Übersetzung. –  Mar 01 '17 at 10:49
  • 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. –  Mar 01 '17 at 12:59
  • @duichwer: Ich habe eine neue Version hochgeladen -- die Links funktionieren jetzt korrekt auch für den Gleichungszähler. –  Mar 01 '17 at 15:26
  • Hufper Wow. Your answer is really awesome. Is there any good documentation for the commands/package that you used to define those methods like getNameFromCounter?.. Maybe in German? – duichwer Mar 01 '17 at 15:27
  • @duichwer: No, the parts with \ExplSyntaxOn... \ExplSyntaxOff is the considered to be the 'new' (cough!!!!) LaTeX 3 syntax -- there is no German documentation as far as I know. I it used to simplify often needed programming tasks in LaTeX (2e). See the documentation of interface3.pdf (texdoc interface3 on commandline, for example). It's very tricky for new users to TeX and I don't claim I am do use it correctly in any occasion –  Mar 01 '17 at 15:30
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}
Mico
  • 506,678
  • 1
    Aside: I seem to be unable at the moment to upload a screenshot; I'll try again in a little while. – Mico Feb 28 '17 at 19:47
  • Who downvoted this? –  Feb 28 '17 at 19:57
  • @ChristianHupfer - I have a hunch it's the very same person who downvoted two of my answers yesterday -- see http://tex.stackexchange.com/a/355744/5001 and http://tex.stackexchange.com/a/355858/5001. That person must be getting a real rush of satisfaction from performing these oh-so-courageous downvotes... – Mico Feb 28 '17 at 20:08
  • One downvote was 'equalized' already, I voted for both ... yes, we have courageous downvoters here.... –  Feb 28 '17 at 20:10
  • By the way, that answer on the cross-referencing packages is really great (that one you got a badge today for ;-) –  Feb 28 '17 at 20:18
  • @ChristianHupfer :-) (It's my fifth great-answer gold badge. Yay!) – Mico Feb 28 '17 at 20:19
  • Well-deserved... I don't even have a Guru - Badge yet –  Feb 28 '17 at 21:05