# Full Part/Chapter/Section etc. Hierarchy shown in Autoref (Koma Script 3.2, scrbook)

I am writing a long piece of text that is structured into several parts, numerous chapters, etc. In the later part of the document, I need to reference sections etc. that belong to a previous Part, say in Part II of the document I need to reference Part I, Section 1.

However, \autoref{} only creates links that are named "Section 1" rather than Part I, Section 1. In other words, it remains unclear to which part the referenced section belongs.

Is there any way to create references that show the full hierarchy of parts, chapters, sections etc?

Best regards

NP.

\documentclass[12pt, a4paper, tocindentauto]{scrbook}

\usepackage{hyperref}

\begin{document}

\part{Part 1}
\chapter{Chapter 1}
\label{chap:P1chap1}

\section{Section 1}
\label{sec:P1chap1section1}

\subsection{Subsection 1}
\label{ssec:sec:P1chap1section1subsection1}

\part{Part 2}
\chapter{Chapter 1}
\label{chap:P2chap1}

\autoref{chap:P1chap1} should look like: I.1

\autoref{sec:P1chap1section1} should look like I.1.1

\autoref{ssec:sec:P1chap1section1subsection1} should look like I.1.1.1

\autoref{chap:P2chap1} should look like II.1

\end{document}


Thanks for all the support so far! However, things got more complicated... :-/ While the referencing works fine in the example, my 'real' document uses some custom numbering for the table of contents. These commands seem to interfere with \ref{} command, unfortunately.

\documentclass[12pt, a4paper, tocindentauto]{scrbook}

\renewcommand\thepart{\Alph{part}}
\renewcommand\thechapter{\Roman{chapter}}

\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}

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

\usepackage{hyperref}

\makeatletter
\AtBeginDocument{%
\def\@prependpart\csname #1\endcsname{%
\thepart.\csname#1\endcsname%
}
\renewcommand{\p@chapter}{\@prependpart}
\renewcommand{\p@section}{\@prependpart}
\renewcommand{\p@subsection}{\@prependpart}
\renewcommand{\p@subsubsection}{\@prependpart}
\let\theHchapterorig\theHchapter%
\renewcommand{\theHchapter}{\thepart.\theHchapterorig}
}
\makeatother

\setcounter{secnumdepth}{3}

\begin{document}

\tableofcontents

\part{Title Part A}
\chapter{Chapter 1}
\label{chap:P1chap1}

\section{Section 1}
\label{sec:P1chap1section1}

\section{Section 2}
\label{sec:P1chap1section2}

\subsection{Subsection 1}
\label{ssec:sec:P1chap1section1subsection1}

\subsubsection{Subsubsection 1}
\label{ssec:sec:P1chap1section1subsubsection1}

\subsubsection{Subsubsection 2}
\label{ssec:sec:P1chap1section1subsubsection2}

\part{Title Part B}
\chapter{Chapter 1}
\label{chap:P2chap1}

\ref{chap:P1chap1} should look like: A.I - OK

\ref{sec:P1chap1section1} should look like A.I.1 - I missing

\ref{ssec:sec:P1chap1section1subsubsection2} should look like A.I.2.1.2 - I missing

\ref{chap:P2chap1} should look like B.I - OK

\end{document}

• Very good example. Welcome. Feb 5 '17 at 12:25
• \renewcommand{\thechapter}{\Roman{part}.\arabic{chapter}} along with \counterwithin{chapter}{part} from package chngcntr. Feb 5 '17 at 12:25
• @Johannes_B: This destroys the unique hyperref anchors, however. The \renewcommand is not necessary, since \counterwithin uses the format then already, which is \Roman for the part counter, but the command changes the full numbering as well, so Chapter I.1 etc. I think only the references are wanted to change this way!
– user31729
Feb 5 '17 at 12:43
• If things work out for a given question, that's nice. If there are other (non-mentioned) tripwires you saw later, make a new question out of it ;-) Please do so. Feb 6 '17 at 6:57

Just \counterwithin{chapter}{part} is prepending the part number everywhere there \thechapter is called, so in the ToC and and the chapter header as well as in for every \thesection etc.

As far as I have understood only the references should be changed, so Part 3, Chapter 5 should become III.5 when being referred to.

