| 1 |
%% LaTeX package xassoccnt - version 2.0 (2021/11/21 -- 12:14:58) |
| 2 |
%% Example file for determing the parent counter of a counter and referring to it file for xassoccnt.sty |
| 3 |
%% |
| 4 |
%% |
| 5 |
%% ------------------------------------------------------------------------------------------- |
| 6 |
%% Copyright (c) 2017 -- 2021 by Dr. Christian Hupfer <> |
| 7 |
%% ------------------------------------------------------------------------------------------- |
| 8 |
%% |
| 9 |
%% This work may be distributed and/or modified under the |
| 10 |
%% conditions of the LaTeX Project Public License, either version 1.3 |
| 11 |
%% of this license or (at your option) any later version. |
| 12 |
%% The latest version of this license is in |
| 13 |
%% http://www.latex-project.org/lppl.txt |
| 14 |
%% and version 1.3 or later is part of all distributions of LaTeX |
| 15 |
%% version 2005/12/01 or later. |
| 16 |
%% |
| 17 |
%% |
| 18 |
%% This work has the LPPL maintenance status `unmaintained` |
| 19 |
%% |
| 20 |
%% |
| 21 |
|
| 22 |
|
| 23 |
% 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 |
| 24 |
% The provided answer by Christian Hupfer did not use xassoccnt but the features were incorporated in the package. |
| 25 |
|
| 26 |
\documentclass{book} |
| 27 |
\usepackage{lipsum} |
| 28 |
\usepackage{xassoccnt} |
| 29 |
\usepackage{xpatch} |
| 30 |
\usepackage{hyperref} |
| 31 |
\usepackage[user,counter,hyperref]{zref} |
| 32 |
\usepackage{cleveref} |
| 33 |
|
| 34 |
\makeatletter |
| 35 |
\AtEndPreamble{ |
| 36 |
\newif\if@hyperrefloaded |
| 37 |
|
| 38 |
\@ifpackageloaded{hyperref}{% |
| 39 |
\@hyperrefloadedtrue |
| 40 |
}{ |
| 41 |
\@hyperrefloadedfalse |
| 42 |
} |
| 43 |
} |
| 44 |
\makeatother |
| 45 |
|
| 46 |
\usepackage{xparse} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
\makeatletter |
| 51 |
|
| 52 |
\ExplSyntaxOn |
| 53 |
\prop_new:N \g_latex_parentcounters_prop |
| 54 |
\seq_new:N \g_latex_counters_seq |
| 55 |
|
| 56 |
\cs_generate_variant:Nn \str_head:n {x,o,V} |
| 57 |
\cs_generate_variant:Nn \str_tail:n {x,o,V} |
| 58 |
\cs_generate_variant:Nn \str_upper_case:n {x,o,V} |
| 59 |
\cs_generate_variant:Nn \str_set:Nn {Nx,No} |
| 60 |
|
| 61 |
% Store all reset lists and make a property list for each counter and its possible parent counter |
| 62 |
|
| 63 |
% Make the first character of a word upper case |
| 64 |
\newcommand{\FirstUpcase}[1]{% |
| 65 |
\str_set:Nx \l_tmpa_str {#1}% |
| 66 |
\str_upper_case:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}% |
| 67 |
} |
| 68 |
|
| 69 |
\ExplSyntaxOff |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
% Define new properties |
| 75 |
\zref@newprop{childcountervalue}{\arabic{\LastRefSteppedCounter}}% This is the naked value |
| 76 |
\zref@newprop{parentcountervalue}{\csname the\GetParentCounter{\LastRefSteppedCounter}\endcsname} |
| 77 |
\zref@newprop{parentcountername}{\GetParentCounter{\LastRefSteppedCounter}} |
| 78 |
|
| 79 |
% Add the new properties to the main property list stored with \zlabel |
| 80 |
\zref@addprops{main}{childcountervalue,parentcountervalue,parentcountername} |
| 81 |
|
| 82 |
% Command for uppercase output |
| 83 |
\NewDocumentCommand{\parentCref}{m}{% |
| 84 |
\zref@ifrefundefined{#1}{% |
| 85 |
\Cref{#1}% |
| 86 |
}{% |
| 87 |
\edef\@tmpb@{\zref@extract{#1}{parentcountervalue}}% |
| 88 |
\edef\@tmpa@{\csname the\zref@extract{#1}{parentcountername}\endcsname}% |
| 89 |
\ifx\@tmpb@\@tmpa@ |
| 90 |
\Cref{#1}% |
| 91 |
\else |
| 92 |
\if@hyperrefloaded |
| 93 |
\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}}% |
| 94 |
\else |
| 95 |
\FirstUpcase{\zref@extract{#1}{counter}} \zref@extract{#1}{childcountervalue} of \FirstUpcase{\zref@extract{#1}{parentcountername}} \zref@extract{#1}{parentcountervalue}% |
| 96 |
\fi |
| 97 |
\fi |
| 98 |
}% |
| 99 |
} |
| 100 |
|
| 101 |
% Command for lowercase output |
| 102 |
\NewDocumentCommand{\parentcref}{m}{% |
| 103 |
\zref@ifrefundefined{#1}{% |
| 104 |
\cref{#1}% |
| 105 |
}{% |
| 106 |
\edef\@tmpb@{\zref@extract{#1}{parentcountervalue}}% |
| 107 |
\edef\@tmpa@{\csname the\zref@extract{#1}{parentcountername}\endcsname}% |
| 108 |
\ifx\@tmpb@\@tmpa@ |
| 109 |
\Cref{#1}% |
| 110 |
\else |
| 111 |
\if@hyperrefloaded |
| 112 |
\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}}% |
| 113 |
\else |
| 114 |
\zref@extract{#1}{counter} \zref@extract{#1}{childcountervalue} of \zref@extract{#1}{parentcountername} \zref@extract{#1}{parentcountervalue}% |
| 115 |
\fi |
| 116 |
\fi |
| 117 |
}% |
| 118 |
} |
| 119 |
|
| 120 |
\makeatother |
| 121 |
|
| 122 |
|
| 123 |
\GetAllResetLists |
| 124 |
\RegisterPostLabelHook{\zlabel} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
\begin{document} |
| 129 |
\chapter{Chapter One} |
| 130 |
Please look at \parentCref{othersection} or at \parentCref{foosubsection} or at \parentcref{foosubsection} |
| 131 |
|
| 132 |
|
| 133 |
\section{Section One.One} |
| 134 |
\label{mysection} |
| 135 |
\lipsum |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
\section{Section One.Two} |
| 141 |
|
| 142 |
\subsection{A subsection} \label{foosubsection} |
| 143 |
Please ignore \parentCref{mysection}. |
| 144 |
|
| 145 |
\chapter{Chapter Two} |
| 146 |
\section{Section Two.One} |
| 147 |
|
| 148 |
As you may have noticed, \parentCref{mysection} is useless. |
| 149 |
|
| 150 |
|
| 151 |
\chapter{Chapter Three} |
| 152 |
\section{Section Three.One} % |
| 153 |
\section{Section Three.Two}\label{othersection} |
| 154 |
|
| 155 |
\end{document} |
| 156 |
|