summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/padcount/padcount.dtx
blob: 7da1eff746a1b98922873e3dd37549d0289be6ea (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
% \iffalse
% +AMDG  This document was begun on 1 May 1202, the
% feast of St. Joseph the Worker, and it
% is humbly dedicated to him and to the Immaculate Heart of
% Mary for their prayers, and to the Sacred Heart of Jesus 
% for His mercy.
%
% This document is copyright 2018 by Donald P. Goodman, and is
% released publicly under the LaTeX Project Public License.  The
% distribution and modification of this work is constrained by the
% conditions of that license.  See
% 	http://www.latex-project.org/lppl.txt
% for the text of the license.  This document is released
% under version 1.3c of that license, and this work may be distributed
% or modified under the terms of that license or, at your option, any
% later version.
% 
% This work has the LPPL maintenance status 'maintained'.
% 
% The Current Maintainer of this work is Donald P. Goodman
% (dgoodmaniii@gmail.com).
% 
% This work consists of padcount.dtx, padcount.ins, and
% derived files padcount.sty and padcount.pdf.
% \fi

% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{padcount}[2018/05/04 v1.0 Pad numbers with arbitrary characters]
%<*driver>
\documentclass{ltxdoc}

\usepackage{doc}
\usepackage{padcount}
\usepackage{fancyvrb}
\usepackage{fvrb-ex}
\usepackage{booktabs}

\begin{document}
\DocInput{padcount.dtx}
\end{document}
%</driver> \fi
%
% \title{The |padcount| Package, v1.0}
% \author{Donald P.\ Goodman III}
% \date{\today}
%
% \maketitle
%
% \begin{abstract}
% \noindent
% Sometimes a plain number is fine; other times, you want it
% \emph{padded}, either with zeroes or some other character.
% This small package makes it easy to do that.  It works not
% only on 
% arabic numerals, but on any expanded list of tokens passed
% to it.  This makes it suitable for, among other things,
% counters of all kinds.
% \end{abstract}
%
% \section{Usage}
%
% About as simply as possible, the package centers on the
% \DescribeMacro{\padnum}|\padnum| macro, which is defined
% as follows:
%
% \begin{quote}
% \cmd{\padnum} \marg{stuff}
% \end{quote}
%
% The argument can be anything that \TeX\ will recognize as
% a list of tokens, though it's anticipated that it'll be
% used mostly for numbers.  By default,
% |padcount| pads to two digits, and it does that
% padding with the character |0|.
%
% The examples in this document assume the existence of a
% \TeX\ count register |\tmp| (created by |\newcount\tmp|)
% and a \LaTeX\ counter |temp| (created by
% |\newcounter{temp}|).
%
% \newcount\tmp
% \newcounter{temp}
% \begin{center}\tabcolsep=0pt
% \begin{tabular}{p{2in}lp{0.4in}p{2in}l}
% |\padnum{4}| & \padnum{4} & {} & |\padnum{47}| & \padnum{47} \\
% |\tmp=8\padnum{\the\tmp}| & \tmp=8\padnum{\the\tmp} & {} &
%		|\tmp=16\padnum{\the\tmp}| & \tmp=16\padnum{\the\tmp} \\
% |\setcounter{temp}{8}| |\padnum{\thetemp}| &
%		\setcounter{temp}{8}\padnum{\thetemp} & {} &
%		|\setcounter{temp}{16}| |\padnum{\thetemp}| &
%		\setcounter{temp}{16}\padnum{\thetemp} \\
% \end{tabular}
% \end{center}
%
% You can change the number of digits you'd like to pad to
% with the macro \DescribeMacro{\setpadnum}|\setpadnum|,
% which takes a single integer as its argument.  By default,
% this is |2|; but any integer whatsoever will work.
%
% \begin{center}\tabcolsep=0pt
% \begin{tabular}{p{2in}r}
% |\setpadnum{4}\padnum{4}| & \setpadnum{4}\padnum{4} \\
% |\setpadnum{8}\padnum{42424}| & \setpadnum{8}\padnum{42424} \\
% \end{tabular}
% \end{center}
%
% You can set the character that will be used for padding
% with \DescribeMacro{\setpadchar}|\setpadchar|.  In fact,
% this need not be a character; it can be any expansible set
% of tokens.
%
% \begin{center}\tabcolsep=0pt
% \begin{tabular}{p{3.65in}r}
% |\setpadnum{4}\setpadchar{*}=\padnum{4}=| &
%		\setpadnum{4}\setpadchar{*}=\padnum{4}= \\
% |\setpadnum{4}\setpadchar{\hskip1.2ex}=\padnum{4}=| &
%		\setpadnum{4}\setpadchar{\hskip1.2ex}=\padnum{4}= \\
% |\setpadnum{4}\setpadchar{*}=\padnum{\roman{temp}}=| &
%		\setpadnum{4}\setpadchar{*}=\padnum{\roman{temp}}= \\
% \end{tabular}
% \end{center}
%
% That last example, incidentally, also demonstrates that we
% can send a counter in any format, without regard to base,
% and |padcount| will do its job.
%
% And that's it; just a small utility package.  Hope it's
% useful in some way.
%
% \section{Implementation}
%
% We start with the macros to count characters, which we've
% gently adapted from those of David Carlisle's answer on
% \url{tex.stackexchange.com}, question 57598.
%    \begin{macrocode}
\newcount\pad@charcount%
\def\pad@expandloop#1{%
	\pad@xloop#1\relax
}
\def\pad@xloop#1{%
	\ifx\relax#1%
	\else%
		\advance\pad@charcount by1%
	\expandafter\pad@xloop%
	\fi%
}%
\def\pad@countchar#1{%
	\pad@charcount=0%
	\expandafter\expandafter\pad@expandloop{#1}%
}%
%    \end{macrocode}
% Now, we can set the defaults.  Set the padding level to
% two characters and the padding character to |0|.  Also
% define some throwaway counters for loops and
% value-holding.
%    \begin{macrocode}
\newcount\pad@tmp%
\newcount\pad@tmpi%
\newcount\pad@num\pad@num=2%
\def\pad@char{0}%
%    \end{macrocode}
% Now, the money:  count the characters, determine how many
% new characters we need to print, then loop the appropriate
% number of times to print them.
%    \begin{macrocode}
\def\padnum#1{%
	\edef\pad@yy{#1}%
	\expandafter\pad@countchar{\pad@yy}%
	\pad@tmp=\pad@charcount%
	\multiply\pad@tmp by-1%
	\advance\pad@tmp by\pad@num%
	\pad@tmpi=0%
	\ifnum\pad@tmp>0%
		\loop%
			\ifnum\pad@tmpi<\pad@tmp%
				\pad@char%
				\advance\pad@tmpi by1%
		\repeat%
	\fi%
	\leavevmode#1%
}%
%    \end{macrocode}
% Lastly, some macros to reset the internal values used by
% |\padnum|.
%    \begin{macrocode}
\def\setpadnum#1{\pad@num=#1}%
\def\setpadchar#1{\def\pad@char{#1}}%
%    \end{macrocode}
% Happy \TeX{}ing!