Debian Codegraph @

texmf-dist/source/latex/padcount/padcount.dtx

raw
1 % \iffalse
2 % +AMDG This document was begun on 1 May 1202, the
3 % feast of St. Joseph the Worker, and it
4 % is humbly dedicated to him and to the Immaculate Heart of
5 % Mary for their prayers, and to the Sacred Heart of Jesus
6 % for His mercy.
7 %
8 % This document is copyright 2018 by Donald P. Goodman, and is
9 % released publicly under the LaTeX Project Public License. The
10 % distribution and modification of this work is constrained by the
11 % conditions of that license. See
12 % http://www.latex-project.org/lppl.txt
13 % for the text of the license. This document is released
14 % under version 1.3c of that license, and this work may be distributed
15 % or modified under the terms of that license or, at your option, any
16 % later version.
17 %
18 % This work has the LPPL maintenance status 'maintained'.
19 %
20 % The Current Maintainer of this work is Donald P. Goodman
21 % ([email protected]).
22 %
23 % This work consists of padcount.dtx, padcount.ins, and
24 % derived files padcount.sty and padcount.pdf.
25 % \fi
26
27 % \iffalse
28 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
29 %<package>\ProvidesPackage{padcount}[2018/05/04 v1.0 Pad numbers with arbitrary characters]
30 %<*driver>
31 \documentclass{ltxdoc}
32
33 \usepackage{doc}
34 \usepackage{padcount}
35 \usepackage{fancyvrb}
36 \usepackage{fvrb-ex}
37 \usepackage{booktabs}
38
39 \begin{document}
40 \DocInput{padcount.dtx}
41 \end{document}
42 %</driver> \fi
43 %
44 % \title{The |padcount| Package, v1.0}
45 % \author{Donald P.\ Goodman III}
46 % \date{\today}
47 %
48 % \maketitle
49 %
50 % \begin{abstract}
51 % \noindent
52 % Sometimes a plain number is fine; other times, you want it
53 % \emph{padded}, either with zeroes or some other character.
54 % This small package makes it easy to do that. It works not
55 % only on
56 % arabic numerals, but on any expanded list of tokens passed
57 % to it. This makes it suitable for, among other things,
58 % counters of all kinds.
59 % \end{abstract}
60 %
61 % \section{Usage}
62 %
63 % About as simply as possible, the package centers on the
64 % \DescribeMacro{\padnum}|\padnum| macro, which is defined
65 % as follows:
66 %
67 % \begin{quote}
68 % \cmd{\padnum} \marg{stuff}
69 % \end{quote}
70 %
71 % The argument can be anything that \TeX\ will recognize as
72 % a list of tokens, though it's anticipated that it'll be
73 % used mostly for numbers. By default,
74 % |padcount| pads to two digits, and it does that
75 % padding with the character |0|.
76 %
77 % The examples in this document assume the existence of a
78 % \TeX\ count register |\tmp| (created by |\newcount\tmp|)
79 % and a \LaTeX\ counter |temp| (created by
80 % |\newcounter{temp}|).
81 %
82 % \newcount\tmp
83 % \newcounter{temp}
84 % \begin{center}\tabcolsep=0pt
85 % \begin{tabular}{p{2in}lp{0.4in}p{2in}l}
86 % |\padnum{4}| & \padnum{4} & {} & |\padnum{47}| & \padnum{47} \\
87 % |\tmp=8\padnum{\the\tmp}| & \tmp=8\padnum{\the\tmp} & {} &
88 % |\tmp=16\padnum{\the\tmp}| & \tmp=16\padnum{\the\tmp} \\
89 % |\setcounter{temp}{8}| |\padnum{\thetemp}| &
90 % \setcounter{temp}{8}\padnum{\thetemp} & {} &
91 % |\setcounter{temp}{16}| |\padnum{\thetemp}| &
92 % \setcounter{temp}{16}\padnum{\thetemp} \\
93 % \end{tabular}
94 % \end{center}
95 %
96 % You can change the number of digits you'd like to pad to
97 % with the macro \DescribeMacro{\setpadnum}|\setpadnum|,
98 % which takes a single integer as its argument. By default,
99 % this is |2|; but any integer whatsoever will work.
100 %
101 % \begin{center}\tabcolsep=0pt
102 % \begin{tabular}{p{2in}r}
103 % |\setpadnum{4}\padnum{4}| & \setpadnum{4}\padnum{4} \\
104 % |\setpadnum{8}\padnum{42424}| & \setpadnum{8}\padnum{42424} \\
105 % \end{tabular}
106 % \end{center}
107 %
108 % You can set the character that will be used for padding
109 % with \DescribeMacro{\setpadchar}|\setpadchar|. In fact,
110 % this need not be a character; it can be any expansible set
111 % of tokens.
112 %
113 % \begin{center}\tabcolsep=0pt
114 % \begin{tabular}{p{3.65in}r}
115 % |\setpadnum{4}\setpadchar{*}=\padnum{4}=| &
116 % \setpadnum{4}\setpadchar{*}=\padnum{4}= \\
117 % |\setpadnum{4}\setpadchar{\hskip1.2ex}=\padnum{4}=| &
118 % \setpadnum{4}\setpadchar{\hskip1.2ex}=\padnum{4}= \\
119 % |\setpadnum{4}\setpadchar{*}=\padnum{\roman{temp}}=| &
120 % \setpadnum{4}\setpadchar{*}=\padnum{\roman{temp}}= \\
121 % \end{tabular}
122 % \end{center}
123 %
124 % That last example, incidentally, also demonstrates that we
125 % can send a counter in any format, without regard to base,
126 % and |padcount| will do its job.
127 %
128 % And that's it; just a small utility package. Hope it's
129 % useful in some way.
130 %
131 % \section{Implementation}
132 %
133 % We start with the macros to count characters, which we've
134 % gently adapted from those of David Carlisle's answer on
135 % \url{tex.stackexchange.com}, question 57598.
136 % \begin{macrocode}
137 \newcount\pad@charcount%
138 \def\pad@expandloop#1{%
139 \pad@xloop#1\relax
140 }
141 \def\pad@xloop#1{%
142 \ifx\relax#1%
143 \else%
144 \advance\pad@charcount by1%
145 \expandafter\pad@xloop%
146 \fi%
147 }%
148 \def\pad@countchar#1{%
149 \pad@charcount=0%
150 \expandafter\expandafter\pad@expandloop{#1}%
151 }%
152 % \end{macrocode}
153 % Now, we can set the defaults. Set the padding level to
154 % two characters and the padding character to |0|. Also
155 % define some throwaway counters for loops and
156 % value-holding.
157 % \begin{macrocode}
158 \newcount\pad@tmp%
159 \newcount\pad@tmpi%
160 \newcount\pad@num\pad@num=2%
161 \def\pad@char{0}%
162 % \end{macrocode}
163 % Now, the money: count the characters, determine how many
164 % new characters we need to print, then loop the appropriate
165 % number of times to print them.
166 % \begin{macrocode}
167 \def\padnum#1{%
168 \edef\pad@yy{#1}%
169 \expandafter\pad@countchar{\pad@yy}%
170 \pad@tmp=\pad@charcount%
171 \multiply\pad@tmp by-1%
172 \advance\pad@tmp by\pad@num%
173 \pad@tmpi=0%
174 \ifnum\pad@tmp>0%
175 \loop%
176 \ifnum\pad@tmpi<\pad@tmp%
177 \pad@char%
178 \advance\pad@tmpi by1%
179 \repeat%
180 \fi%
181 \leavevmode#1%
182 }%
183 % \end{macrocode}
184 % Lastly, some macros to reset the internal values used by
185 % |\padnum|.
186 % \begin{macrocode}
187 \def\setpadnum#1{\pad@num=#1}%
188 \def\setpadchar#1{\def\pad@char{#1}}%
189 % \end{macrocode}
190 % Happy \TeX{}ing!