3

I know that LaTeX gobbles up whitespace after macro names such that there is no space between the å and the b in

\documentclass{article} \begin{document} \aa b \end{document} 

enter image description here

I have a similar question about macros with optional arguments, e.g.,

\documentclass{article} \newtheorem{defn}{Definition} \begin{document} {\defn[Gauss] Y} {\defn[Gauss]Y} \end{document} 

Here, the whitespace after the ] is removed and replaced by some constant space in the output.

(I know that you shouldn't use \defn like that, but I couldn't come up with a better example.)

Is the internal logic to absorb all whitespace after the optional argument or is something else at work here?

enter image description here

1
  • Multiple whitespaces are always gobbled, not only with (optional) arguments. Try \documentclass{article}\begin{document}Hello World\end{document} for example. Commented Aug 21, 2022 at 8:50

2 Answers 2

6

If you're used to do

\newtheorem{abc}{Abc} 

(or similar instruction) in the preamble and then

{\abc text} {\abc[opt] text} 

then don't. Use the proper

\begin{abc} text \end{abc} \begin{abc}[opt] text \end{abc} 

for several reasons, the main one being that you're not executing \endabc.

The fact that \begin{abc} or \begin{abc}[opt] “gobbles” all spaces and replaces them with a fixed amount of horizontal space is a feature of the particular command.

The internal \abc command (not to be used in the document) issues \par and stores the possible optional argument and then delivers it together with the heading when a paragraph is started. So spaces are ignored because they're scanned when TeX is in vertical mode.

2

LaTeX doesn’t gobble whitespace after optional (or obligatory) arguments – at least not any more than it always does (i.e. multiple consecutive simple spaces/tabs are collapsed to a single space):

\documentclass{article} \newcommand{\foo}[1][]{foo#1} \begin{document} % output: fooab \foo[a]b % output: fooa b \foo[a] b % output: fooa b \foo[a] b % output: fooa b \foo[a]\ \ b \end{document} 

Presumably \newtheorem internally defines the resulting command in such a way that it always has a constant amount of whitespace after it, but that’s not part of the default behaviour of TeX commands.

The gobbling of whitespace after option-less commands is simply due to a problem of delineation: Without a space as in \aab LaTeX would have no way of knowing that the command name ends after the second a so there needs to be some marker to signal the end of the command. That can be whitespace but you can also use {}: \aa{}b (and using both, \aa{} b, gives you whitespace in the output because the {} already signals the end of the command to LaTeX). But if you have any kind of arguments, optional or obligatory, the end of the command name is unambiguous so there is no need to gobble whitespace.

(EDIT: Actually \aa{}b is not exactly equivalent to \aa b because the former adds a zero-width token after \aa. Most of the time the result is the same but the token can make a difference at times, for example for ligatures (fi looks different from f{}i), for kerning, or for certain commands which work on the following token such as \' or \^. Thanks to David Carlisle for this insight!)

Also as a side note, as far as I know the recommended way of using theorems is with environments:

\begin{defn}[Gauss] Y \end{defn} 

But I guess you just used \defn for demonstration purposes?

2
  • 5
    note \aa b and \aa{}b are not equivalent one is åb the other is å{}b which can affect kerning and ligatures, and for commands other than \aa could have radically different effects Commented Aug 21, 2022 at 9:42
  • Thank you for the remark; I wasn’t aware of that! I will add it to my answer above. Commented Aug 22, 2022 at 7:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.