-
Notifications
You must be signed in to change notification settings - Fork 0
/
numdef.sty
108 lines (83 loc) · 4.18 KB
/
numdef.sty
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
% Save file as: NUMDEF.STY Source: [email protected]
% numdef.sty
%%%%%%%%%%
% Prefixing a macro definition with \num allows numbers at the end of
% the command name ( At first glance )
% eg:
% \num\def\x1{x one}
% \num\def\x2{x two}
% \num\newcommand{\y100}[1]{y one hundred, with argument #1}
% David Carlisle
% NOTE: this is a LaTeX style option, but may be used with plain TeX.
% \catcode`\@=11 \input numdef.sty \catcode`\@=12
% SEE NOTES AT END OF FILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\num@def#1{\def\num@name{#1}\edef\num@str{\string #1}%
\expandafter\ifx\csname\num@str{*}\endcsname
\relax\num@start\fi\futurelet\next\num@d@f}%
\def\num@d@f{\ifx\next*\let\@tempa\num@defA
\else\ifx\next\bgroup\let\@tempa\num@defB
\else\let\@tempa\num@defC \fi\fi\@tempa}%
\newcount\num@count
\def\N{\number\num@count}%
\def\num@defA*{\def\num@{*}\num@def@}%
\def\num@defB#1{\let\num@=\N\num@count= #1 \num@def@}%
\def\num@defC{\let\num@=\N\afterassignment\num@def@\num@count= }%
\def\num@def@{\expandafter\num@define\csname\num@str{\num@}\endcsname}%
%%
\def\num#1#2{\let\num@define=#1\num@def#2}%
%%
\def\num@start{%
\expandafter\ifx\num@name\@undefined\let\next\num@st@rt
\else\def\next{\errmessage{numdef: \num@str\space is already defined}}%
\fi\next}%
\def\num@st@rt{\expandafter\def\csname\num@str{*}\endcsname
{\errmessage{numdef: \num@str\N\space is not defined}}%
\expandafter\num@use\num@name}%
\def\num@use#1{\def#1{\edef\num@str{\string #1}\futurelet\@tempa\num@us@}}
\def\num@us@{%
\ifx\@tempa\bgroup\let\next\num@useA\else\let\next\num@useB\fi\next}
\def\num@useA#1{\num@count= #1 \num@use@}%
\def\num@useB{\afterassignment\num@use@\num@count= }%
\def\num@use@{\expandafter\ifx\csname\num@str{\N}\endcsname\relax
\def\num@{*}\else\let\num@=\N\fi\csname\num@str{\num@}\endcsname}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
\num takes a liberal view of where to put { } pairs. In particular
the <number> and/or the command name may be surrounded by { }.
The token after \num must be a command for defining macros. If a \long
(or \outer) def is required, first define, eg, \def\ldef{\long\def} as
\num\long\def ... does NOT work.
All of the following definitions are equivalent:
\num\ldef\x10#1{x ten (#1)} \num\ldef\x{10}#1{x ten (#1)}
\num\ldef\x{"A}#1{x ten (#1)} \num\ldef\x'12#1{x ten (#1)}
\num\newcommand{\x10}[1]{x ten (#1)} \num\newcommand{\x{10}}[1]{x ten (#1)}
To use the macro defined via \num just type the name followed by the
<number>, optionally enclosed by { }.
eg \x10{abc} \x{10}{abc} \X"A{abc} all produce "x ten (abc)".
If the <number> is not surrounded by { }, the command name gobbles spaces
If there has not been a definition corresponding to the <number>, then a
default command is run, initially this produces an error message, but it
can be redefined using `*' instead of a <number> thus after one of the
above definitions \x2 would generate an error "\x2 is not defined"
If a default definition for \x<number> is required, use a definition
such as
\num\def\x*{the replacement text for x\N}
Here \N stands for the value of the <number>, so now \x2 expands to
"the replacement text for x2"
eg:
\num\def\poly1{degenerate case (n=1)} \num\def\poly2{degenerate case (n=2)}
\num\def\poly3{triangle} \num\def\poly4{quadrilateral}
\num\def\poly*{$\N$-gon}
Note that this example could more easily be achieved by
\def\poly#1{\ifcase #1\or degenerate case (n=1) \or ...\else $#1$-gon\fi}
\num is only really useful if one needs to have an `array' of macros and
need to update the definition of one macro in the array without altering
the expansions of the others.
Warning, if you use the * form to define a macro with arguments, make sure
that any macro that might change the value of \N is kept local to a { } group
eg
\num\def\z1{NOW} \num\def\y*#1{N is \N, #1\ N is \N}
\y3{\z1} % produces "N is 3 NOW N is 1" \z1 has made \N be 1
\y3{{\z1}}% produces "N is 3 NOW N is 3" changes to \N are local to a group.