7

Simple question, maybe duplicate but can't find it.

I see some people (especially novices) writing something like

\textbf {text} %or
\frac {1} {2}

It looks not good with the spaces between the command and arguments but anyway compiles without errors.

I guess there is a downside of coding like that. But which one?

Update

Just in this moment I saw this:

\overline T

Why is this working without any error? Looking at this, indices and exponents are coming to my mind, they are working also without curly braces x_a^2

So I guess it is (all) uncritical.

2
  • 1
    The braces part is a duplicate of tex.stackexchange.com/questions/82329/… Sep 19, 2014 at 13:58
  • 3
    a space after a command name isn't tokenised so it really isn't there as far as TeX is concerned. spaces between arguments are tokenized but skipped while scanning for arguments so have no effect either Sep 19, 2014 at 13:59

2 Answers 2

8

At many places those spaces do not harm:

  • After command names (consisting of letters) spaces are ignored as "end of command name"). Example: space in \textbf {text}.
  • TeX refuses to take a space as undelimited argument unless it is surrounded by braces. Example: second space in \frac {1} {2}
  • LaTeX uses the \@ifnextchar for the optional star and optional arguments in square brackets. Then spaces are ignored by \@ifnextchar. Examples: \\ * [2ex] or \\ [2ex] (the space can also come from a line break). Therefore something like \\\relax [2ex] or \\{} [2ex] would be needed, if [2ex] should not be considered as optional argument for \\.

Spaces can enhance readability, but it is also some matter of taste, which coding style is preferred.

Care is needed for:

  • Undelimited arguments.
  • Different catcodes of the space, especially if the command switches to some kind of verbatim modes.

Argument token/group

  • Often the curly braces can be dropped, if the argument consists of one token only: \overline{T} = \overline T.

  • But again care is needed. Sometimes curly braces look like argument braces, but they are not:

    \detokenize\foo
    

    will not work, the braces are necessary here:

    \detokenize{\foo}
    

    The closing curly brace must be given explicitly, but the opening one can also be given implicitly:

    \detokenize\bgroup\foo}
    
  • A macro can also be defined in a way, that they require the curly argument braces, usually by using a # at the end of the parameter text. Then a curly brace must follow, when using this macro. Example is \textcolor:

    \def\textcolor#1#{\@textcolor{#1}}
    \def\@textcolor#1#2#3{\protect\leavevmode{\color#1{#2}#3}}
    

    The argument with the colored text must have argument braces. This is the price. The gain is an efficient implementation for passing an optional color mode argument to the corresponding \color command.

5
  • there's (at least) one exception to the "space between double backslash and [...]" case. because a line in a multi-line display starts annoyingly frequently with a term in brackets, amsmath goes to some lengths to not ignore that space, so that only dimension directives that follow immediately, as \\[3pt] are recognized as such; if a space intervenes, the [3pt] will be printed. this is annoying, but less confusing than the ! Missing number, treated as zero. message that happens otherwise. Sep 19, 2014 at 14:41
  • Actually \newcommand\foo\bar works, but just by chance.
    – egreg
    Sep 19, 2014 at 14:43
  • @egreg Thank, I am now using \detokenize as example. Sep 19, 2014 at 15:00
  • What exactly is an undelimited argument? (Maybe a German translation for it?) Sep 19, 2014 at 15:28
  • @RafaelWörner Mandatory arguments, defined by \newcommand are undelimited arguments. Delimited arguments ("Argumente mit Begrenzern") are read until their delimiting tokens are found (and the curly braces must be balanced properly). The arguments in square brackets are usually delimited arguments, another example: (...,...) of \put. Sep 19, 2014 at 15:32
5

Of the examples you give, only this one makes any difference:

\textbf {text} %or
zzz

Putting a space before a comment introduces a space, the above will typeset as text zzz

Compare with

\textbf {text}%or
zzz

which typesets as textzzz

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .