Debian Codegraph @

texmf-dist/doc/latex/cntformats/cntformats_en.tex

raw
1 % arara: pdflatex
2 % !arara: biber
3 % !arara: pdflatex
4 % arara: pdflatex
5 % --------------------------------------------------------------------------
6 % the CNTFORMATS package
7 %
8 % A different way to read counters
9 %
10 % --------------------------------------------------------------------------
11 % Clemens Niederberger
12 % Web: https://github.com/cgnieder/cntformats/
13 % E-Mail: [email protected]
14 % --------------------------------------------------------------------------
15 % Copyright 2012-2014 Clemens Niederberger
16 %
17 % This work may be distributed and/or modified under the
18 % conditions of the LaTeX Project Public License, either version 1.3
19 % of this license or (at your option) any later version.
20 % The latest version of this license is in
21 % http://www.latex-project.org/lppl.txt
22 % and version 1.3 or later is part of all distributions of LaTeX
23 % version 2005/12/01 or later.
24 %
25 % This work has the LPPL maintenance status `maintained'.
26 %
27 % The Current Maintainer of this work is Clemens Niederberger.
28 % --------------------------------------------------------------------------
29 % If you have any ideas, questions, suggestions or bugs to report, please
30 % feel free to contact me.
31 % --------------------------------------------------------------------------
32 \documentclass[load-preamble+]{cnltx-doc}
33 \usepackage{cntformats}
34
35 \setcnltx{
36 package = {cntformats} ,
37 authors = Clemens Niederberger ,
38 email = {[email protected]} ,
39 url = {https://github.com/cgnieder/cntformats/} ,
40 info = {A different way to read counters} ,
41 add-cmds = {
42 @cntfmts@parsed@pattern, AddCounterPattern,
43 eSaveCounterPattern,
44 NewCounterPattern, NewPatternFormat,
45 ReadCounterFrom, ReadCounterPattern, ReadCounterPatternFrom,
46 SaveCounterPattern
47 } ,
48 add-silent-cmds = {
49 alphalph,
50 cs,
51 ExplSyntaxOff, ExplSyntaxOn,
52 int,
53 myoddnumber,
54 numexpr, ordinalnum,
55 relax,
56 somesetupcommand,
57 tmpa, tmpb, the
58 }
59 }
60
61 \defbibheading{bibliography}[\bibname]{\addsec{#1}}
62
63 \newcommand*\at{\cnltxat}
64
65 \usepackage[biblatex]{embrac}[2012/06/29]
66 \ChangeEmph{[}[,.02em]{]}[.055em,-.08em]
67 \ChangeEmph{(}[-.01em,.04em]{)}[.04em,-.05em]
68
69 \usepackage{booktabs,array,ragged2e}
70 \usepackage{subcaption}
71
72 % ----------------------------------------------------------------------------
73 % example definitions that have to be done in the preamble:
74 \usepackage{alphalph}
75 \NewPatternFormat{aa}{\alphalph}
76 \usepackage{fmtcount}
77 \NewPatternFormat{o}{\ordinalnum}
78 \newcommand*\myoddnumber[1]{\the\numexpr2*(#1)-1\relax}
79 \NewPatternFormat{x}{\myoddnumber}
80 \NewCounterPattern{test}{t}
81
82 \begin{document}
83
84 \section{Motivation}
85 \cntformats\ provides a way to format counters with what I will call patterns.
86 This does not in any way effect the usual \LaTeXe\ way of treating counters
87 and does not use \cs*{the\meta{counter}} nor is it affected by the
88 redefinition of them.
89
90 This package is aimed at package or class authors and probably not very useful
91 for document authors.
92
93 When I first had the idea for this package the idea of what it does already
94 existed as part of the \pkg{exsheets}~\cite{bnd:exsheets} package. I can't
95 recall why I came up with the idea in the first place or why I originally
96 wanted a new syntax for formatting the \code{question} counter. I am also not
97 convinced any more that it is a good idea. Anyway, here we are.
98
99 Until and including v0.6\changedversion{0.7} \cntformats{} used to be part of
100 the \pkg{exsheets} bundle. Since version 0.7 it is distributed as a package
101 of its own.
102
103 \section{License and Requirements}\label{sec:license}
104 \license
105
106 \cntformats\ requires the \needpackage{etoolbox} package~\cite{pkg:etoolbox}
107 and the \pkg*{cnltx-base}\footnote{\CTANurl{cnltx}} package~\cite{bnd:cnltx}.
108
109 \section{Example}
110 A use case typically looks as follows:
111 \begin{example}[side-by-side]
112 \ReadCounterPattern{se.sse}
113 \end{example}
114 where the key \code{se} stands for the current value of the \code{section}
115 counter and \code{sse} for \code{subsection}, respectively. \code{se.sse} is
116 an example for what will be called \emph{pattern}. The keys for the counters
117 can have optional arguments that specify the format:
118 \begin{example}[side-by-side]
119 \stepcounter{subsection}
120 \ReadCounterPattern{se[A](sse[r])}
121 \end{example}
122 \code{A} stands for \cs*{Alph} and \code{r} for \cs*{roman}. A complete list
123 can be found in table~\ref{tab:predefined:formats} on
124 page~\pageref{tab:predefined:formats}. As you can see you can insert
125 arbitrary other tokens in a pattern that won't be changed. It is important to
126 notice, though, that the patterns are only replaced if they're \emph{not}
127 placed in a braced group!
128
129 \begin{example}[side-by-side]
130 \ReadCounterPattern{{se[A](sse[r])}}
131 \end{example}
132
133 I would imagine that the argument to \cs{ReadCounterPattern} is usually
134 supplied by a user setting an option \ldots
135 \begin{sourcecode}
136 \somesetupcommand{
137 counter-format = se[A](sse[r])
138 }
139 \end{sourcecode}
140 \ldots{} and then internally used by the corresponding package or class.
141
142
143 \section{Usage}
144 In the following description of the available commands the symbol
145 \textcolor{expandable}{\expandablesign} means that the command is expandable.
146
147 In order to make counters known to \cntformats\ the following commands are used:
148 \begin{commands}
149 \command{AddCounterPattern}[\sarg\oarg{module}\marg{counter}\marg{pattern}]
150 This command will make the (existing) counter \meta{counter} known to
151 \cntformats\ and assign the pattern \meta{pattern} to it.
152 \command{NewCounterPattern}[\sarg\oarg{module}\marg{counter}\marg{pattern}]
153 This command will create a new counter \meta{counter}, make it known to
154 \cntformats\ and assign the pattern \meta{pattern} to it.
155 \command{ReadCounterFrom}[\oarg{module}\marg{counter}\marg{internal cmd}]
156 If you use one of the commands above with the starred version the number
157 for the pattern is \emph{not automatically fetched} from the internal
158 \cs*{c@\meta{counter}}. This can (and must) now be assigned with
159 \cs{ReadCounterFrom} where \meta{internal cmd} is the macro that holds
160 the number.
161 \end{commands}
162 The commands above can only be used in the document preamble.
163
164 After the creation of these pattern markers one wants to be able to use them.
165 There are a number of macros that allow different aspects of usage.
166 \begin{commands}
167 \command{ReadCounterPattern}[\oarg{module}\marg{pattern}]
168 Reads, interprets and prints a pattern.
169 \expandable\command{\at cntfmts\at parsed\at pattern}
170 After \cs{ReadCounterPattern} has been used the current pattern
171 interpretation is stored in this macro. The \emph{interpretation} is
172 \emph{not} what is printed. See the examples below for details.
173 \command{ReadCounterPatternFrom}[\oarg{module}\marg{macro that holds
174 pattern}]
175 Reads, interprets and prints a pattern that's stored in a macro.\\
176 Otherwise the same as \cs{ReadCounterPattern}.
177 \command{SaveCounterPattern}[\marg{cmd a}\marg{cmd b}\marg{pattern}]
178 Saves the \meta{pattern} in \meta{cmd a} and the interpreted pattern in
179 \meta{cmd b}.
180 \command{eSaveCounterPattern}[\oarg{module}\marg{cmd a}\marg{cmd
181 b}\marg{pattern}]
182 Saves the \meta{pattern} in \meta{cmd a} and the expanded pattern in
183 \meta{cmd b}.
184 \command{SaveCounterPatternFrom}[\oarg{module}\marg{cmd a}\marg{cmd
185 b}\marg{macro that holds pattern}]
186 Like \cs{SaveCounterPattern} but reads the pattern from a macro.
187 \command{eSaveCounterPatternFrom}[\oarg{module}\marg{cmd a}\marg{cmd
188 b}\marg{macro that holds pattern}]
189 Like \cs{eSaveCounterPattern} but reads the pattern from a macro.
190 \end{commands}
191
192 The optional argument \meta{module} should be specific for a package, say, so
193 that different patterns for the \code{section} counter for example don't
194 interfer with each other. This means: \cs{ReadCounterPattern} and friends
195 \emph{only} read the patterns known to the specified module! If you leave the
196 argument the default module \code{cntfmts} is used.
197
198 The \pkg{exsheets} package uses the commands with the module \code{exsheets}.
199 You can find the following lines in \pkg{exsheets}' code:
200 \begin{sourcecode}
201 \AddCounterPattern*[exsheets]{section}{se}
202 \ReadCounterFrom[exsheets]{section} \l__exsheets_counter_sec_int
203 \NewCounterPattern*[exsheets]{question}{qu}
204 \ReadCounterFrom[exsheets]{question} \l__exsheets_counter_qu_int
205 \end{sourcecode}
206
207 Another example would be the \pkg{tasks} package~\cite{pkg:tasks}:
208 \begin{sourcecode}
209 \NewCounterPattern*[tasks]{task}{tsk}
210 \ReadCounterFrom[tasks]{task} \g__tasks_int
211 \end{sourcecode}
212
213 In fact \pkg{exsheets} loads the \pkg{tasks} package and also does the
214 following
215 \begin{sourcecode}
216 \AddCounterPattern*[tasks]{question}{qu}
217 \ReadCounterFrom[tasks]{question}\l__exsheets_counter_qu_int
218 \end{sourcecode}
219 so \pkg{tasks} knows about the question counter and uses the same pattern as
220 \pkg{exsheets} does.
221
222 Now let's see a short example that hopefully explains what the other macros
223 do:
224 \begin{example}
225 % preamble
226 % \NewCounterPattern{test}{t}
227 \setcounter{test}{11}
228 \ReadCounterPattern{t}
229 \ReadCounterPattern{t[a]} \\
230 \ttfamily\makeatletter
231 \meaning\@cntfmts@parsed@pattern
232
233 \bigskip
234 \SaveCounterPattern\tmpa\tmpb{t[a]}
235 \meaning\tmpa \\
236 \meaning\tmpb
237
238 \bigskip
239 \eSaveCounterPattern\tmpa\tmpb{t[a]}
240 \meaning\tmpa \\
241 \meaning\tmpb
242 \end{example}
243 You can see that somehow an additional \cs*{@empty} found its way into the
244 interpreted pattern. This is due to the fact that reading optional arguments
245 expandably isn't easy and must have some safety net. In this case looking for
246 an optional argument is done by reading the token following the command as
247 argument. If there is no option then \cs*{@empty} is found so no other tokens
248 provided by users get read which might otherwise lead to strange
249 effects/outcome/errors\footnote{Imagine for example there would be \emph{no}
250 token following the command -- nasty error messages could follow.}. On the
251 other hand the grabbing of the next token can lead to spaces after the pattern
252 being ignored, see the next example: if there is no optional argument then the
253 next token is grabbed ignoring the spaces as is normal for a \TeX{} macro.
254 The next example demonstrates this.
255
256 A word of caution: although I used a one-letter pattern in the above example I
257 would not recommend this. The pattern should consist of two or more
258 characters otherwise it might get inconvenient hiding the characters that you
259 \emph{don't} want replaced:
260 \begin{example}
261 \setcounter{test}{3}
262 Where is the first space: \ReadCounterPattern{t t[r] t}?\par
263 \ReadCounterPattern{every instance of the letter t{} is replaced}
264 \end{example}
265
266 \section{Predefined and New Patterns and Format Keys}
267 \subsection{Predefined Patterns and Format Keys}
268 \cntformats\ predefines a number of pattern keys. These are listed in
269 table~\ref{tab:predefined:patterns}.
270
271 \subsection{New Patterns and Format Keys}
272 Table~\ref{tab:predefined:formats} lists the predefined formats. If you want
273 you can add own formats.
274 \begin{commands}
275 \command{NewPatternFormat}[\marg{pattern}\marg{format}]
276 \meta{format} is a number presentation command like \cs*{@alph}, \ie,
277 it needs a mandatory argument that takes a number. It is used in
278 \meta{format} \emph{without} its argument. This command can only be used
279 in the preamble.
280 \end{commands}
281
282 \begin{table}
283 \centering
284 \caption{Predefined Patterns and Format Keys.}
285 \addtocounter{table}{-1}% necessary fix for subcaption and KOMA
286 \begin{minipage}{.48\linewidth}
287 \centering
288 \subcaption{Predefined Patterns for the module \code{cntfmts}.}
289 \label{tab:predefined:patterns}
290 \begin{tabular}{>{\ttfamily}l>{\ttfamily}l}
291 \toprule
292 \normalfont\bfseries counter & \normalfont\bfseries pattern \\
293 \midrule
294 chapter & ch \\
295 section & se \\
296 subsection & sse \\
297 subsubsection & ssse \\
298 paragraph & pg \\
299 \bottomrule
300 \end{tabular}
301 \end{minipage}
302 \begin{minipage}{.48\linewidth}
303 \centering
304 \subcaption{Predefined Format Keys}
305 \label{tab:predefined:formats}
306 \begin{tabular}{>{\ttfamily}ll}
307 \toprule
308 \normalfont\bfseries key & \normalfont\bfseries format \\
309 \midrule
310 1 & \cs*{arabic} \\
311 a & \cs*{alph} \\
312 A & \cs*{Alph} \\
313 r & \cs*{roman} \\
314 R & \cs*{Roman} \\
315 \bottomrule
316 \end{tabular}
317 \end{minipage}
318 \end{table}
319
320 Here are now a few examples of possible new patterns. Suppose the following code
321 in the preamble:
322 \begin{sourcecode}
323 \usepackage{alphalph,fmtcount}
324 \newcommand*\myoddnumber[1]{\the\numexpr2*(#1)-1\relax}
325
326 \NewPatternFormat{aa}{\alphalph}
327 \NewPatternFormat{o}{\ordinalnum}
328 \NewPatternFormat{x}{\myoddnumber}
329
330 \newcounter{test}
331 \NewCounterPattern{test}{t}
332 \setcounter{test}{4}
333 \end{sourcecode}
334
335 Then we can use the new pattern and the new formats as
336 follows:\setcounter{test}{4}
337 \begin{example}
338 \ReadCounterPattern{t[aa]}
339 \ReadCounterPattern{t[o]}
340 \ReadCounterPattern{t[x]}
341 \end{example}
342
343 \end{document}