add tag
AndréC (imported from SE)
I don't quite understand the special rule that follows texbook exercise 20.5:

[![screenshot][1]][1]

If I understand correctly (which I don't), if the opening parenthesis `{`which starts the replacement text is immediately preceded by a hash character `#` like this `#{` then this opening parenthesis is at the same time a delimiter which marks the beginning of the replacement text and indicates that this parenthesis is also part of the parameter text (which it can never be in the general case). 

But then, I don't understand why this `\def\a#1#{\hbox to #1}` definition applied to the `\a3pt{x}` example is extended to `\hbox to 3pt{x}`. 

Why is there the "x" in `3pt{x}`?

I don't understand the explanation given:

> because the argument of \a is delimited by a left brace

Translated with www.DeepL.com/Translator (free version)


  [1]: https://i.stack.imgur.com/QBy0y.png
Top Answer
Skillmon (imported from SE)
You can think of it like this:

```latex
\def\foo#1;{}
```

will read everything after `\foo` until there is a `;`, so `\foo 123;` will read `123` as `#1` and remove `;` from the input stream as well.

Similarly

```latex
\def\foo#1#{}
```

will create a macro that has a right delimited argument, but instead of `;` this right delimiter is `{`, so it reads everything up to the next opening brace, *but* contrarily to the first case, in this case `{` will not be removed from the input stream (well, actually it will be removed but reinserted by `\foo`'s replacement text). So `\def\foo#1#{}` behaves like `\def\foo#1;{;}` but uses `{` as the right delimiter instead of `;`.

You can see that the opening brace is actually removed and later reinserted by taking a look at the `\meaning` (or `\show`ing the definition):

```latex
\def\foo#1#{}\show\foo
\def\foo#1#{foo}\show\foo
```

will print

>```latex
>> \foo=macro:
>#1{->{.
>> \foo=macro:
>#1{->foo{.
>```

to the terminal.

----

**EDIT:** This tries to answer the comment

>As I understand it, the parameter text is a regular expression. In this regular expression the opening parenthesis `{` is a delimiter of the replacement text and is therefore removed when replacing. This is not the case when it is immediately preceded by `#`. What is the purpose of this rule?

If TeX would also remove that opening brace what would be left would be an unbalanced token list (an unmatched closing brace). Therefore if the following macros aren't created very carefully, this would throw an error, and creating macros with the same logic would be much harder. So the only purpose of this rule is that you can create macros which are right delimited by a token of category code 1 (a `{` in normal catcodes) without needing a lot of macros to sanitize the now unbalanced input stream.

Imagine the following situation:

```latex
\def\foo#1#{}
\foo 123{abc}
```

After the definition of `\foo` and its expansion what would be left in the input stream if the `{` wasn't reinserted would be

```latex
abc}
```

and we'd have to somehow sanitize that unmatched closing brace. Say we want to create a macro which reads everything up to an opening brace and the next group, what we'd have to do now would be to create a macro that grabs every token until it meets a closing brace, but `\def\bar#1}{}` will throw an error as well, so how should we create this? What we'd need to do would be something like the following (note that I create the unbalanced text by expanding an `\iffalse{\fi` in the following):

```latex
\documentclass[]{article}

\makeatletter
\long\def\grabuntilclosingbrace@fi@firstoftwo\fi\@secondoftwo#1#2{\fi#1}
\def\grabuntilclosingbrace
  {%
    \begingroup
    \aftergroup\grabuntilclosingbrace@done
    \grabuntilclosingbrace@a
  }
\def\grabuntilclosingbrace@a
  {%
    \futurelet\grabuntilclosingbrace@tok\grabuntilclosingbrace@b
  }
\def\grabuntilclosingbrace@b
  {%
    \ifx\grabuntilclosingbrace@tok\egroup
      \grabuntilclosingbrace@fi@firstoftwo
    \fi
    \@secondoftwo
    {%
      \afterassignment\grabuntilclosingbrace@final
      \let\afterassignment@tok=%
    }
    {%
      \grabuntilclosingbrace@c
    }%
  }
\def\grabuntilclosingbrace@final
  {%
    \aftergroup\grabuntilclosingbrace@end
    \endgroup
  }
\long\def\grabuntilclosingbrace@done#1\grabuntilclosingbrace@end
  {Argument was: \texttt{#1}}
\long\def\grabuntilclosingbrace@c#1{\aftergroup#1\grabuntilclosingbrace@a}
\makeatother

\begin{document}
\expandafter\grabuntilclosingbrace\iffalse{\fi abc}
\end{document}
```

And this macro can't deal with spaces or nested groups. See how complicated life would be, if TeX didn't give us the super handy `\def\foo#1#{}` rule?


----

# If you want to know, what this rule can be used for:

Say we have some macro that can't deal with nested groups in its argument, so we have to test whether the argument has a group, after all we want to give a helpful error message instead of just letting our macro fail. So we need to create a test that tests for a nested group. With the logic of `\def\foo#1#{}` we can reduce this to a test whether an argument is empty (this reuses code/ideas from https://topanswers.xyz/tex?q=474#a510).

```latex
\documentclass[]{article}

\makeatletter
\long\def\ifgroupin#1%
  {%
    \ifgroupin@a#1{}\ifgroupin@tokB\ifgroupin@false
    \ifgroupin@tokA\ifgroupin@tokB\@firstoftwo
  }
\long\def\ifgroupin@a#1#{\ifgroupin@b}
\long\def\ifgroupin@b#1{\ifgroupin@c\ifgroupin@tokA}
\long\def\ifgroupin@c#1\ifgroupin@tokA\ifgroupin@tokB{}
\long\def\ifgroupin@false\ifgroupin@tokA\ifgroupin@tokB\@firstoftwo#1#2{#2}
\makeatother

\begin{document}
\ifgroupin{abc}{true}{false}

\ifgroupin{a{b}c}{true}{false}
\end{document}
```

An alternative version which uses the classic `\if\relax\detokenize{#1}\relax` empty test, because this might be easier to understand (but takes about 160% the time of the previous implementation):

```latex
\makeatletter
\long\def\ifgroupin#1%
  {%
    \if\relax
      \detokenize\expandafter\expandafter\expandafter{\ifgroupin@a#1{}}\relax
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }
\long\def\ifgroupin@a#1#{\@gobble}
\makeatother
```

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.