%% LaTeX package xassoccnt - version 1.7 (2018/12/28 -- 06:45:16) %% Example file for determing the parent counter of a counter and referring to it file for xassoccnt.sty %% %% %% ------------------------------------------------------------------------------------------- %% Copyright (c) 2017 -- 2018 by Dr. Christian Hupfer %% ------------------------------------------------------------------------------------------- %% %% This work may be distributed and/or modified under the %% conditions of the LaTeX Project Public License, either version 1.3 %% of this license or (at your option) any later version. %% The latest version of this license is in %% http://www.latex-project.org/lppl.txt %% and version 1.3 or later is part of all distributions of LaTeX %% version 2005/12/01 or later. %% %% %% This work has the LPPL maintenance status `author-maintained` %% %% % Source was a question on TeX.SE by user gigabytes: http://tex.stackexchange.com/questions/353491/how-to-show-also-the-name-of-the-parent-counter-with-cleveref % The provided answer by Christian Hupfer did not use xassoccnt but the features were incorporated in the package. \documentclass{book} \usepackage{lipsum} \usepackage{xassoccnt} \usepackage{xpatch} \usepackage{hyperref} \usepackage[user,counter,hyperref]{zref} \usepackage{cleveref} \makeatletter \AtEndPreamble{ \newif\if@hyperrefloaded \@ifpackageloaded{hyperref}{% \@hyperrefloadedtrue }{ \@hyperrefloadedfalse } } \makeatother \usepackage{xparse} \makeatletter \ExplSyntaxOn \prop_new:N \g_latex_parentcounters_prop \seq_new:N \g_latex_counters_seq \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_upper_case:n {x,o,V} \cs_generate_variant:Nn \str_set:Nn {Nx,No} % Store all reset lists and make a property list for each counter and its possible parent counter % Make the first character of a word upper case \newcommand{\FirstUpcase}[1]{% \str_set:Nx \l_tmpa_str {#1}% \str_upper_case:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}% } \ExplSyntaxOff % Define new properties \zref@newprop{childcountervalue}{\arabic{\LastRefSteppedCounter}}% This is the naked value \zref@newprop{parentcountervalue}{\csname the\GetParentCounter{\LastRefSteppedCounter}\endcsname} \zref@newprop{parentcountername}{\GetParentCounter{\LastRefSteppedCounter}} % Add the new properties to the main property list stored with \zlabel \zref@addprops{main}{childcountervalue,parentcountervalue,parentcountername} % Command for uppercase output \NewDocumentCommand{\parentCref}{m}{% \zref@ifrefundefined{#1}{% \Cref{#1}% }{% \edef\@tmpb@{\zref@extract{#1}{parentcountervalue}}% \edef\@tmpa@{\csname the\zref@extract{#1}{parentcountername}\endcsname}% \ifx\@tmpb@\@tmpa@ \Cref{#1}% \else \if@hyperrefloaded \FirstUpcase{\zref@extract{#1}{counter}} \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{childcountervalue}} of \FirstUpcase{\zref@extract{#1}{parentcountername}} \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{parentcountervalue}}% \else \FirstUpcase{\zref@extract{#1}{counter}} \zref@extract{#1}{childcountervalue} of \FirstUpcase{\zref@extract{#1}{parentcountername}} \zref@extract{#1}{parentcountervalue}% \fi \fi }% } % Command for lowercase output \NewDocumentCommand{\parentcref}{m}{% \zref@ifrefundefined{#1}{% \cref{#1}% }{% \edef\@tmpb@{\zref@extract{#1}{parentcountervalue}}% \edef\@tmpa@{\csname the\zref@extract{#1}{parentcountername}\endcsname}% \ifx\@tmpb@\@tmpa@ \Cref{#1}% \else \if@hyperrefloaded \zref@extract{#1}{counter} \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{childcountervalue}} of \zref@extract{#1}{parentcountername} \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{parentcountervalue}}% \else \zref@extract{#1}{counter} \zref@extract{#1}{childcountervalue} of \zref@extract{#1}{parentcountername} \zref@extract{#1}{parentcountervalue}% \fi \fi }% } \makeatother \GetAllResetLists \RegisterPostLabelHook{\zlabel} \begin{document} \chapter{Chapter One} Please look at \parentCref{othersection} or at \parentCref{foosubsection} or at \parentcref{foosubsection} \section{Section One.One} \label{mysection} \lipsum \section{Section One.Two} \subsection{A subsection} \label{foosubsection} Please ignore \parentCref{mysection}. \chapter{Chapter Two} \section{Section Two.One} As you may have noticed, \parentCref{mysection} is useless. \chapter{Chapter Three} \section{Section Three.One} % \section{Section Three.Two}\label{othersection} \end{document}