This needs a redefinition of the \p@chapter and \p@section etc. commands, by using an 'argument' grabber, say \@prependpart that adds the \thepart. output to the references and stores in the .aux file at definition time, i.e. the correct part number is frozen for references.

In addition to put chapter on the reset list of part, this will cause problems with hyperref's hyper anchor system. The hyper anchor for chapter one is usually being called chapter.1, but resetting the chapter number for each part, the chapter.1 anchor isn't unique any longer.

Redefining the \theHchapter anchor macro will solve this.

\documentclass[12pt, a4paper, tocindentauto]{scrbook}

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

\usepackage{hyperref}

\makeatletter
\AtBeginDocument{%
\def\@prependpart\csname #1\endcsname{%
\thepart.\csname#1\endcsname%
}
\renewcommand{\p@chapter}{\@prependpart}
\renewcommand{\p@section}{\@prependpart}
\renewcommand{\p@subsection}{\@prependpart}
\renewcommand{\p@subsubsection}{\@prependpart}
\let\theHchapterorig\theHchapter%
\renewcommand{\theHchapter}{\thepart.\theHchapterorig}
}
\makeatother

\begin{document}

\part{Part 1}
\chapter{Chapter 1}
\label{chap:P1chap1}

\section{Section 1}
\label{sec:P1chap1section1}

\subsection{Subsection 1}
\label{ssec:sec:P1chap1section1subsection1}

\part{Part 2}
\chapter{Chapter 1}
\label{chap:P2chap1}

\autoref{chap:P1chap1} should look like: I.1

\autoref{sec:P1chap1section1} should look like I.1.1

\autoref{ssec:sec:P1chap1section1subsection1} should look like I.1.1.1

\autoref{chap:P2chap1} should look like II.1

\end{document}


Update for another numbering scheme

\documentclass[12pt, a4paper, tocindentauto]{scrbook}

\renewcommand\thepart{\Alph{part}}
\renewcommand\thechapter{\Roman{chapter}}

\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}

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

\usepackage{xpatch}
\usepackage{hyperref}

\makeatletter
\AtBeginDocument{%
\xpretocmd{\p@chapter}{\thepart.}{}{}
\xpretocmd{\p@section}{\thepart.\thechapter.}{}{}
\xpretocmd{\p@subsection}{\p@section}{}{}
\xpretocmd{\p@subsubsection}{\p@subsection}{}{}
\let\theHchapterorig\theHchapter%
\renewcommand{\theHchapter}{\thepart.\theHchapterorig}%
}{}
}
\makeatother

\setcounter{secnumdepth}{3}

\begin{document}

\tableofcontents

\part{Title Part A}
\chapter{Chapter 1}
\label{chap:P1chap1}

\section{Section 1}
\label{sec:P1chap1section1}

\section{Section 2}
\label{sec:P1chap1section2}

\subsection{Subsection 1}
\label{ssec:sec:P1chap1section1subsection1}

\subsubsection{Subsubsection 1}
\label{ssec:sec:P1chap1section1subsubsection1}

\subsubsection{Subsubsection 2}
\label{ssec:sec:P1chap1section1subsubsection2}

\part{Title Part B}
\chapter{Chapter 1}
\label{chap:P2chap1}

\ref{chap:P1chap1} should look like: A.I - OK

\ref{sec:P1chap1section1} should look like A.I.1 - I missing

\ref{ssec:sec:P1chap1section1subsubsection2} should look like A.I.2.1.2 - I missing

\ref{chap:P2chap1} should look like B.I - OK

\end{document}

• Great thanks for the quick comments and solution! :-) Saved me tons of time! Just one last quick question: is it possible to remove the "chapter", "section", "subsection" from the reference, just showing "I.1" instead of "chapter I.1"? Feb 5 '17 at 13:02
• @user124612: You're welcome. Please consider to accept the solution later on. Happy TeXing!
– user31729
Feb 5 '17 at 13:03
• @Schweinebacke: That's true, but varioref is not involved here. In fact, I never used that package for personal purposes
– user31729
Feb 5 '17 at 13:52
• @user124612: Why the unacceptance? If you did not post the setup you have and did not show the real document it is not really kosher to unaccept the solution that was working for the MWE you posted initially
– user31729
Feb 5 '17 at 21:32
• @user124612 If a answer is a solution for the given example but not for the real document it would be fair to accept it and make a follow-up question with a new MWE. Feb 6 '17 at 10:09