wright@gefion.rice.edu (Andrew Wright) (03/20/91)
Does anyone have, or can anyone point me to, a "newcommand"-like macro that allows the definition of macros with optional arguments ala LaTeX, ie. optional arguments enclosed in []? Thanks, Andrew Wright
ogawa@orion.arc.nasa.gov (Arthur Ogawa) (03/22/91)
In article <1991Mar19.183216.18630@rice.edu> wright@gefion.rice.edu (Andrew Wright) writes: |Does anyone have, or can anyone point me to, a "newcommand"-like |macro that allows the definition of macros with optional arguments |ala LaTeX, ie. optional arguments enclosed in []? The following is something I just worked out, please let me know if it helps or if it has flaws. Next, someone is going to ask for a "newcommand-like macro that allows the definition of macros with optional star".... %%%%%%%%%%%%%%%%%%%%%%%cut here%%%%%%%%%%%%%%%%%%%%%%% %% wright.tex--example of how to use extended meanings for \newcommand \documentstyle{wright}% %\tracingall \newcommand\testwright[2]{\foo#1\bar}% \show\testwright \renewcommand\testwright[1][\baz]{\foo#1\bar#2\blech}% \show\testwright \expandafter\show\csname\string\testwright\endcsname \stop %%%%%%%%%%%%%%%%%%%%%%%cut here%%%%%%%%%%%%%%%%%%%%%%% %% wright.sty--example of how to change LaTeX's \newcommand (and \renewcommand) %% to define a command with an optional argument. %% Usage: %% \newcommand and \renewcommand are backward compatible. %% To define a command with an optional argument, these commands will take %% a second optional argument: %% %% \newcommand\kilroy[1][\blech]{\foo#1\bar#2\baz}% %% %% will define \kilroy to have an optional argument with default value of %% \blech and expansion \foo#1\bar#2\baz, where the optional argument is #1. %% NB: the optional argument is the first parameter. You must bear this in %% mind when using the \newcommand macro. %% NB: If you wish to define a command that takes an optional argument, but no %% required arguments, usage is, eg: %% %% \newcommand\kilroy[0][\blech]{\foo#1\bar#2\baz}% %% %% Implementation: %% modified commands: %% \newcommand %% \renewcommand %% %% new commands: %% \@newcommand %% \@@newcommand %% \@@@newcommand %% \@ifredefinable %% \@@ifredefinable %% \@commanddef %% \@ldef %% \@hashchar %% \@macparm %% %% commands that change their meaning when these macros are executed: %% \@@ifredefinable %% \@macparm %% \def\newcommand{\let\@@ifdefinable\@ifdefinable\@newcommand}% \def\renewcommand{\let\@@ifdefinable\@ifredefinable\@newcommand}% \def\@newcommand#1{\@ifnextchar[{\@@newcommand#1}{\@@newcommand#1[\z@]}}% \def\@@newcommand#1[#2]{\@ifnextchar[{\@@@newcommand#1[#2]}{\@@@newcommand#1[#2][]}}% \long\def\@@@newcommand#1[#2][#3]#4{\@@ifdefinable#1{\@commanddef#1[#2][#3]{#4}}}% \def\@ifredefinable#1#2{% \edef\@tempa{\expandafter\@cdr\string#1\@nil}% \@ifundefined{\@tempa}{\@latexerr{\string#1\space undefined}\@ehc}{}% \let#1\undefined \@ifdefinable#1{#2}% }% \long\def\@commanddef#1[#2][#3]#4{% \let#1\relax\@tempcnta#2\def\@tempb{#3}\let\@macparm\relax \ifx\@tempb\@empty \@tempcntb\@ne \edef\@tempa{#1}% \else \@tempcntb\tw@ \edef\@tempa{\csname\string#1\endcsname[\@macparm1]}% \def#1{% \@ifnextchar[{\csname\string#1\endcsname}{\csname\string#1\endcsname[#3]}% }% \fi \@whilenum\@tempcnta>\z@\do{% \edef\@tempa{\@tempa\@macparm\the\@tempcntb}% \advance\@tempcntb\@ne\advance\@tempcnta\m@ne }% \let\@macparm\@hashchar\expandafter\@ldef\@tempa{#4}% }% \catcode`\?=12\relax \def\@ldef{\long\def}% \let\@hashchar#% the macro-parameter catcode Arthur Ogawa Internet: ogawa@orion.arc.nasa.gov Ph: 1/415/691-1126 TeX consultant AppleLink: ogawa FAX:1/415/962-1969
ogawa@orion.arc.nasa.gov (Arthur Ogawa) (03/22/91)
In article <1991Mar19.183216.18630@rice.edu> wright@gefion.rice.edu (Andrew Wright) writes: |Does anyone have, or can anyone point me to, a "newcommand"-like |macro that allows the definition of macros with optional arguments |ala LaTeX, ie. optional arguments enclosed in []? My apologies if this turns out to be a repost; rn messed up sending out my previous followup. Please let me know if this help or has problems; it's pretty fresh stuff. Next, somebody will as for a "newcommand-like macro that allows the definition of macros with optional star".... %%%%%%%%%%%%%%%%%%%% cut here %%%%%%%%%%%%%%%%%%%% %% wright.tex--example of how to use extended meanings for \newcommand \documentstyle{wright}% %\tracingall \newcommand\testwright[2]{\foo#1\bar}% \show\testwright \renewcommand\testwright[1][\baz]{\foo#1\bar#2\blech}% \show\testwright \expandafter\show\csname\string\testwright\endcsname \stop %%%%%%%%%%%%%%%%%%%% cut here %%%%%%%%%%%%%%%%%%%% %% wright.sty--example of how to change LaTeX's \newcommand (and \renewcommand) %% to define a command with an optional argument. %% Usage: %% \newcommand and \renewcommand are backward compatible. %% To define a command with an optional argument, these commands will take %% a second optional argument: %% %% \newcommand\kilroy[1][\blech]{\foo#1\bar#2\baz}% %% %% will define \kilroy to have an optional argument with default value of \blech %% and expansion \foo#1\bar#2\baz, where the optional argument is #1. %% NB: the optional argument is the first parameter. You must bear this in %% mind when using the \newcommand macro. %% NB: If you wish to define a command that takes an optional argument, but no %% required arguments, usage is, eg: %% %% \newcommand\kilroy[0][\blech]{\foo#1\bar#2\baz}% %% %% Implementation: %% modified commands: %% \newcommand %% \renewcommand %% %% new commands: %% \@newcommand %% \@@newcommand %% \@@@newcommand %% \@ifredefinable %% \@@ifredefinable %% \@commanddef %% \@ldef %% \@hashchar %% \@macparm %% %% commands that change their meaning when these macros are executed: %% \@@ifredefinable %% \@macparm %% \def\newcommand{\let\@@ifdefinable\@ifdefinable\@newcommand}% \def\renewcommand{\let\@@ifdefinable\@ifredefinable\@newcommand}% \def\@newcommand#1{\@ifnextchar[{\@@newcommand#1}{\@@newcommand#1[\z@]}}% \def\@@newcommand#1[#2]{\@ifnextchar[{\@@@newcommand#1[#2]}{\@@@newcommand#1[#2][]}}% \long\def\@@@newcommand#1[#2][#3]#4{\@@ifdefinable#1{\@commanddef#1[#2][#3]{#4}}}% \def\@ifredefinable#1#2{% \edef\@tempa{\expandafter\@cdr\string#1\@nil}% \@ifundefined{\@tempa}{\@latexerr{\string#1\space undefined}\@ehc}{}% \let#1\undefined \@ifdefinable#1{#2}% }% \long\def\@commanddef#1[#2][#3]#4{% \let#1\relax\@tempcnta#2\def\@tempb{#3}\let\@macparm\relax \ifx\@tempb\@empty \@tempcntb\@ne \edef\@tempa{#1}% \else \@tempcntb\tw@ \edef\@tempa{\csname\string#1\endcsname[\@macparm1]}% \def#1{% \@ifnextchar[{\csname\string#1\endcsname}{\csname\string#1\endcsname[#3]}% }% \fi \@whilenum\@tempcnta>\z@\do{% \edef\@tempa{\@tempa\@macparm\the\@tempcntb}% \advance\@tempcntb\@ne\advance\@tempcnta\m@ne }% \let\@macparm\@hashchar\expandafter\@ldef\@tempa{#4}% }% \catcode`\?=12\relax \def\@ldef{\long\def}% \let\@hashchar#% the macro-parameter catcode Arthur Ogawa Internet: ogawa@orion.arc.nasa.gov Ph: 1/415/691-1126 TeX consultant AppleLink: ogawa FAX:1/415/962-1969