11

From the list of catcodes, \ starts a new control sequence, { starts a group, and } ends a group. In LaTeX, a macro appears as

\foo[opt-arg]{arg1}{arg2}

However, TeX does not seem to have the notion of arguments to control sequences, so what is going on here?

  • 1
    What do you mean by “TeX does not seem to have the notion of arguments to control sequences”? Is “optional” missing before “arguments”? – egreg Aug 31 '15 at 12:53
  • I mean, how can an argument be implemented as a group. Also there is no square brackets among the predefined catcodes. – user877329 Aug 31 '15 at 12:54
  • Well, TeX has the notion of arguments to control sequences, which is one of the main points about macros. Optional arguments in LaTeX are implemented with delimited arguments. – egreg Aug 31 '15 at 13:02
24

A macro is any control sequence (or active character) defined with \def, \gdef, \edef or \xdef. TeX macros support up to nine arguments, which contradicts your statement about it not having the notion of arguments to control sequences.

The most common usage of arguments is in the “undelimited” form; say that you do

\def\foo#1{--#1--}

so \foo takes an argument. When \foo is expanded at point of use, TeX looks for the next token; if it is { (a character token with category code 1, to be precise), then TeX looks for the whole list of tokens up to the matching } (a character token with category code 2, to be precise), then strips off the braces and replaces \foo{<...>} with

--<...>--

(where <...> represents the token list determined as specified). Otherwise the next token <token> is used for the replacement and one gets

--<token>--

So, for example, \foo{abc} and \foo x will result in, respectively

--abc--
--x--

Note that \newcommand in LaTeX uses \def with undelimited arguments, unless it has two optional arguments after the name of the command to define. The above definition in LaTeX would be

\newcommand{\foo}[1]{--#1--}

However, arguments can also be delimited; if we do

\def\foo[#1]#2{--#1--#2--}

then the first argument to \foo will be anything that appears between [ (that is required to follow \foo) and the next (required) appearance of ] at the same brace level; then the second argument will be determined as before. So \foo[x]{yz} will result in

--x--yz--

(the delimiter tokens will disappear, together with a possible set of outer braces, if it doesn't leave unbalanced braces).

LaTeX uses this feature for introducing commands with optional arguments. A definition such as

\newcommand{\foo}[2][default]{--#1--#2--}

will set up thing so that \foo{abc} and \foo[x]{abc} will result, respectively, in

--default--abc--
--x--abc--

Very basically, the above \newcommand defines two macros, \foo and \fooOPT; when \foo is expanded, TeX looks whether [ follows. In this case it is replaced by \fooOPT which is then expanded normally, having been defined as

\def\fooOPT[#1]#2{--#1--#2--}

If no [ follows, \foo is replaced by \fooOPT[default]. Actually, \fooOPT is \\foo (with a backslash in the name, but it's mostly irrelevant). The definition of \foo is (very basically):

\def\foo{\@ifnextchar[{\\foo}{\\foo[default]}}
14

{} have two distinct uses in TeX, as grouping and to delimit macro arguments (when they do not form a group).

Ignoring latex specifics and tests that make the argument optional, then

\foo[arg1]{arg2}{arg3}

is defined in tex primitives as

\def\foo[#1]#2#3{something using #1, #2 and #3}

so a use such as

\foo[abc]{xyz}{123}

then #1 is abc as the argument is delimited by [ and ] (actually in latex the argument would just be delimited by ] as the [ is removed while checking for the optional argument), #2 is xyz and #3 is 123 as these arguments are undelimited arguments so take a single token or a matching brace group and the braces around arguments 2 and 3 are removed while scanning for the arguments and do not form a group, so the end result is

something using abc, xyz and 123.

The argument scanning is built into the tex macro language in tex-the-program, not something implemented in the format.

  • 1
    your proofreading skills do need attention. – barbara beeton Aug 31 '15 at 14:00
  • @barbarabeeton it's all in vain anyway that other bloke got the tick! – David Carlisle Aug 31 '15 at 14:13
  • Possibly because of the typos. ;-) – egreg Aug 31 '15 at 14:31
  • 3
    Both of them had the advantage that I was out with my family :-) – Joseph Wright Aug 31 '15 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.