| 1 |
\documentclass{ltxdoc} |
| 2 |
\usepackage[T1]{fontenc} |
| 3 |
\usepackage{tabularx} |
| 4 |
\usepackage{syntax} |
| 5 |
\usepackage{varioref} |
| 6 |
\usepackage{color} |
| 7 |
\usepackage{booktabs} |
| 8 |
\usepackage{alltt} |
| 9 |
\usepackage{textcomp} |
| 10 |
\usepackage[bookmarksopen=true]{hyperref} |
| 11 |
|
| 12 |
% Define this document's metadata. |
| 13 |
\def\ncfileversion{2.0} |
| 14 |
\def\ncfiledate{2010/06/01} |
| 15 |
|
| 16 |
\title{The \textsf{newcommand.py} utility\thanks{\textsf{newcommand.py} |
| 17 |
has version number \ncfileversion, last revised \ncfiledate.}} |
| 18 |
\author{\href{mailto:[email protected]}{Scott Pakin} \\ |
| 19 |
\href{mailto:[email protected]}{[email protected]}} |
| 20 |
\date{\ncfiledate} |
| 21 |
\hypersetup{% |
| 22 |
pdftitle={The newcommand.py utility}, |
| 23 |
pdfauthor={Scott Pakin <[email protected]>}, |
| 24 |
pdfsubject={Creating user-defined macros with more flexible argument |
| 25 |
processing}, |
| 26 |
pdfkeywords={LaTeX macros, optional arguments, newcommand, parenthesized |
| 27 |
arguments, starred commands, Python} |
| 28 |
} |
| 29 |
|
| 30 |
% Help prevent weird line breaks in URLs |
| 31 |
\def\UrlBreaks{} |
| 32 |
\def\UrlBigBreaks{\do/} |
| 33 |
|
| 34 |
% Define some useful shortcuts. |
| 35 |
\newcommand*{\ncpy}{\texttt{newcommand.py}} % The name of the script |
| 36 |
\newcommand*{\usercmd}[1]{\textcolor{blue}{#1}} % User command entry |
| 37 |
\newcommand{\prototype}[1]{{% % Prompt and user entry |
| 38 |
\bigskip |
| 39 |
\noindent |
| 40 |
\ttfamily\small\raggedright |
| 41 |
\hangafter=1\hangindent=4em |
| 42 |
~~~~\% Prototype:~\textcolor{blue}{#1}\strut\par |
| 43 |
\vspace*{-0.5\baselineskip}% |
| 44 |
}} |
| 45 |
|
| 46 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 47 |
|
| 48 |
\begin{document} |
| 49 |
\maketitle |
| 50 |
\sloppy |
| 51 |
|
| 52 |
\begin{abstract} |
| 53 |
\LaTeX's \cs{newcommand} is fairly limited in the way it processes |
| 54 |
optional arguments, but the \TeX\ alternative, a batch of \cs{def}s |
| 55 |
and \cs{futurelet}s, can be overwhelming to the casual \LaTeX\ user. |
| 56 |
\ncpy\ is a Python program that automatically generates |
| 57 |
\LaTeX\ macro definitions for macros that require more powerful |
| 58 |
argument processing than \cs{newcommand} can handle. \ncpy\ is |
| 59 |
intended for \LaTeX\ advanced beginners (i.e., those who know how to |
| 60 |
use \cs{newcommand} but not internal \LaTeXe\ macros like |
| 61 |
|\@ifnextchar|) and for more advanced users who want to save some |
| 62 |
typing when defining complex macros. |
| 63 |
\end{abstract} |
| 64 |
|
| 65 |
\section{Introduction} |
| 66 |
|
| 67 |
\LaTeX's \cs{newcommand} is a rather limited way to define new macros. |
| 68 |
Only one argument can be designated as optional, it must be the first |
| 69 |
argument, and it must appear within square brackets. Defining macros |
| 70 |
that take multiple optional arguments or in which an optional argument |
| 71 |
appears in the middle of the argument list is possible but well beyond |
| 72 |
the capabilities of the casual \LaTeX\ user. It requires using |
| 73 |
\TeX\ primitives such as \cs{def} and \cs{futurelet} and/or |
| 74 |
\LaTeXe\ internal macros such as \cs{@ifnextchar}. |
| 75 |
|
| 76 |
\ncpy\ is a Python program that reads a specification of an argument |
| 77 |
list and automatically produces \LaTeX\ code that processes the |
| 78 |
arguments appropriately. \ncpy\ makes it easy to define |
| 79 |
\LaTeX\ macros with more complex parameter parsing than is possible |
| 80 |
with \cs{newcommand} alone. Note that you do need to have Python |
| 81 |
installed on your system to run \ncpy. Python is freely available for |
| 82 |
download from \url{http://www.python.org/}. |
| 83 |
|
| 84 |
To define a \LaTeX\ macro, one gives \ncpy\ a macro description |
| 85 |
written in a simple specification language. The description |
| 86 |
essentially lists the required and optional arguments and, for each |
| 87 |
optional argument, the default value. The next section of this |
| 88 |
document describes the syntax and provides some examples, but for now, |
| 89 |
let's look at how one would define the most trivial macro possible, |
| 90 |
one that takes no arguments. Enter the following at your operating |
| 91 |
system's prompt: |
| 92 |
|
| 93 |
\begin{alltt} |
| 94 |
\usercmd{newcommand.py "MACRO trivial"} |
| 95 |
\end{alltt} |
| 96 |
|
| 97 |
\noindent |
| 98 |
(Depending on your system, you may need to prefix that command with |
| 99 |
``|python|''.) The program should output the following \LaTeX\ code |
| 100 |
in response: |
| 101 |
|
| 102 |
\begin{verbatim} |
| 103 |
% Prototype: MACRO trivial |
| 104 |
\newcommand{\trivial}{% |
| 105 |
% Put your code here. |
| 106 |
} |
| 107 |
\end{verbatim} |
| 108 |
|
| 109 |
\noindent |
| 110 |
Alternatively, you can run \ncpy\ interactively, entering macro |
| 111 |
descriptions at the ``|% Prototype:|'' prompt: |
| 112 |
|
| 113 |
\prototype{MACRO trivial} |
| 114 |
\begin{verbatim} |
| 115 |
\newcommand{\trivial}{% |
| 116 |
% Put your code here. |
| 117 |
} |
| 118 |
% Prototype: |
| 119 |
\end{verbatim} |
| 120 |
|
| 121 |
\noindent |
| 122 |
Enter your operating system's end-of-file character (Ctrl-D in Unix or |
| 123 |
Ctrl-Z in Windows) to exit the program. |
| 124 |
|
| 125 |
While you certainly don't need \ncpy\ to write macros that are as |
| 126 |
trivial as \cs{trivial}, the previous discussion shows how to run the |
| 127 |
program and the sort of output that you should expect. There will |
| 128 |
always be a ``\texttt{Put your code here}'' comment indicating where |
| 129 |
you should fill in the actual macro code. At that location, all of |
| 130 |
the macro's parameters---both optional and required---will be defined |
| 131 |
and can be referred to in the ordinary way: |#1|, |#2|, |#3|, etc. |
| 132 |
|
| 133 |
|
| 134 |
\section{Usage} |
| 135 |
|
| 136 |
As we saw in the previous section, macros are defined by the word |
| 137 |
``|MACRO|'' followed by the macro name, with no preceding backslash. |
| 138 |
In this section we examine how to specify increasingly sophisticated |
| 139 |
argument processing using \ncpy. |
| 140 |
|
| 141 |
|
| 142 |
\subsection{Required arguments} |
| 143 |
|
| 144 |
Required arguments are entered as~|#1|, |#2|, |#3|, \dots, with no |
| 145 |
surrounding braces: |
| 146 |
|
| 147 |
\prototype{MACRO required \#1 \#2 \#3 \#4 \#5} |
| 148 |
\begin{verbatim} |
| 149 |
\newcommand{\required}[5]{% |
| 150 |
% Put your code here. |
| 151 |
% You can refer to the arguments as #1 through #5. |
| 152 |
} |
| 153 |
\end{verbatim} |
| 154 |
|
| 155 |
Parameters must be numbered in monotonically increasing order, |
| 156 |
starting with~|#1|. Incorrectly ordered parameters will produce an |
| 157 |
error message: |
| 158 |
|
| 159 |
\prototype{MACRO required \#1 \#3 \#4} |
| 160 |
\begin{verbatim} |
| 161 |
^ |
| 162 |
newcommand.py: Expected parameter 2 but saw parameter 3. |
| 163 |
\end{verbatim} |
| 164 |
|
| 165 |
|
| 166 |
\subsection{Optional arguments} |
| 167 |
|
| 168 |
Optional arguments\label{par:optional-args} are written as either |
| 169 |
``|OPT[|\meta{param}|=|\linebreak[1]|{|\meta{default}|}]|'' or |
| 170 |
``|OPT(|\meta{param}|=|\linebreak[1]|{|\meta{default}|})|''. In the |
| 171 |
former case, square brackets are used to offset the optional argument; |
| 172 |
in the latter case, parentheses are used. \meta{param} is the |
| 173 |
parameter number (|#1|, |#2|, |#3|, \dots), and \meta{default} is the |
| 174 |
default value for that parameter. Note that curly braces are required |
| 175 |
around \meta{default}. |
| 176 |
|
| 177 |
\prototype{MACRO optional OPT[\#1=\{maybe\}]} |
| 178 |
\begin{verbatim} |
| 179 |
\newcommand{\optional}[1][maybe]{% |
| 180 |
% Put your code here. |
| 181 |
% You can refer to the argument as #1. |
| 182 |
} |
| 183 |
\end{verbatim} |
| 184 |
|
| 185 |
Up to this point, the examples have been so simple that \ncpy\ is |
| 186 |
overkill for entering them. We can now begin specifying constructs |
| 187 |
that \LaTeX's \cs{newcommand} can't handle, such as a parenthesized |
| 188 |
optional argument, an optional argument that doesn't appear at the |
| 189 |
beginning of the argument list, and multiple optional arguments: |
| 190 |
|
| 191 |
\prototype{MACRO parenthesized OPT(\#1=\{abc\})} |
| 192 |
\begin{verbatim} |
| 193 |
\makeatletter |
| 194 |
\newcommand{\parenthesized}{% |
| 195 |
\@ifnextchar({\parenthesized@i}{\parenthesized@i({abc})}% |
| 196 |
} |
| 197 |
|
| 198 |
\def\parenthesized@i(#1){% |
| 199 |
% Put your code here. |
| 200 |
% You can refer to the argument as #1. |
| 201 |
} |
| 202 |
\makeatother |
| 203 |
\end{verbatim} |
| 204 |
|
| 205 |
|
| 206 |
\prototype{MACRO nonbeginning \#1 OPT[\#2=\{abc\}]} |
| 207 |
\begin{verbatim} |
| 208 |
\makeatletter |
| 209 |
\newcommand{\nonbeginning}[1]{% |
| 210 |
\@ifnextchar[{\nonbeginning@i{#1}}{\nonbeginning@i{#1}[{abc}]}% |
| 211 |
} |
| 212 |
|
| 213 |
\def\nonbeginning@i#1[#2]{% |
| 214 |
% Put your code here. |
| 215 |
% You can refer to the arguments as #1 and #2. |
| 216 |
} |
| 217 |
\makeatother |
| 218 |
\end{verbatim} |
| 219 |
|
| 220 |
\prototype{MACRO multiple OPT[\#1=\{abc\}] OPT[\#2=\{def\}]} |
| 221 |
\begin{verbatim} |
| 222 |
\makeatletter |
| 223 |
\newcommand{\multiple}[1][abc]{% |
| 224 |
\@ifnextchar[{\multiple@i[{#1}]}{\multiple@i[{#1}][{def}]}% |
| 225 |
} |
| 226 |
|
| 227 |
\def\multiple@i[#1][#2]{% |
| 228 |
% Put your code here. |
| 229 |
% You can refer to the arguments as #1 and #2. |
| 230 |
} |
| 231 |
\makeatother |
| 232 |
\end{verbatim} |
| 233 |
|
| 234 |
The template for optional arguments that was |
| 235 |
shown~\vpageref[above]{par:optional-args} stated that optional |
| 236 |
arguments contain a ``\meta{param}|={|\meta{default}|}|'' |
| 237 |
specification. In fact, optional arguments can contain |
| 238 |
\emph{multiple} ``\meta{param}|={|\meta{default}|}|'' specifications, |
| 239 |
as long as they are separated by literal text: |
| 240 |
|
| 241 |
\prototype{MACRO multiopt OPT(\#1=\{0\},\#2=\{0\})} |
| 242 |
\begin{verbatim} |
| 243 |
\makeatletter |
| 244 |
\newcommand{\multiopt}{% |
| 245 |
\@ifnextchar({\multiopt@i}{\multiopt@i({0},{0})}% |
| 246 |
} |
| 247 |
|
| 248 |
\def\multiopt@i(#1,#2){% |
| 249 |
% Put your code here. |
| 250 |
% You can refer to the arguments as #1 and #2. |
| 251 |
} |
| 252 |
\makeatother |
| 253 |
\end{verbatim} |
| 254 |
|
| 255 |
\noindent |
| 256 |
In that example, \cs{multiopt} takes an optional parenthesized |
| 257 |
argument. If omitted, it defaults to |(0,0)|. If provided, the |
| 258 |
argument must be of the form ``|(|\meta{x}|,|\meta{y}|)|''. In either |
| 259 |
case, the comma-separated values within the parentheses are parsed |
| 260 |
into~|#1| and~|#2|. Contrast that with the following: |
| 261 |
|
| 262 |
\prototype{MACRO multiopt OPT(\#1=\{0,0\})} |
| 263 |
\begin{verbatim} |
| 264 |
\makeatletter |
| 265 |
\newcommand{\multiopt}{% |
| 266 |
\@ifnextchar({\multiopt@i}{\multiopt@i({0,0})}% |
| 267 |
} |
| 268 |
|
| 269 |
\def\multiopt@i(#1){% |
| 270 |
% Put your code here. |
| 271 |
% You can refer to the argument as #1. |
| 272 |
} |
| 273 |
\makeatother |
| 274 |
\end{verbatim} |
| 275 |
|
| 276 |
The optional argument still defaults to |(0,0)|, but |#1| receives |
| 277 |
\emph{all} of the text that lies between the parentheses; |
| 278 |
\cs{multiopt} does not parse it into two comma-separated values |
| 279 |
in~|#1| and~|#2|, as it did in the previous example. |
| 280 |
|
| 281 |
\bigskip |
| 282 |
|
| 283 |
The \meta{default} text in an |OPT| term can reference any macro |
| 284 |
parameter introduced before the |OPT|\@. Hence, the following defines |
| 285 |
a macro that accepts a required argument followed by an optional |
| 286 |
argument. The default value of the optional argument is the value |
| 287 |
provided for the required argument: |
| 288 |
|
| 289 |
\prototype{MACRO paramdefault \#1 OPT[\#2=\{\#1\}]} |
| 290 |
\begin{verbatim} |
| 291 |
\makeatletter |
| 292 |
\newcommand{\paramdefault}[1]{% |
| 293 |
\@ifnextchar[{\paramdefault@i{#1}}{\paramdefault@i{#1}[{#1}]}% |
| 294 |
} |
| 295 |
|
| 296 |
\def\paramdefault@i#1[#2]{% |
| 297 |
% Put your code here. |
| 298 |
% You can refer to the arguments as #1 and #2. |
| 299 |
} |
| 300 |
\makeatother |
| 301 |
\end{verbatim} |
| 302 |
|
| 303 |
|
| 304 |
\subsection{Literal text} |
| 305 |
|
| 306 |
In addition to required and optional parameters, it is also possible |
| 307 |
to specify text that must appear literally in the macro call. Merely |
| 308 |
specify it within curly braces: |
| 309 |
|
| 310 |
\prototype{MACRO textual \#1 \{ and \} \#2 \{.\}} |
| 311 |
\begin{verbatim} |
| 312 |
\makeatletter |
| 313 |
\newcommand{\textual}[1]{% |
| 314 |
\textual@i{#1}% |
| 315 |
} |
| 316 |
|
| 317 |
\def\textual@i#1 and #2.{% |
| 318 |
% Put your code here. |
| 319 |
% You can refer to the arguments as #1 and #2. |
| 320 |
} |
| 321 |
\makeatother |
| 322 |
\end{verbatim} |
| 323 |
|
| 324 |
\noindent |
| 325 |
A macro such as \cs{textual} can be called like this: |
| 326 |
|
| 327 |
\begin{verbatim} |
| 328 |
\textual {Milk} and {cookies}. |
| 329 |
\end{verbatim} |
| 330 |
|
| 331 |
\noindent |
| 332 |
Actually, in that example, because both |Milk| and |cookies| are |
| 333 |
delimited on the right by literal text, \TeX\ can figure out how to |
| 334 |
split \cs{textual}'s argument into~|#1| and~|#2| even if the curly |
| 335 |
braces are omitted: |
| 336 |
|
| 337 |
\begin{verbatim} |
| 338 |
\textual Milk and cookies. |
| 339 |
\end{verbatim} |
| 340 |
|
| 341 |
\subsection{Starred macros} |
| 342 |
|
| 343 |
The names of some \LaTeX\ macros can be followed by an optional |
| 344 |
``|*|'' to indicate a variation on the normal processing. For |
| 345 |
example, \cs{vspace}, which introduces a given amount of vertical |
| 346 |
space, discards the space if it appears at the top of the page. |
| 347 |
|\vspace*|, in contrast, typesets the space no matter where it |
| 348 |
appears. \ncpy\ makes it easy for users to define their own starred |
| 349 |
commands: |
| 350 |
|
| 351 |
\prototype{MACRO starred * \#1 \#2} |
| 352 |
\begin{verbatim} |
| 353 |
\makeatletter |
| 354 |
\newif\ifstarred@star |
| 355 |
\newcommand{\starred}{% |
| 356 |
\@ifstar{\starred@startrue\starred@i*}{\starred@starfalse\starred@i*}% |
| 357 |
} |
| 358 |
|
| 359 |
\def\starred@i*#1#2{% |
| 360 |
\ifstarred@star |
| 361 |
% Put code for the "*" case here. |
| 362 |
\else |
| 363 |
% Put code for the non-"*" case here. |
| 364 |
\fi |
| 365 |
% Put code common to both cases here (and/or above the \ifstarred@star). |
| 366 |
% You can refer to the arguments as #1 and #2. |
| 367 |
} |
| 368 |
\makeatother |
| 369 |
\end{verbatim} |
| 370 |
|
| 371 |
\noindent |
| 372 |
Note that unlike the generated code shown up to this point, the code |
| 373 |
for starred macros includes \emph{multiple} placeholders for user |
| 374 |
code. |
| 375 |
|
| 376 |
The ``|*|'' in a starred macro does not have to immediately follow the |
| 377 |
macro name; it can appear anywhere in the macro specification. |
| 378 |
However, \ncpy\ currently limits macros to at most one asterisk. |
| 379 |
|
| 380 |
Embedding an asterisk within curly braces causes it to be treated not |
| 381 |
as an optional character but as (required) literal text. Contrast the |
| 382 |
preceding example with the following one: |
| 383 |
|
| 384 |
\prototype{MACRO starred \{*\} \#1 \#2} |
| 385 |
\begin{verbatim} |
| 386 |
\makeatletter |
| 387 |
\newcommand{\starred}{% |
| 388 |
\starred@i% |
| 389 |
} |
| 390 |
|
| 391 |
\def\starred@i*#1#2{% |
| 392 |
% Put your code here. |
| 393 |
% You can refer to the arguments as #1 and #2. |
| 394 |
} |
| 395 |
\makeatother |
| 396 |
\end{verbatim} |
| 397 |
|
| 398 |
The asterisk in that definition of \cs{starred} must be included in |
| 399 |
every macro invocation or \TeX\ will abort with a ``\texttt{Use of |
| 400 |
\string\starred@i doesn't match its definition}'' error. |
| 401 |
|
| 402 |
|
| 403 |
\subsection{More than nine arguments} |
| 404 |
|
| 405 |
\TeX\ imposes a limit of nine arguments per macro. Internally, |
| 406 |
``|#|'' is expected to be followed by exactly one digit, which means |
| 407 |
that ``|#10|'' refers to argument~|#1| followed by the character~|0|. |
| 408 |
Fortunately, it's rare that a macro needs more than nine arguments and |
| 409 |
rarer still that those arguments are not better specified as a list of |
| 410 |
\meta{key}|=|\meta{value} pairs, as supported by the \textsf{keyval} |
| 411 |
package and many other \LaTeX\ packages. |
| 412 |
|
| 413 |
If large numbers of arguments are in fact necessary, \ncpy\ does let |
| 414 |
you specify them. The trick that the generated code uses is to split |
| 415 |
the macro into multiple macros, each of which takes nine or fewer |
| 416 |
arguments and stores the value of each argument in a variable that can |
| 417 |
later be accessed. Because digits are awkward to use in macro names, |
| 418 |
\ncpy\ uses roman numerals to name arguments in the case of more than |
| 419 |
nine arguments: |\|\meta{name}|@arg@i| for |#1|, |
| 420 |
|\|\meta{name}|@arg@ii| for |#2|, |\|\meta{name}|@arg@iii| for |#3|, |
| 421 |
|\|\meta{name}|@arg@iv| for |#4|, and so forth. The following example |
| 422 |
takes 14 required arguments and one optional argument (which defaults |
| 423 |
to the string ``|etc|''): |
| 424 |
|
| 425 |
\prototype{MACRO manyargs \#1 \#2 \#3 \#4 \#5 \#6 \#7 \#8 \#9 \#10 |
| 426 |
\#11 \#12 \#13 \#14 OPT[\#15=\{etc\}]} |
| 427 |
\begin{verbatim} |
| 428 |
\makeatletter |
| 429 |
\newcommand{\manyargs}[9]{% |
| 430 |
\def\manyargs@arg@i{#1}% |
| 431 |
\def\manyargs@arg@ii{#2}% |
| 432 |
\def\manyargs@arg@iii{#3}% |
| 433 |
\def\manyargs@arg@iv{#4}% |
| 434 |
\def\manyargs@arg@v{#5}% |
| 435 |
\def\manyargs@arg@vi{#6}% |
| 436 |
\def\manyargs@arg@vii{#7}% |
| 437 |
\def\manyargs@arg@viii{#8}% |
| 438 |
\def\manyargs@arg@ix{#9}% |
| 439 |
\manyargs@i |
| 440 |
} |
| 441 |
|
| 442 |
\def\manyargs@i#1#2#3#4#5{% |
| 443 |
\def\manyargs@arg@x{#1}% |
| 444 |
\def\manyargs@arg@xi{#2}% |
| 445 |
\def\manyargs@arg@xii{#3}% |
| 446 |
\def\manyargs@arg@xiii{#4}% |
| 447 |
\def\manyargs@arg@xiv{#5}% |
| 448 |
\@ifnextchar[{\manyargs@ii}{\manyargs@ii[{etc}]}% |
| 449 |
} |
| 450 |
|
| 451 |
\def\manyargs@ii[#1]{% |
| 452 |
\def\manyargs@arg@xv{#1}% |
| 453 |
% Put your code here. |
| 454 |
% You can refer to the arguments as \manyargs@arg@i through \manyargs@arg@xv. |
| 455 |
} |
| 456 |
\makeatother |
| 457 |
\end{verbatim} |
| 458 |
|
| 459 |
The current version of \ncpy\ is limited to 4000 arguments, which |
| 460 |
should be more than enough for most purposes. |
| 461 |
|
| 462 |
|
| 463 |
\subsection{Summary} |
| 464 |
|
| 465 |
A macro is defined in \ncpy\ with: |
| 466 |
|
| 467 |
\begin{center} |
| 468 |
\texttt{MACRO} \meta{name} \meta{arguments} |
| 469 |
\end{center} |
| 470 |
|
| 471 |
\noindent |
| 472 |
in which \meta{name} is the name of the macro, and \meta{arguments} is |
| 473 |
zero or more of the following: |
| 474 |
|
| 475 |
\begin{center} |
| 476 |
\renewcommand{\arraystretch}{1.1} |
| 477 |
\begin{tabularx}{\linewidth}{@{}lXl@{}} |
| 478 |
\toprule |
| 479 |
\multicolumn{1}{@{}c}{Argument} & |
| 480 |
\multicolumn{1}{c}{Meaning} & |
| 481 |
\multicolumn{1}{c@{}}{Example} \\ |
| 482 |
\midrule |
| 483 |
|
| 484 |
|#|\meta{number} & Parameter (required) & |#1| \\ |
| 485 |
\marg{text} & Literal text (required) & |{+}| \\ |
| 486 |
|OPT[#|\meta{number}|=|\marg{text}|]| & Parameter (optional, with default) & |
| 487 |
|OPT[#1={tbp}]| \\ |
| 488 |
|OPT(#|\meta{number}|=|\marg{text}|)| & Same as the above, but with |
| 489 |
parentheses instead of brackets & |OPT(#1={tbp})| \\ |
| 490 |
|*| & Literal asterisk (optional) & |*| \\ |
| 491 |
\bottomrule |
| 492 |
\end{tabularx} |
| 493 |
\end{center} |
| 494 |
|
| 495 |
Within an |OPT| argument, |#|\meta{number}|=|\marg{text} can be |
| 496 |
repeated any number of times, as long as the various instances are |
| 497 |
separated by literal text. |
| 498 |
|
| 499 |
|
| 500 |
\section{Further examples} |
| 501 |
|
| 502 |
\subsection{Mimicking \LaTeX's \texttt{picture} environment} |
| 503 |
|
| 504 |
The \LaTeX\ |picture| environment takes two, parenthesized, |
| 505 |
coordinate-pair arguments, the second pair being optional. Here's how |
| 506 |
to define a macro that takes the same arguments as the |picture| |
| 507 |
environment and parses them into $x_1$, $y_1$, $x_2$, and~$y_2$ |
| 508 |
(i.e.,~|#1|--|#4|): |
| 509 |
|
| 510 |
\prototype{MACRO picturemacro \{(\}\#1\{,\}\#2\{)\} OPT(\#3=\{0\},\#4=\{0\})} |
| 511 |
\begin{verbatim} |
| 512 |
\makeatletter |
| 513 |
\newcommand{\picturemacro}{% |
| 514 |
\picturemacro@i% |
| 515 |
} |
| 516 |
|
| 517 |
\def\picturemacro@i(#1,#2){% |
| 518 |
\@ifnextchar({\picturemacro@ii({#1},{#2})}{\picturemacro@ii({#1},{#2})({0},{0})}% |
| 519 |
} |
| 520 |
|
| 521 |
\def\picturemacro@ii(#1,#2)(#3,#4){% |
| 522 |
% Put your code here. |
| 523 |
% You can refer to the arguments as #1 through #4. |
| 524 |
} |
| 525 |
\makeatother |
| 526 |
\end{verbatim} |
| 527 |
|
| 528 |
The first pair of parentheses and the comma are quoted because they |
| 529 |
represent required, literal text. |
| 530 |
|
| 531 |
|
| 532 |
\subsection{Mimicking \LaTeX's \texttt{\string\parbox} macro} |
| 533 |
|
| 534 |
\LaTeX's \cs{parbox} macro takes three optional arguments and two |
| 535 |
required arguments. Furthermore, the third argument defaults to |
| 536 |
whatever value was specified for the first argument. This is easy to |
| 537 |
express in \LaTeX\ with the help of \ncpy: |
| 538 |
|
| 539 |
\prototype{MACRO parboxlike OPT[\#1=\{s\}] OPT[\#2=\{\string\relax\}] |
| 540 |
OPT[\#3=\{\#1\}] \#4 \#5} |
| 541 |
\begin{verbatim} |
| 542 |
\makeatletter |
| 543 |
\newcommand{\parboxlike}[1][s]{% |
| 544 |
\@ifnextchar[{\parboxlike@i[{#1}]}{\parboxlike@i[{#1}][{\relax}]}% |
| 545 |
} |
| 546 |
|
| 547 |
\def\parboxlike@i[#1][#2]{% |
| 548 |
\@ifnextchar[{\parboxlike@ii[{#1}][{#2}]}{\parboxlike@ii[{#1}][{#2}][{#1}]}% |
| 549 |
} |
| 550 |
|
| 551 |
\def\parboxlike@ii[#1][#2][#3]#4#5{% |
| 552 |
% Put your code here. |
| 553 |
% You can refer to the arguments as #1 through #5. |
| 554 |
} |
| 555 |
\makeatother |
| 556 |
\end{verbatim} |
| 557 |
|
| 558 |
|
| 559 |
\subsection{Dynamically changing argument formats} |
| 560 |
\label{sec:dynamic-args} |
| 561 |
|
| 562 |
With a little cleverness, it is possible for a macro to accept one of |
| 563 |
two completely different sets of arguments based on the values |
| 564 |
provided for earlier arguments. For example, suppose we want to |
| 565 |
define a macro, \cs{differentargs} that can be called as either |
| 566 |
|
| 567 |
\begin{verbatim} |
| 568 |
\differentargs*[optarg]{reqarg} |
| 569 |
\end{verbatim} |
| 570 |
|
| 571 |
\noindent |
| 572 |
or |
| 573 |
|
| 574 |
\begin{verbatim} |
| 575 |
\differentargs{reqarg}(optarg) |
| 576 |
\end{verbatim} |
| 577 |
|
| 578 |
\noindent |
| 579 |
That is, the presence of an asterisk determines whether |
| 580 |
\cs{differentargs} should expect an optional argument in square |
| 581 |
brackets followed by a required argument or to expect a required |
| 582 |
argument followed by an optional argument in parentheses. |
| 583 |
|
| 584 |
The trick is to create two helper macros: one for the ``|*|'' case |
| 585 |
(\cs{withstar}) and the other for the non-``|*|'' case |
| 586 |
(\cs{withoutstar}). \cs{differentargs} can then invoke one of |
| 587 |
\cs{withstar} or \cs{withoutstar} based on whether or not it sees an |
| 588 |
asterisk. The following shows how to use \ncpy\ to define |
| 589 |
\cs{differentargs}, \cs{withstar}, and \cs{withoutstar} and how to |
| 590 |
edit \cs{differentargs} to invoke its helper macros: |
| 591 |
|
| 592 |
\prototype{MACRO differentargs *} |
| 593 |
\begin{verbatim} |
| 594 |
\makeatletter |
| 595 |
\newif\ifdifferentargs@star |
| 596 |
\newcommand{\differentargs}{% |
| 597 |
\@ifstar{\differentargs@startrue\differentargs@i*} |
| 598 |
{\differentargs@starfalse\differentargs@i*}% |
| 599 |
} |
| 600 |
\end{verbatim} |
| 601 |
\begingroup |
| 602 |
\small |
| 603 |
\begin{alltt} |
| 604 |
\cs{def}\cs{differentargs@i}*\{% |
| 605 |
\cs{ifdifferentargs@star} |
| 606 |
% Put code for the "*" case here. |
| 607 |
\colorbox{yellow}{\cs{let}\cs{next}=\cs{withstar}} |
| 608 |
\cs{else} |
| 609 |
% Put code for the non-"*" case here. |
| 610 |
\colorbox{yellow}{\cs{let}\cs{next}=\cs{withoutstar}} |
| 611 |
\cs{fi} |
| 612 |
% Put code common to both cases here (and/or above the \cs{ifdifferentargs@star}). |
| 613 |
\colorbox{yellow}{\cs{next}} |
| 614 |
\} |
| 615 |
\cs{makeatother} |
| 616 |
\end{alltt} |
| 617 |
\endgroup |
| 618 |
|
| 619 |
\prototype{MACRO withstar OPT[\#1=\{starry\}] \#2} |
| 620 |
\begin{verbatim} |
| 621 |
\newcommand{\withstar}[2][starry]{% |
| 622 |
% Put your code here. |
| 623 |
% You can refer to the arguments as #1 and #2. |
| 624 |
} |
| 625 |
\end{verbatim} |
| 626 |
|
| 627 |
\prototype{MACRO withoutstar \#1 OPT(\#2=\{dark\})} |
| 628 |
\begin{verbatim} |
| 629 |
\makeatletter |
| 630 |
\newcommand{\withoutstar}[1]{% |
| 631 |
\@ifnextchar({\withoutstar@i{#1}}{\withoutstar@i{#1}({dark})}% |
| 632 |
} |
| 633 |
|
| 634 |
\def\withoutstar@i#1(#2){% |
| 635 |
% Put your code here. |
| 636 |
% You can refer to the arguments as #1 and #2. |
| 637 |
} |
| 638 |
\makeatother |
| 639 |
\end{verbatim} |
| 640 |
|
| 641 |
Note that we edited \cs{differentargs@i} to let \cs{next} be |
| 642 |
equivalent to either \cs{withstar} or \cs{withoutstar} based on |
| 643 |
whether an asterisk was encountered. \cs{next} is evaluated outside |
| 644 |
of the \cs{ifdifferentargs@star}\dots\linebreak[0]\cs{fi} control |
| 645 |
structure. This rigmarole is necessary because directly calling |
| 646 |
\cs{withstar} or \cs{withoutstar} would cause those macros to see |
| 647 |
\cs{ifdifferentargs@star}'s \cs{else} or \cs{fi} as their first |
| 648 |
argument when they ought to see the text following the |
| 649 |
\cs{differentargs} call. |
| 650 |
|
| 651 |
|
| 652 |
\section{Grammar} |
| 653 |
|
| 654 |
The following is the formal specification of \ncpy's grammar, written |
| 655 |
in a more-or-less top-down manner. Literal values, shown in a |
| 656 |
typewriter font, are case-sensitive. \meta{letter} refers to a letter |
| 657 |
of the (English) alphabet. \meta{digit} refers to a digit. |
| 658 |
|
| 659 |
\setlength{\grammarindent}{7em} |
| 660 |
\begin{grammar} |
| 661 |
<decl> ::= \[[ "MACRO" <ident> <arglist> \]] |
| 662 |
|
| 663 |
<ident> ::= \[[ \begin{rep} <letter> \end{rep} \]] |
| 664 |
|
| 665 |
<arglist> ::= \[[ \begin{rep} \\ <arg> \end{rep} \]] |
| 666 |
|
| 667 |
<arg> ::= \[[ |
| 668 |
\begin{stack} |
| 669 |
<formal> \\ |
| 670 |
<quoted> \\ |
| 671 |
<optarg> \\ |
| 672 |
"*" |
| 673 |
\end{stack} |
| 674 |
\]] |
| 675 |
|
| 676 |
<formal> ::= \[[ "#" \begin{rep} <digit> \end{rep} \]] |
| 677 |
|
| 678 |
<quoted> ::= \[[ "{" <rawtext> "}" \]] |
| 679 |
|
| 680 |
<rawtext> ::= \[[ \begin{rep} \tok{anything except a "{", "}", or "\#"} \end{rep} \]] |
| 681 |
|
| 682 |
<optarg> ::= \[[ "OPT" <delim> <defvals> <delim> \]] |
| 683 |
|
| 684 |
<delim> ::= \[[ \begin{stack} "[" \\ "]" \\ "(" \\ ")" \end{stack} \]] |
| 685 |
|
| 686 |
<defvals> ::= \[[ |
| 687 |
\begin{rep} |
| 688 |
<defval> \\ |
| 689 |
\begin{stack} |
| 690 |
<quoted> \\ |
| 691 |
<rawtext> |
| 692 |
\end{stack} |
| 693 |
\end{rep} |
| 694 |
\]] |
| 695 |
|
| 696 |
<defval> ::= \[[ <formal> "=" <quoted> \]] |
| 697 |
\end{grammar} |
| 698 |
|
| 699 |
|
| 700 |
\section{Acknowledgements} |
| 701 |
|
| 702 |
I'd like to say thank you to the following people: |
| 703 |
|
| 704 |
\begin{itemize} |
| 705 |
\item John Aycock for writing the |
| 706 |
\href{http://pages.cpsc.ucalgary.ca/~aycock/spark/}{Scanning, |
| 707 |
Parsing, and Rewriting Kit (SPARK)}---the lexer and parser |
| 708 |
underlying \ncpy---and making it freely available and |
| 709 |
redistributable. |
| 710 |
|
| 711 |
\item Hendri Adriaens for pointing out a bug in the code generated |
| 712 |
by \ncpy. Previously, bracketed text within a mandatory argument |
| 713 |
could be mistaken for an optional argument. |
| 714 |
|
| 715 |
\item Tom Potts for reporting a spurious error message caused by the |
| 716 |
processing of |OPT|. This bug has now been fixed. Tom Potts also |
| 717 |
proposed the example used in Section~\ref{sec:dynamic-args} in |
| 718 |
which the starred and unstarred versions of a macro take different |
| 719 |
arguments. |
| 720 |
\end{itemize} |
| 721 |
|
| 722 |
|
| 723 |
\section{Copyright and license} |
| 724 |
|
| 725 |
Copyright~\copyright{} 2010, Scott Pakin |
| 726 |
|
| 727 |
\bigskip |
| 728 |
|
| 729 |
This package may be distributed and/or modified under the conditions |
| 730 |
of the \LaTeX{} Project Public License, either version~1.3c of this |
| 731 |
license or (at your option) any later version. The latest version of |
| 732 |
this license is in: |
| 733 |
|
| 734 |
\begin{center} |
| 735 |
\url{http://www.latex-project.org/lppl.txt} |
| 736 |
\end{center} |
| 737 |
|
| 738 |
\noindent |
| 739 |
and version~1.3c or later is part of all distributions of \LaTeX{} |
| 740 |
version 2006/05/20 or later. |
| 741 |
|
| 742 |
\end{document} |