summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/xassoccnt/xassoccnt_getparentcounter_example.tex
blob: 43d5467608641b252513bb687db345eac9c94f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
%% 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 <typography dot with dot latex at gmail dot com>
%% -------------------------------------------------------------------------------------------
%%
%% 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}