| 1 |
\documentclass{article} |
| 2 |
|
| 3 |
\usepackage{etex} |
| 4 |
\usepackage{xifthen} |
| 5 |
\usepackage[ascii]{inputenc} |
| 6 |
\usepackage[T1]{fontenc} |
| 7 |
\usepackage[warn]{textcomp} |
| 8 |
\usepackage[a4paper, pdftex, margin=1.5in]{geometry} |
| 9 |
\usepackage{lmodern} |
| 10 |
\usepackage{typetex} |
| 11 |
\usepackage{xoptarg} |
| 12 |
\usepackage{microtype} |
| 13 |
\usepackage{xcolor} |
| 14 |
\usepackage{url} |
| 15 |
\usepackage[bottom,hang]{footmisc} |
| 16 |
\usepackage[babel]{csquotes} |
| 17 |
\usepackage[british]{babel} |
| 18 |
\usepackage{hyperref} |
| 19 |
|
| 20 |
\setcounter{secnumdepth}{-\maxdimen} |
| 21 |
|
| 22 |
\newcommand*{\pack}{\textsf} |
| 23 |
|
| 24 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 25 |
|
| 26 |
\newenvironment*{display}{% |
| 27 |
\list{}{}% |
| 28 |
\item \relax |
| 29 |
}{% |
| 30 |
\endlist |
| 31 |
} |
| 32 |
|
| 33 |
\newcommand*{\setdate}{} |
| 34 |
\def \setdate #1/#2/#3#4{% |
| 35 |
\year = #1 |
| 36 |
\month = #2 |
| 37 |
\day = #3#4 |
| 38 |
} |
| 39 |
|
| 40 |
\makeatletter |
| 41 |
\newcommand*{\getfileinfo}[1]{% |
| 42 |
\def \filename {#1}% |
| 43 |
\def \@tempb ##1 ##2 ##3\relax ##4\relax {% |
| 44 |
\def \filedate {##1}% |
| 45 |
\def \fileversion {##2}% |
| 46 |
\def \fileinfo {##3}% |
| 47 |
}% |
| 48 |
\edef \@tempa {\csname ver@#1\endcsname}% |
| 49 |
\expandafter \@tempb \@tempa \relax ? ? \relax \relax |
| 50 |
} |
| 51 |
|
| 52 |
\newcommand*{\TypeOut}[1]{% |
| 53 |
\texttt{% |
| 54 |
\frenchspacing |
| 55 |
\begingroup |
| 56 |
\let \protect = \string |
| 57 |
\xdef \@gtempa {#1}% |
| 58 |
\endgroup |
| 59 |
\let \@tempa = \@gtempa |
| 60 |
\@onelevel@sanitize \@tempa \@tempa}% |
| 61 |
} |
| 62 |
\makeatother |
| 63 |
|
| 64 |
\title {The \pack{xoptarg} Package} |
| 65 |
\author {Josselin Noirel\\\url{http://www.jnoirel.fr/}} |
| 66 |
\date {% |
| 67 |
\getfileinfo{xoptarg.sty} |
| 68 |
\expandafter \setdate \filedate |
| 69 |
\today\ (\fileversion)} |
| 70 |
|
| 71 |
\begin{document} |
| 72 |
|
| 73 |
\maketitle |
| 74 |
|
| 75 |
\begin{abstract} |
| 76 |
Because they rely on the \cmd{futurelet} primitive, the macros with |
| 77 |
optional arguments cannot be expandable. However, it is possible to |
| 78 |
make them expandable if there is at least one mandatory argument |
| 79 |
(see `Limitations'). |
| 80 |
\end{abstract} |
| 81 |
|
| 82 |
\tableofcontents |
| 83 |
|
| 84 |
\section{Introduction} |
| 85 |
|
| 86 |
\newcommand*{\cmda}[1]{1. #1 2. #1 3. #1 4. #1} |
| 87 |
\newcommand*{\cmdb}[3]{[#1#2#3 #1#3#2 #2#1#3 #2#3#1 #3#1#2 #3#2#1]} |
| 88 |
\newcommand*{\cmdc}[2][X]{(#1,#2)} |
| 89 |
|
| 90 |
Normal macros are expandable: |
| 91 |
% |
| 92 |
\begin{texcode} |
| 93 |
\cmd[3]{newcommand}*{\cmd{cmda}}[1]{1. \#1 2. \#1 3. \#1 4. \#1} \newline |
| 94 |
\cmd[3]{newcommand}*{\cmd{cmdb}}[3]{[\#1\#2\#3 \#1\#3\#2 \#2\#1\#3 |
| 95 |
\#2\#3\#1 \#3\#1\#2 \#3\#2\#1]} \newline |
| 96 |
\end{texcode} |
| 97 |
% |
| 98 |
`Expandable' means that if you print them on the terminal with |
| 99 |
\cmd{typeout} |
| 100 |
% |
| 101 |
\begin{texcode} |
| 102 |
\cmd[1]{typeout}{\cmd[1]{cmda}{Test}...and \cmd[3]{cmdb}{T}{h}{e} rest} \par |
| 103 |
\end{texcode} |
| 104 |
% |
| 105 |
it will result in the processed message which is printed during the |
| 106 |
compilation: |
| 107 |
% |
| 108 |
\begin{display} |
| 109 |
\TypeOut{\cmda{Test}...and \cmdb{T}{h}{e} rest} |
| 110 |
\typeout{TEST>>> \cmda{Test}...and \cmdb{T}{h}{e} rest} |
| 111 |
\end{display} |
| 112 |
|
| 113 |
But, commands defined to take an optional argument behave |
| 114 |
differently. The code: |
| 115 |
% |
| 116 |
\begin{texcode} |
| 117 |
\cmd[3]{newcommand}*{\cmd{cmdc}}[2][X]{(\#1,\#2)} \newline |
| 118 |
\cmd[1]{typeout}{\cmd[2]{cmdc}[A]{B} \cmd[1]{cmdc}{C}} |
| 119 |
\end{texcode} |
| 120 |
% |
| 121 |
prints the unprocessed |
| 122 |
% |
| 123 |
\begin{display} |
| 124 |
\TypeOut{\cmdc[A]{B} \cmdc{C}} |
| 125 |
\typeout{TEST>>> \cmdc[A]{B} \cmdc{C}} |
| 126 |
\end{display} |
| 127 |
% |
| 128 |
and not \tex{(A,B)~(X,C)}! (Of course, in typesetting context, it |
| 129 |
doesn't make any difference\dots\ the interest of the package lies |
| 130 |
in the expandability in moving-argument contexts.) The \pack{xoptarg} |
| 131 |
helps you to make your macros expandable even when they take an |
| 132 |
optional argumen (see `Limitations' though). |
| 133 |
|
| 134 |
\section{Usage} |
| 135 |
|
| 136 |
\subsection{Defining commands} |
| 137 |
|
| 138 |
Your macros, including those that take an optional argument can be |
| 139 |
expandable provided they also take at least one mandatory |
| 140 |
argument\footnote{If your macros don't take any optional argument, the |
| 141 |
standard behaviour prevails so that you don't to have to worry about |
| 142 |
them. The `at least one mandatory argument' condition applies only |
| 143 |
when the macro takes an optional argument.}. Practically spoken, |
| 144 |
your command, when it takes an optional argument, must take at least |
| 145 |
two arguments in total\footnote{A~second requirement is that the |
| 146 |
mandatory arguments be surrounded by explicit braces \tex{\{\dots\}}.}. |
| 147 |
|
| 148 |
Load the package with |
| 149 |
% |
| 150 |
\begin{texcode} |
| 151 |
\cmd[1]{usepackage}{xoptarg} |
| 152 |
\end{texcode} |
| 153 |
% |
| 154 |
then, instead of using \cmd{newcommand}, use \cmd{xnewcommand} |
| 155 |
% |
| 156 |
\begin{texcode} |
| 157 |
\cmd[4]{xnewcommand}*{\<command\>}[$n$][\<default\>]{\<definition\>} |
| 158 |
\end{texcode} |
| 159 |
% |
| 160 |
This is the same syntax as \cmd{newcommand}'s and it works even if the |
| 161 |
command does take any optional argument. A~normal, non-expandable, |
| 162 |
macro is defined when you define a~macro that takes one optional argument |
| 163 |
and no mandatory one. A~warning is issued to inform you that it won't |
| 164 |
be possible to make it expandable\footnote{The command will be made |
| 165 |
robust using \cmd{protected} if \eTeX\ is available.}. |
| 166 |
|
| 167 |
\subsection{Changing the prefix} |
| 168 |
|
| 169 |
This package uses \pack{newcommand} that allows a~more sophisticated |
| 170 |
syntax: |
| 171 |
% |
| 172 |
\begin{texcode} |
| 173 |
\cmd[5]{xnewcommand}[\<prefix\>]{\<command\>}[$n$][\<default\>]{\<definition\>} |
| 174 |
\end{texcode} |
| 175 |
% |
| 176 |
For instance, to define a~macro globally, use |
| 177 |
% |
| 178 |
\begin{texcode} |
| 179 |
\cmd[3]{xnewcommand}[\cmd{global}]{\<command\>}{\<definition\>} |
| 180 |
\end{texcode} |
| 181 |
% |
| 182 |
(But \cmd{newcommand} would do the job as well, as \pack{newcommand} |
| 183 |
improves it also.) |
| 184 |
|
| 185 |
\section{Limitations} |
| 186 |
|
| 187 |
The limitations of the package are the following: |
| 188 |
% |
| 189 |
\begin{itemize} |
| 190 |
\item To be expandable, the macros with an optional argument must have |
| 191 |
at least one mandatory argument. |
| 192 |
|
| 193 |
\item The mandatory arguments must be surrounded by braces in this |
| 194 |
case. |
| 195 |
|
| 196 |
\item There is no \cmd{rexnewcommand} command. |
| 197 |
\end{itemize} |
| 198 |
|
| 199 |
\end{document} |