What does it mean?
To repeat the factual information that Martin gave: defining a macro like
\def\macro<other arguments>#{<replacement text>}
accomplishes the functional equivalent of trying to write
\def\macro<other arguments>{{<replacement text>{}
except that it works, because the above breaks two rules of macro definitions. First, it can't function as intended because TeX, when scanning a \def
, will read until the first {
to find the arguments, and once it reaches that brace, will assume that the replacement text has started. So you can never actually have {
in the arguments. And second, while scanning the replacement text, it will skip over balanced brace-pairs, so the opening {
you try to insert will prevent it from stopping its absorption of the replacement text at the following }
. (You can get around this one by using \bgroup
, but the first problem is unsolvable without #{
.) So this syntax really covers the one logical case of delimited arguments that is excluded by these parsing rules.
To answer your last question, yes, you can write
\def\bigmacro{
\def\macro##{<stuff>}
<more stuff>
}
so that when calling \bigmacro
, it begins by defining \macro
to have a "delimited brace" after it. As egreg and Martin have said, doubling the #
is just part of TeX's syntax.
Why does it work that way?
Okay, there's a more general situation that's excluded by the rules, namely, the putative
\def\macro<args>{<more args>{<replacement>}
where you want to match a {
right in the middle of the arguments. You can't do that with #{
. I don't know for sure, but it is consistent with the way things work that this is so that no part of TeX's argument scanning in macro expansion will destroy brace pairs. You see, as it currently stands, #{
will use an opening brace as a boundary for argument grabbing, but will not include it in the grabbing, because it is put back after being found. This means that a group {...}
remains intact.
If you think about how the rules work, this always happens in unrestricted scanning. You can destroy a group with \let
, but that's because you are saying "look for the next token"; it's not possible to write \let\a={<stuff>}
; or rather, it would result in \let\a={
followed by <stuff>}
(and so probably an error). But with macro argument scanning, TeX just looks until it finds something to call #1
, and since the rules allow using {...}
to form a "group" that can be caught, it's important not to cross the boundaries of brace pairs. Someone might have written them intentionally to signal to the argument scanner.
Another reason not to break brace groups is, of course, that if you write
{\macro <stuff> {<more stuff>} ...
and if \macro
could somehow catch the opening {
, then <stuff>
and <more stuff>
would end up in the same group, perhaps against the expectations of whomever wrote it. That would cause some problems with \aftergroup
, at least. (I admit that this is an incredibly technical point, but then, so is #{
.)
When is this construction useful?
Sanitizing
Well, I found it useful in this answer of mine. The question was how to expandably convert TeX code into "plain text", roughly, and I needed to use only macro expansion to find groups in that code so I could remove them (and also so I could work around their effects in inhibiting my own code). So part of my sanitizer is a macro defined like
\def\SanitizeGroups#1#{
the effect of which is that I know that #1
is everything between the appearance of \SanitizeGroups
and the first following group. In other words, #1
contains no groups and right after it there is a group. That means that if I use a macro like
\def\SanitizeTokens#1{...}
to parse individual "tokens" of the text following \SanitizeGroups
, I can be sure that everything picked up by a call to \SanitizeTokens
is actually a single token and not, say, something inside {...}
.
Verbatim arguments
Another use: in this answer to a question about the #{
construction, Philippe Goutet presents an example from a Tugboat article:
\def\bold#{\bgroup\bf\let\next= }
which means that \bold
will simply "insert \bf
at the beginning of the immediately-following group"; the purpose of #{
here is to make sure that when you use \let
to swallow the opening brace of that group, it really does swallow a brace. This forces the user to write \bold{<text>}
rather than, possibly, \bold a
to get a single bold letter a
, but then, you have to use braces with \bf
anyway so it's not much of an imposition. The reason for doing it this way rather than
\def\bold#1{{\bf #1}}
(the "Programmer" approach, only the third level of TeXpertise out of seven in that article) is that if you don't read the stuff in the braces, its catcodes are not fixed, so it can contain verbatim text.
##
pair in a replacement text is changed into a single#
when the macro is expanded.latex.ltx
. Search for#{
and you'll find all the places where it is used and will easily understand the meaning, eg\documentclass[...]{...}
for grabbing the [...],