78

Sometimes writing braces and omitting them produces the same result:

\tilde{\psi}  \mathrm{e}^{\mathrm{i} \pi}

vs

\tilde \psi   \mathrm e^{\mathrm i \pi}

Or (taken from Werner's comment below)

\frac{1}{2} vs \frac12

If that's the case, how bad for the quality of my code is omitting them? Of course, if one compiles a pair of formulas, there is no difference. But, since I ignore how TeX works, my point is: if one writes long codes with these habits,

  1. doesn't TeX go somehow crazy because I'm not writing as it expects?
  2. For instance, does it have any effect on how fast are long documents compiled? Any reason to keep them?
10
  • 8
    Here's another example where things are a bit more confusing and, in my opinion, suffers from legibility: $\frac{1}{2}$ vs $\frac12$.
    – Werner
    Nov 11, 2012 at 20:04
  • 11
    Same as asking if you should indent your code or keep the lines 80 or so character wide. Including braces is helpful because sometimes you might need to change something and might not realize to put the braces back e.g. p_1 to p_{01} and not p_01. Also it helps if someone else is going to read your code. In fact it is a good practice to write your code thinking that someone else will read it. At the end of the day it is a matter of personal choice.
    – mythealias
    Nov 11, 2012 at 20:12
  • 4
    Omitting the braces hinders legibility of source code. If the problem is that the braces are hard to type with your keyboard, then follow my usual advice: buy your keyboard with whatever layout you prefer, as long as it's "International". :)
    – egreg
    Nov 11, 2012 at 20:14
  • 12
    I like this question, and I love typing without any extraneous spaces or brackets. And I have no problems reading the stuff, so people who claim that it's more readable speak for themselves, just like I speak for myself... the point is that there's probably no good answer to this question. Nov 11, 2012 at 20:23
  • 6
    @Werner: Note that \frac12 is not Knuthian - in Plain Tex one writes 1 \over 2, which is more readable (if perhaps more dangerous) than \frac{1}{2}. Nov 15, 2012 at 12:29

6 Answers 6

43

(Disclaimer: The question clearly says "even when the result is the same", which I answer by saying that when the result is the same, the result is the same. However, there are (numerous) examples when the result is not the same, see e.g. Frank Mittelbach's answer!)


There is only one valid answer in my opinion: From the TeX's point of view, if there is no difference, there is no difference (what a nice tautology). So from the TeX's point of view, it is not "bad".

As people say in the comments, the second point is about the legibility of your code to humans, and this really depends on what human we're speaking about. You have people who are very strict on using braces, and you have people like me who got used to omitting them and got used to reading codes like \bar\theta, \frac12, \frac1n etc.


Mimicking David Carlisle's answer in What does \z@ do? and echoing egreg's comment, the following MWE

\documentclass{article}
\def\a{\setbox0=\hbox{$\frac12\ \bar\theta$}\relax}% No braces
%\def\a{\setbox0=\hbox{$\frac{1}{2}\ \bar{\theta}$}\relax}% Braces
\def\b{\a\a\a\a\a\a\a\a\a\a}%         10
\def\c{\b\b\b\b\b\b\b\b\b\b}%        100
\def\d{\c\c\c\c\c\c\c\c\c\c}%      1,000
\def\e{\d\d\d\d\d\d\d\d\d\d}%     10,000
\def\f{\e\e\e\e\e\e\e\e\e\e}%    100,000
\def\g{\f\f\f\f\f\f\f\f\f\f}%  1,000,000
\def\h{\g\g\g\g\g\g\g\g\g\g}% 10,000,000
\begin{document}
abc \h
\end{document}

compiles without braces in 1m11s and with braces in 1m13s. A minor (perhaps negligible) difference in time. This was on a linux machine with AMD Turion 0.55GHz.


UPDATE: Another test, with 100M instances of \frac12 vs \frac{1}{2} on a newer machine (Intel i5, 3.10GHz, linux) shows typical times 3m42s vs. 3m52s, that's a negligible difference of 100ns per \frac.

5
  • I did a simple benchmark and there doesn't seem to be any real time difference.
    – yo'
    Nov 11, 2012 at 20:38
  • 1
    @JorgeCampos A test with the expansion of a macro one million of times shows that stripping braces requires a tenth of a second more (on my machine). Since you quite certainly have less than 10000 calls like those, we arrive to less than a millisecond.
    – egreg
    Nov 11, 2012 at 21:03
  • No time difference indeed. I didn't mean to include the benchmark, I was asking tohecz to include the statement "there is no real time difference" in his answer :)
    – c.p.
    Nov 11, 2012 at 21:05
  • 1
    @tohecz: I've added a small timing test.
    – Werner
    Nov 11, 2012 at 21:31
  • 8
    I'm personally like to omit "unnecessary" braces when I think they help the readability, i.e., I would always write x_i not x_{i} but one should be very careful to limit this to the cases where it is not dangerous and there are a few -- see my answer. This is why it is considered "bad" from a LaTeX perspective! Nov 11, 2012 at 21:43
56

The LaTeX book consistently uses {} in all cases so all the examples use that form, you always see a^{2} even though everyone knows that a^2 works just as well and arguably improves the look of the source.

When the command taking the argument is a macro and the argument is necessarily a single token, most people drop the brackets as a matter of course so

\newcommand\foo{abc}
\setlength\textwidth{10cm}

rather than

 \newcommand{\foo}{abc}
 \setlength{\textwidth}{10cm}

and that is effectively standard LaTeX syntax now as well.

However the equivalence between a single unbraced token and a brace group is most consistently implemented for macros. For TeX primitives sometimes they are equivalent and sometimes not. So you can go

\fbox{a}    \fbox a

and they do the same thing (although I would never do the latter) but

\hbox{a}

boxes a but

\hbox a

produces

! Missing { inserted.
<to be read again> 
                   a

More problematic are places where people omit brackets, relying on the parsing rules of a TeX primitive, which then breaks when you later load a package that redefines the command to give extra functionality.

A common example is sub or super scripting. You can go

$a^\frac12b$

which produces a to the half times b, but often it would be nice to redefine the ^ character to be an active character (or \mathcode"8000 character) with definition defined by \def or \newcommand If you do that, any properly braced usage still works but an example such as the above would then be equivalent to

$a^{\frac}12b$

The frac would then raise an error that it was missing its arguments.

A usage such as

 $a^{\frac{1}{2}}b$

would work whether the arguments of ^ where parsed as a TeX primitive superscript or as a macro.

Thus users ignoring the rules in the LaTeX book and not always bracing arguments can cause problems and limit the scope of package designers. (The standard LaTeX2e \frac and \mathrm and friends were explicitly designed to cope with that unbraced superscript use, on the grounds that keeping users happy was probably better than assuming they followed instructions.)

51

There are a few places in TeX where braces are a required part of the syntax but those are few. When it comes down to arguments then TeX accepts a single token without surounding braces so \mathrm i would work if "typeset". But try this:

\newcommand\x{log}

\tableofcontents

\section{$\mathrm \x$}

and you will get a subtle error in your output (but no error message):

enter image description here

The reason is that \x is suddenly turned during processing into its components so what LaTeX sees when typesetting the TOC is $\mathrm log$ and thus only picks up "l" as the argument.

For this reason, the LaTeX manual claims that all arguments have to have braces even if this is technically not true.

Personally, I do not use braces in all such cases, but whenever the single token is a command one better think twice before omitting the braces because even if it appears to work it may not do so.

5
  • +1 for a very good example! I believe that something like \ProvideDocumentCommand exists that would overcome this problem?
    – yo'
    Nov 13, 2012 at 13:46
  • 3
    @tohecz what you are thinking of is \DeclareRobustCommand because that is what would prevent the expansion messing things up. But my point was simply to show that because TeX is a macro language we have to understand that data gets expanded and not just "processed" once in a while and that has its own rules. Which is why the statement in your answer which i translate to "it doesn't matter" is simply not true always ... it only doesn't matter sometimes and you need to know when which is non-trivial. And therefore LaTeX claims that they are always needed! Nov 13, 2012 at 16:12
  • I don't say that braces does not matter generally. The OP clearly asks "... evevn if the result is the same", which I answer by saying: "If the result is the same, the result is the same", which is certainly a true statement.
    – yo'
    Nov 13, 2012 at 19:50
  • 1
    @tohecz of course you are technically right: if there is no difference there is no difference. But my fear is that people reading your answer will take away from it not the wrong conclusion, i.e., not realizing that what appears to make no difference may be under certain circumstance result in a difference which is unfortunately the nature of the beast. Nov 13, 2012 at 20:02
  • I added a disclaimer, you're right that it seems to be necessary.
    – yo'
    Nov 13, 2012 at 20:53
17

Braces are not just for delimiting arguments.

Braces around arguments are generally only necessary if the argument consists of more than one token (according to TeX's rules). So \tilde{\psi} is the same as \tilde\psi, but \textbf psi will only make p bold.

One of the goals of LaTeX clearly was to tame TeX's free-wheeling syntax: TeX allows macros to be defined with arbitrary delimiters for the arguments, for example. This feature is nowhere to be seen in the LaTeX interface, though LaTeX uses it internally for the optional argument feature (\macro[optional argument]{obligatory argument}). Some TeX built-ins require braces (like \hbox, as David Carlisle wrote), while others prohibit them: you must write \vskip 1cm and not \vskip{1cm}. LaTeX introduces equivalents (\vspace) that always accept braces around arguments, and the LaTeX book further "standardizes" the syntax by always writing the braces, even when the language does not require them. Users of plain TeX don't seem to use cosmetic braces nearly as much as LaTeX users.

But in addition to macro arguments, there's another important function of braces: They delimit the scope of non-\global declarations. For example, the effects of \center, \raggedright, and font commands like \bfseries, \itshape and the (deprecated) \rm, \it, \bf persist until the end of the brace group that contains them:

This paragraph begins with regular text, {\bfseries switches to bold text} and
returns to regular text at the end of the group.

While braces around macro arguments are generally harmless, redundant braces can have puzzling effects-- especially in combination with paragraph formatting commands, which must still be in scope when the paragraph ends. The following snippet will not be set ragged right:

\begin{itemize}
   \item {\raggedright This is a paragraph of italic text, intended 
        to be typeset with a ragged right margin. But because the braces introduce
        a scope, the closing brace unsets the ragged right parameters before the 
        paragraph can be formatted.}
\end{itemize}

No braces are needed after \item, of course; but some people use them in the mistaken belief that \item takes an argument, and that this style of coding is more "readable". The brace group ends just before the paragraph does, so the paragraph is justified like the rest of the document. (Instead of dropping the braces, the snippet could also be fixed by ending the paragraph inside the brace group: ... can be formatted.\par}

Conclusion: Braces are an integral part of TeX's syntax. LaTeX style encourages using them more than the TeX engine requires, but some redundant braces are not harmless. A basic understanding of their function helps one know when they can be omitted, and when they should be. Some handy principles:

  1. Braces delimiting a macro argument do not create a scope (they are consumed on parsing the argument). However, some commands will themselves introduce a brace group in their expansion. The following will not be typeset ragged right, because the implementation of \emph expands its argument inside a brace group:

    \emph{\raggedright Some text I tried to typeset italic and ragged right.}

  2. The braces around a macro definition also do not create a scope, allowing us to write things like

     \newcommand\mybold{this: \bfseries}
     Everything after \mybold is now set in boldface.
    

    which produces:

    Everything after this: is now set in boldface.

  3. LaTeX's \begin{something} and \end{something} macros internally add a brace group, so they are scope delimiters.

2
  • AFAIK, LaTeX does use the "delimited arguments" in picture environment.
    – mbork
    Nov 18, 2012 at 12:49
  • 1
    Hmm, you mean the arguments delimited by parentheses, @mbork? That's another case of "internal" use, I'd say. LaTeX uses the feature to define its command syntax, but you can't specify arbitrary delimiters to your own macros with \newcommand, \ProvideCommand, etc.-- and since the LaTeX Gods direct us to only use the LaTeX interface, arbitrary delimiters are effectively forbidden. (With the very partial exception, now that I think about it, of the \verb command.)
    – alexis
    Nov 18, 2012 at 19:35
16

no one seems to have mentioned that latex files are sometimes converted automatically to (for example) xml/mathml, and such converters may require scoping by braces where (la)tex itself does not.

3
  • If a conversion tool is so simplistic that it cannot apply TeX's tokenization rules, it's not going to be very useful anyway.
    – alexis
    Nov 13, 2012 at 20:22
  • 5
    @alexis -- it has often been pointed out that only tex can effectively parse and process tex code. witness the file xii.tex. i'll leave it at that. Nov 13, 2012 at 21:14
  • Certainly, but the lack of braces around tokens is not what makes this difficult.
    – alexis
    Nov 13, 2012 at 21:37
13

I like to write as few brackets as possible. I "learned" this style after reading the TeXbook, since Knuth defines many macros in such a way that no brackets are needed.

I find his style much nicer and economical than LaTeX's usual definitions, where one needs to use brackets in many places, e.g. \chapter{Some title}. His macros are usually terminated by \par, so you write

\beginchapter Some title

and you're done (you just need to make sure to have a blanck line following it). The definition is in manmac.tex in case you're curious (it actually requires the chapter number followed by a dot before the title, but the idea is what counts here).

For another example, look at how his "letter" format on pag. 403 requires no brackets in any place. That's sheer elegance, IMHO.

After reading the TeXbook I realized how superflous brackets can be if you define your macros conveniently.

That's just one aspect of the use of brackets though.

1
  • 2
    That's ok for plain, but bad for LaTeX (as @alexis explains). Nov 16, 2012 at 14:21

You must log in to answer this question.

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