System Macros/Handling Arguments

From Wiki
< System Macros
Revision as of 16:52, 8 June 2020 by Garulfo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

< Prev: Expansion Control | Top: System Macros | Next: Definitions & Assignments >

Gobbling arguments

Controlling arguments is the backbone of macro processing, so naturally there are predefined macros to help you using them. The first set of macros does nothing, except that they get rid of a number of arguments, up to ten arguments altogether. This type of macro is especially useful when you don't really need the user supplied argument(s) to your macro. Their names are \gobbleoneargument up to \gobbletenarguments.

For example, assume you need a macro that would be called like this:

\checkoddpage{this page is odd}

The 'best' definition for that macro is this:

\def\checkoddpage{%
  \ifodd\pageno
    \expandafter\message
  \else
    \expandafter\gobbleoneargument
  \fi
}

The 'simplistic' alternative macro definition:

\def\checkoddpage#1{%
  \ifodd\pageno
    \message{#1}%
  \fi
}

actually runs slower, since the argument is scanned twice: once by \checkoddpage, and once by \message.

Selecting arguments

The next list consists of macros that are trivial, but quite important in some applications. The list of the definitions is like this:

\long\def\firstofoneargument     #1{#1}
\long\def\firstoftwoarguments    #1#2{#1}
\long\def\secondoftwoarguments   #1#2{#2}
...
\long\def\fifthoffivearguments  #1#2#3#4#5{#5}

The need for these commands appears when you have to strip braces from a (saved) argument. For instance, when you have a list with 'subelements', the list expansion tends to look like this:

{{0}{0},{0}{1},{1}{0},{1}{1}}

(A two by two matrix, stored as a commalist). Imagine that you want to grab the row numbers and do something with it. This definition:

\def\doprocessrow#1{... what i really want to do ...}
\def\processrow#1{\expandafter\firstoftwoarguments\doprocessrow}
\processcommalist[{0}{0},{0}{1},{1}{0},{1}{1}]\processrow

does the trick.

Optional delimited arguments

When working with delimited arguments, spaces and lineendings can interfere. The next set of macros uses TeX's internal scanner for grabbing everything between arguments. Forgive me the funny names.

\dosingleargument\command     = \command[#1]
\dodoubleargument\command     = \command[#1][#2]
\dotripleargument\command     = \command[#1][#2][#3]
\doquadrupleargument\command  = \command[#1][#2][#3][#4]
\doquintupleargument\command  = \command[#1][#2][#3][#4][#5]
\dosixtupleargument\command   = \command[#1][#2][#3][#4][#5][#6]
\doseventupleargument\command = \command[#1][#2][#3][#4][#5][#6][#7]

These macros are used in the following way:

\def\docommand[#1][#2]%
  {... #1 ... #2 ...}

\def\command%
  {\dodoubleargument\docommand}

So \dodoubleargument leads to:

\docommand[#1][#2]
\docommand[#1][]
\docommand[][]

The macros above insure that the resulting call always has the correct number of bracket pairs, even if the user did not supply all of the options. In this case, a number of trailing bracket pairs are empty. A set of related \if booleans is initialized to give you access to the number of user-supplied parameters.

Optional delimited arguments with key-value sets

These maybe too mysterious macros enable us to handle more than one setup at once.

\dosingleargumentwithset \command[#1]
\dodoubleargumentwithset \command[#1][#2]
\dotripleargumentwithset \command[#1][#2][#3]
\dodoubleemptywithset    \command[#1][#2]
\dotripleemptywithset    \command[#1][#2][#3]

The first macro calls \command[##1] for each string in the set #1. The second one calls for \command[##1][#2] and the third, well one may guess. These commands support constructions like:

\def\dodefinesomething[#1][#2]%
  {\getparameters[\??xx#1][#2]}

\def\definesomething%
  {\dodoubleargumentwithset\dodefinesomething}

Which accepts calls like:

\definesomething[alfa,beta,...][variable=...,...]

Now a whole bunch of variables like \@@xxalfavariable and \@@xxbetavariable is defined.

Optional braced arguments

We've already seen some commands that take care of optional arguments between [].

The next two commands handle the ones with {}. They are called as:

\dosinglegroupempty    \IneedONEargument
\dodoublegroupempty    \IneedTWOarguments
\dotriplegroupempty    \IneedTHREEarguments
\doquadruplegroupempty \IneedFOURarguments
\doquintuplegroupempty \IneedFIVEarguments

where \IneedONEargument takes one and the others two and three arguments, et cetera.

These macros were first needed in ppchTeX. At first glance, the functionality sounds trivial. But in fact, it is not. Consider this expanded input:

\def\test#1{\message{#1}}
\dosinglegroupempty\test {a}Text
\dosinglegroupempty\test Text

In the last line, #1 will not print the letter T, as would be 'normal' TeX behaviour. These macros can explictly take care of spaces, which means that the next definition and calls are valid:

\dotriplegroupempty\test {a} {b} {c}
\dotriplegroupempty\test {a} {b}
\dotriplegroupempty\test
{a}
{b}

And alike.

Just as their [], they also set the \ifXXXXargument switches.


< Prev: Expansion Control | Top: System Macros | Next: Definitions & Assignments >