System Macros/Handling Arguments

From Wiki
< System Macros
Revision as of 17:25, 1 August 2006 by Mojca Miklavec (talk | contribs) (Basic navigation)
Jump to navigation Jump to search

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

Argument grabbing and handling

This 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.

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.

These macros are trivial, but quite important in some applications. Here is the definition, which is also the example code:

\long\def\firstofoneargument     #1{#1}
\long\def\firstoftwoarguments    #1#2{#1}
\long\def\firstofthreearguments  #1#2#3{#1}
\long\def\firstoffourarguments   #1#2#3#4{#1}
\long\def\secondoftwoarguments   #1#2{#2}
\long\def\secondofthreearguments #1#2#3{#2}
\long\def\secondoffourarguments  #1#2#3#4{#2}
\long\def\thirdofthreearguments  #1#2#3{#3}
\long\def\thirdoffourarguments   #1#2#3#4{#3}
\long\def\fourthoffourarguments  #1#2#3#4{#4}

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.

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