On pp. 203–204 of the TeXbook, Knuth describes how TeX absorbs macros' arguments: delimited go until the first appearance of the delimiter, and undelimited go until the next token, in both cases respecting brace groups. But then he says:
In both cases ,if the argument found in this way has the form
'{<nested tokens>}'
, where<nested tokens>
stands for any sequence of tokens that is properly nested with respect to braces, the outermost braces enclosing the argument are removed and the<nested tokens>
will remain.
This is a very annoying feature for delimited arguments! For example, in this question, there is no good way to deal with a macro \macro#1\pgfeov
where #1
may either be a single braced argument or a pair of arguments, because the braces are removed by the argument scanner and then the result (if not double-braced) looks like multiple tokens and so is caught (by an inner macro) as more than one argument even though I put braces around it.
I consulted the TeXbook in the hopes that some kind of explanation would be given, but there is not even an exercise on the subject. Can anyone suggest why this is a useful feature?
Edit: I want to clarify that I am not talking about "arguments" in the imperative-language sense where one tries to treat macros as functions, passing its inputs as, say, \macro[#1]<#2>(#3,#4)
. I mean them in the sense described above: token lists absorbed by TeX by parsing text.
I also want to add that I am less interested in workarounds than in the answer to the question in the title: why (philosophically) does TeX work like this? For example, I know that the reason that spaces are ignored after control words is that if they were not, there would be no way to omit a space after one, since the scanner will keep piling on letters to the macro name until it no longer can. So there is a benefit to this rule; it makes the language more powerful.
Best Answer
One thing to note is that TeX's only means of nesting arguments are braces. You can define a macro
\def\whatever[#1]{...}
but when you call it as\whatever[oh[well]]
, things go down the drain awfully. Calling it as\whatever[{oh[well]}]
however works swimmingly, and\whatever
never notices it has been taken for a ride by slipping a]
into its argument. So the braces can be used as a means of hiding occurences of the closing delimiter from TeX without actually affecting the intended argument.It also means that whenever you call a macro using delimited arguments with a non-literal argument (more exactly, an argument not completely under your own control, as it often happens when you write a macro package to be used by others), you should always add a layer of braces around each delimited argument, like
\whatever[{#1}]
or similar. There is no other way to ensure that arguments will not get chopped up into something different because they themselves may contain a closing bracket.