8

This is probably a duplicate, but since Google doesn't allow me to search on the curly braces { or } I cannot find the information.

The question: why can I just do {Some text}?

How do I call this? I seem I lack some knowledge to put this into Latex terms. If I would guess, then I'd be inclined to say that this is possible, because { } defines the scope of a new undefined or standard environment.

But I don't know if that's what it's doing.

Here's a minimal working example.

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

{check this out}

check this out

\end{document}
Peter Grill
  • 223,288
  • Do you want to print braces? Use \lbrace or \{ for the left one and \rbrace or \} for the right one. – Astrinus Dec 20 '14 at 20:53
  • You mean \{ and \} ? –  Dec 20 '14 at 20:53
  • I don't mean printing them. I mean why does this work? Why doesn't it give a syntax error? I would expect that it would crash at {check this out}. From my understanding { and later on } are invalid tokens. But they aren't, so my question is: what do they do in this case, and where can I read up on it? I'm used to the idea that { is preceded by some kind of macro name. E.g. \begin – Melvin Roest Dec 20 '14 at 20:59
  • 4
    It doesn't crash as it is valid syntax. The braces define a group, which means that changes will be local to that group. Just because you don't have any settings in there to keep local doesn't make it invalid syntax. For instance, xxx {\bfseries abc} yyy will make only the abc to be bold - the yyy will be in normal font. The braces keep the \bfseries local to within the { and }. – Peter Grill Dec 20 '14 at 21:05
  • Thanks, I knew it was something basic. I didn't know what a group was, now it make sense. – Melvin Roest Dec 20 '14 at 21:10
  • @PeterGrill Looks like an answer to me ;-) – Joseph Wright Dec 20 '14 at 21:25
  • Haha yea it is :) – Melvin Roest Dec 20 '14 at 21:26
  • 1
    you also asked where to read about this. there's a nice chapter on "grouping" in victor eijkhout's tex by topic -- texdoc texbytopic. probably more than you wanted to know, but a good resource for anything about tex itself (not latex). – barbara beeton Dec 20 '14 at 22:02
  • 1
    @MelvinRoest Just note than, apart from a group when used alone, they act “different” when a macro is scanning for its argument. When macro \foo looks for one argument, if it finds { as a following token, the content between {..} will be the argument (and this has nothing to do with groups). If the macro doesn't find a { then it takes the first token (e.g., \foo abc will be the same as \foo{a}bc). – Manuel Dec 21 '14 at 04:04

1 Answers1

13

Surrounding text with braces, i.e., {text} doesn't result in a error as it is perfectly valid syntax.

The braces define a group, which means that changes will be local to that group. In this case there aren't any settings in the group to keep local, but it is still valid syntax. For instance,

xxx {\bfseries abc} yyy 

results in

enter image description here

Note that only the abc is in bold - the yyy is in normal font. The braces keep the \bfseries local to within the { and }.

Besides grouping there are some other uses of curly braces:

1. Text Mode:

In text mode, curly braces can be used to control the spacing after a control word. So

\TeX code  {\TeX} code

yields:

enter image description here

where the space following the control word is removed without the use of {}.

2. Math Mode:

In the math mode, adding curly braces controls has additional effects. For instance, braces can affect the spacing around "math atoms". (Upshot: Don't add curly braces unnecessarily when in math mode.)

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{showexpl}

\lstset{ language={[LaTeX]TeX}, backgroundcolor=\color{yellow!40}, basicstyle=\small\ttfamily, }

\begin{document}

xxx {\bfseries abc} yyy \quad \TeX code {\TeX} code

\begin{LTXexample}[pos=r] \par $a=b$ \par $a{=b}$ \par ${a=}b$ \par $a{=}b$ \end{LTXexample} \end{document}

Peter Grill
  • 223,288