default.sty (14141B)
1 % DEFAULT.STY, version 1.3 July 1998, provides 2 % 1. macro parameter-defaulting mechanism. 3 % 2. user stack and dynamic array 4 % Particularly useful if you have macros with many parameters, and you 5 % don't want to remember all of them all the time. 6 % 7 % Author: Zhuhan Jiang, University of New England, Australia 8 % Email: zhuhan@neumann.une.edu.au 9 % 10 % NOTES: 11 % 1. This style file can be loaded in as many times as you want, 12 % without overrunning the resources for such as counter 13 % registers and etc. So different packages may all use it to 14 % default their macro parameters, without causing any conflict. 15 % 2. Macro parameters that can be defaulted through this style file 16 % are those included in square brackets. E.g. #1, #2, #3 in 17 % \foo[#1][#2][#3]#4#5 are defaultable while #4 and #5 here are 18 % not. 19 % 3. If \foo is a macro to be defaulted, the original macro \foo 20 % will be renamed as ``\foo@ raw'' (yes, there is a space) and 21 % the new macro \foo will be defined through the use of the 22 % original ``\foo@ raw''. 23 % 24 % MAIN MACRO: 25 % \setdefalut{cmdname}{n}{default1}{..}{default_n} 26 % - gives defaults to \cmdname[p_1][p_2]..[p_n] 27 % (the macro to be defaulted must be already defined 28 % when \setdefault is executed) 29 % EXAMPLES: 30 % 1. Suppose \foo is defined for \foo[#1][#2][#3]{#4}, we may default 31 % all (or some of) the parameters in the SQUARE BRACKETS to for 32 % instance 'one', 'two' and 'three' via 33 % \setdefault{foo}{3}{one}{two}{three} 34 % This way, command \foo{stuff} or \foo[]{stuff} or \foo[][]{stuff} 35 % or \foo[][][]{stuff} will all be translated as 36 % \foo[one][two][three]{stuff}. Command \foo[][2]{stuff} will be 37 % translated as \foo[one][2][three]{stuff}. 38 % 2. See how \placebox is defined and redefined via \setdefault 39 % in this file. 40 % 41 % TIP: 42 % The best strategy to define (e.g.) \foo[#1][#2]{#3} so that 43 % it has defaults for #1 and #2 44 % 1. define \foo@[#1][#2]#3 as normal (what you wish) 45 % 2. define \foo[#1][#2]#{\foo@[#1][#2]} to make {#3} mandatory 46 % 3. use \setdefault{foo}{2}{one}{two} to default #1=one and #2 to two 47 % 48 % MINOR MACROS: 49 % \placebox[#1][#2][#3][#4][#5][#6]{#7} 50 % - place stuff #7 in a box at the position specified 51 % by parameters #1, ..., #6. (move mini page around) 52 % #1: box position (default 0) 53 % 1--6--2 <-- top of the box 54 % | | 55 % | | 56 % | | 57 % 9 5 7 0 is -5 <-- middle height of the box 58 % | | 59 % 10 11 12 <-height up <-- baseline 60 % | | depth down 61 % 4--8--3 <-- bottom of the box 62 % #1<=0: box has no width, no height 63 % #2: x shift (default 0pt) 64 % #3: y shift (default 0pt) 65 % #4,#5: extra x,y shift in multiple of box width/10, height/10 66 % #6: box type: v=vbox, otherwise=hbox 67 % #7: content to be boxed 68 % \push{ ... } - push stuff to stack; anything can be saved to 69 % \pop - pop stack 70 % \popnil - delete stack top element (pop to nil) 71 % \fetch{#1} - fetch #1-th entry from the stack 72 % (illegal number #1 will produce \relax) 73 % if #1>0, fetch #1-th entry from bottom of stack 74 % if #1>=0, fetch (-#1)-th element from top of stack 75 % e.g. 76 % fetch{0} - fetch the element of the top of stack 77 % fetch{-1} - the element just below the top of stack 78 % fetch{2} - 2nd element from the bottom of stack 79 % 80 \ifx\EntryDefault\undefined 81 \message{1.3 13/7/98}% 82 {\catcode`\@=11\relax \def\temp@macro{\space\space\space\space}% 83 \wlog{\temp@macro DEFAULT.STY version 1.3 July 1998 By Z Jiang}% 84 \wlog{\temp@macro University of New England, Australia}% 85 \wlog{\temp@macro Email: zhuhan@neumann.une.edu.au}}% 86 \fi 87 \xdef\EntryDefault{\the\catcode`\@}\catcode`\@=11\relax % SAVE @'s STATUS 88 % 89 % 90 % NEW MEASURES etc (parameter defaulting mechanism given by macros below) 91 \def\new@measures{% 92 \def\temp@@macro##1##2##3{\expandafter\ifx\csname ##3temp##2##1\endcsname 93 \relax\edef\next{\noexpand\csname new##1\noexpand\endcsname 94 \csname ##3temp##2##1\endcsname}% 95 \else\let\next\relax\fi\next}% 96 \def\temp@macro##1{% 97 \temp@@macro{##1}{@}{}\temp@@macro{##1}{@@}{}\temp@@macro{##1}{@@@}{}}% 98 \temp@macro{count}\temp@macro{toks}\temp@macro{box}\temp@macro{read}% 99 \temp@@macro{if}{@}{if}\temp@@macro{if}{@@}{if}\temp@@macro{if}{@@@}{if}% 100 \def\temp@@macro##1{\expandafter\ifx\csname temp##1dim\endcsname 101 \relax\edef\next{\noexpand\csname newdimen\noexpand\endcsname 102 \csname temp##1dim\endcsname}% 103 \else\let\next\relax\fi\next}% 104 \temp@@macro{@}\temp@@macro{@@}\temp@@macro{@@@}% 105 }% 106 \let\temp@@@macro=\wlog\def\wlog#1{}\new@measures 107 % 108 %%% GENERAL STACK 109 \def\make@STKcount{\csname newcount\endcsname 110 \STKcount\global\STKcount=0\relax}% 111 \ifx\STKcount\undefined@ 112 \def\next{\make@STKcount}\else\def\next{}\fi 113 \next % ensures stack pointer not flushed if this piece of code 114 % is loaded again 115 % 116 \long\def\push#1{\global\advance\STKcount1\relax 117 \expandafter\gdef\csname STK\the\STKcount\string~\endcsname{#1}}% 118 % 119 \def\popnil{\expandafter\let\expandafter\temp@macro 120 \csname STK\the\STKcount\string~\endcsname 121 \ifnum\STKcount>0\global\expandafter\let 122 \csname STK\the\STKcount\string~\endcsname=\undefined@ 123 \global\advance\STKcount-1% 124 \else 125 \def\temp@macro{}\global\STKcount=0% 126 \fi\relax % \temp@macro for pop 127 }% 128 % 129 \def\pop{\popnil\temp@macro}% 130 % 131 \def\fetch@#1{\ifnum#1>0 \relax \temp@count=#1 \else 132 \temp@count=\STKcount \advance\temp@count by #1 \fi 133 \csname STK\the\temp@count\string~\endcsname 134 }% 135 \def\fetch#{\fetch@}% 136 % 137 %% GET PARAMETERS 138 \long\def\get@nepara[#1][#2]{{\def\next@{#2}% 139 \ifx\next@\empty\push{#1}\else\push{#2}\fi}\ag@in}% 140 % 141 \long\def\get@para\left@#1\right@{% 142 \def\check@{% 143 \ifx[\next@ \def\full@####1{\get@nepara[#1]####1}% 144 \else \def\full@{\get@nepara[#1][#1]}\fi 145 \full@}% 146 \futurelet\next@\check@}% 147 % 148 \long\def\do@nepara\left@#1\right@#2\p@r@end{% 149 \gdef\p@r@data{#2}\global\advance\p@r@count1\get@para\left@#1\right@}% 150 % 151 \def\ag@in{\ifx\p@r@data\empty \def\next@{\relax 152 \getp@r@s\run@CMD}\else 153 \def\next@{\expandafter\do@nepara\p@r@data\p@r@end}\fi 154 \next@}% 155 % 156 \def\run@CMD{\csname STK\the\STKcount\string~\endcsname}% 157 % 158 \def\num@to@word#1{\ifcase #1 \or 159 \def\temp@macro{one}\or \def\temp@macro{two}\or 160 \def\temp@macro{three}\or \def\temp@macro{four}\or 161 \def\temp@macro{five}\or \def\temp@macro{six}\or 162 \def\temp@macro{seven}\or \def\temp@macro{eight}\or 163 \def\temp@macro{nine}\else \def\temp@macro{}\fi 164 \temp@toks=\expandafter{\temp@macro}}% 165 % 166 \temp@@count=1 % make nine newtoks, if they don't already exist 167 \loop \num@to@word{\temp@@count}% 168 \expandafter\ifx\csname p@r@\the\temp@toks\endcsname\relax 169 \temp@@toks={newtoks}\expandafter\csname\expandafter\the 170 \expandafter\temp@@toks\expandafter\endcsname 171 \csname p@r@\the\temp@toks\endcsname\fi 172 \advance\temp@@count by 1 \ifnum\temp@@count<10 \repeat 173 % 174 \ifx\p@r@count\undefined@ \csname newcount\endcsname\p@r@count\fi 175 \long\def\st@ckparas#1\p@r@end{% 176 \global\p@r@count=0\gdef\p@r@data{#1}\ag@in}% 177 % 178 % PARAMETER ASSIGNMENT % use \temp@count 179 \def\getp@r@s{\temp@count=\p@r@count 180 {\loop 181 \ifnum\temp@count>0 % 182 \expandafter\let\expandafter 183 \temp@macro\csname STK\the\STKcount\string~\endcsname 184 \ifcase\temp@count 185 \or \global\p@r@one=\expandafter{\temp@macro}% 186 \or \global\p@r@two=\expandafter{\temp@macro}% 187 \or \global\p@r@three=\expandafter{\temp@macro}% 188 \or \global\p@r@four=\expandafter{\temp@macro}% 189 \or \global\p@r@five=\expandafter{\temp@macro}% 190 \or \global\p@r@six=\expandafter{\temp@macro}% 191 \or \global\p@r@seven=\expandafter{\temp@macro}% 192 \or \global\p@r@eight=\expandafter{\temp@macro}% 193 \or \global\p@r@nine=\expandafter{\temp@macro}% 194 \else \errmessage{Parameter capacity exceeded.}% 195 % this should never happen: TeX's max para no. is 9 196 \fi 197 \global\expandafter\let 198 \csname STK\the\STKcount\string~\endcsname=\undefined@% 199 \global\advance\STKcount-1% 200 \global\advance\temp@count-1\relax 201 \fi 202 \ifnum\temp@count>0 % 203 \repeat}}% 204 % 205 \def\clrp@r@s{%GLOBALLY clear 206 \global\p@r@one={}\global\p@r@two={}\global\p@r@three={}% 207 \global\p@r@four={}\global\p@r@five={}\global\p@r@six={}% 208 \global\p@r@seven={}\global\p@r@eight={}\global\p@r@nine={}}% 209 % 210 \def\read@paras#1{\temp@@@count=1 %use \temp@@@count and \temp@@count 211 \loop \num@to@word{\the\temp@@@count}\csname p@r@\the\temp@toks\endcsname={}% 212 \advance\temp@@@count by 1 \ifnum\temp@@@count<10 \repeat 213 \temp@@@count=1 \temp@@count=#1 % 214 \def\read@one@para##1{\num@to@word{\temp@@@count}% 215 \def\temp@macro{\left@ ##1\right@}% 216 \temp@@@toks=\expandafter{\temp@macro}% 217 \csname p@r@\the\temp@toks\endcsname\expandafter{\temp@macro}% 218 \advance\temp@@@count by 1 \read@continue}% 219 \def\read@continue{\num@to@word{\temp@@@count}% which uses \temp@macro 220 \ifnum\temp@@@count>\temp@@count \let\next@@\make@default 221 \edef\temp@@macro{\the\p@r@one\the\p@r@two\the\p@r@three\the\p@r@four 222 \the\p@r@five\the\p@r@six\the\p@r@seven\the\p@r@eight\the\p@r@nine}% 223 \else \let\next@@\read@one@para 224 \fi \next@@}\read@continue}% 225 % 226 \def\make@@default#1#2{\temp@count=#2\relax \temp@@count=1 %used \temp@@macro 227 \loop \num@to@word{\temp@@count}% 228 \ifnum\temp@@count>\temp@count \csname p@r@\the\temp@toks\endcsname={}% 229 \else \csname p@r@\the\temp@toks\endcsname 230 =\expandafter{\expandafter[\expandafter 231 \the\csname p@r@\the\temp@toks\endcsname]}\fi 232 \advance\temp@@count by 1 % 233 \ifnum\temp@@count<10 \repeat 234 \edef\temp@macro{\the\p@r@one\the\p@r@two\the\p@r@three\the\p@r@four 235 \the\p@r@five\the\p@r@six\the\p@r@seven\the\p@r@eight\the\p@r@nine}% 236 \temp@toks=\expandafter{\temp@macro}% 237 \temp@@toks=\expandafter{\temp@@macro}% 238 \expandafter\let\expandafter\temp@macro\csname #1\endcsname 239 \expandafter\ifx\csname #1@ raw\endcsname\relax 240 \expandafter\let\csname #1@ raw\endcsname\temp@macro\fi 241 \expandafter\edef\csname#1\endcsname{% 242 \noexpand\push{\noexpand\edef\noexpand\next@@{% 243 \noexpand\noexpand \noexpand \csname \noexpand#1@ raw\noexpand\endcsname 244 \the\temp@toks}% 245 \noexpand\popnil\noexpand\clrp@r@s\noexpand\next@@}\noexpand\st@ckparas 246 \the\temp@@toks \noexpand\p@r@end}}% 247 % 248 % \setdefalut{cmdname}{n}{default1}{..}{default_n} 249 % then \cmdname[#1][..][#n] can be defaulted. 250 \def\setdefault#1#2{\def\make@default{\make@@default{#1}{#2}}\read@paras{#2}} 251 % 252 % DEFAULT MECHANISM END: this piece of code can be included in any 253 % package and can be reloaded any times without overruning the resources. 254 % 255 % Now an example to use \setdefault: 256 % lower a box 257 \long\def\xylower#1#2#3{\bgroup\hskip#1\hbox{% #1=x #2=y #3=content 258 \ifmmode\let\SoftLower\undefined\else\def\SoftLower{}$\fi 259 \mathop{\vtop{\ialign{##\crcr \noalign{\kern#2}#3\crcr}}}% 260 \ifx\SoftLower\undefined\else $\fi}\egroup}% 261 % 262 % #1=box position(0) #2=x shift(0pt) #3=y shift(0pt) #6=v (vbox) 263 % #4,#5=extra x,y shift in multiple of box width/10, height/10 #7=content 264 \long\def\placebox[#1][#2][#3][#4][#5][#6]#7{\begingroup 265 \def\temp@macro{#6}\def\temp@@macro{v}\ifx\temp@macro\temp@@macro 266 \setbox\temp@box=\vbox{#7}\else \leavevmode % #1 (box position) 267 \setbox\temp@box=\hbox{#7}\fi\temp@dim=-\wd\temp@box % 1--6--2 268 \temp@@dim=\ht\temp@box \temp@@@dim=\dp\temp@box % | | 269 \ifnum#1<0 \temp@count=-#1 \else\temp@count=#1 \fi % | | 270 \ifnum\temp@count=0 \temp@count=5 \fi % | | 271 \ifcase\temp@count % 9 5 7 0 is -5 272 \or \temp@dim=0pt \temp@@dim=0pt % | | 273 \or \temp@@dim=0pt % 10 11 12 <-height up 274 \or \advance\temp@@dim by \temp@@@dim % | | depth down 275 \temp@@dim=-\temp@@dim % 4--8--3 276 \or \temp@dim=0pt \advance\temp@@dim by % 277 \temp@@@dim \temp@@dim=-\temp@@dim % -#1: box has no width etc 278 \or \divide\temp@dim by 2 \advance\temp@@@dim by \temp@@dim 279 \divide\temp@@@dim by 2 \temp@@dim=-\temp@@@dim 280 \or \divide\temp@dim by 2 \temp@@dim=0pt 281 \or \advance\temp@@dim by \temp@@@dim 282 \temp@@dim=-\temp@@dim \divide\temp@@dim by 2 283 \or \advance\temp@@dim by \temp@@@dim 284 \temp@@dim=-\temp@@dim \divide\temp@dim by 2 285 \or \temp@dim=0pt \advance\temp@@dim by \temp@@@dim 286 \temp@@dim=-\temp@@dim \divide\temp@@dim by 2 287 \or \temp@dim=0pt \advance\temp@@@dim by \temp@@dim \temp@@dim=-\temp@@dim 288 \or \divide\temp@dim by 2 289 \advance\temp@@@dim by \temp@@dim \temp@@dim=-\temp@@dim 290 \or \advance\temp@@@dim by \temp@@dim \temp@@dim=-\temp@@dim 291 \or \errmessage{Invalid range of first parameter: #1}\fi 292 \advance\temp@dim by #2 \advance \temp@@dim by #3 293 \temp@@@dim=\wd\temp@box \divide \temp@@@dim by 10 294 \multiply\temp@@@dim by #4 \advance\temp@dim by \temp@@@dim 295 \temp@@@dim=\ht\temp@box \advance\temp@@@dim by \dp\temp@box 296 \divide \temp@@@dim by 10 \multiply\temp@@@dim by #5 297 \advance\temp@@dim by \temp@@@dim 298 \setbox\temp@box=\hbox{\xylower{\temp@dim}{\temp@@dim}{\box\temp@box}}% 299 \ifnum#1>0 \else \wd\temp@box=0pt \ht\temp@box=0pt \dp\temp@box=0pt \fi 300 \box\temp@box\endgroup 301 }% 302 % 303 \ifx\setdefault\undefined\else\setdefault{placebox}{6}{0}{0pt}{0pt}{0}{0}{h}\fi 304 % example ends 305 % 306 \let\wlog\temp@@@macro\let\temp@@@macro\undefined@ 307 \catcode`\@=\EntryDefault\relax 308 \endinput 309 % 310 %ENDMACROS 311 %