16

I'm somewhat new to LaTeX, and since I'm used to more traditional OO programming languages like Java, C, or Python this is bugging me as it seems like very basic syntax for LaTeX. I'm wondering if there's a good general resource for this level of syntax as I feel like this would be useful for writing LaTeX packages too.

I'm kind of confused as to what the distinction between {} and [] is in LaTeX as it seems like both serve as a way of specifying parameters, but it seems like other LaTeX may work inside {}. I want to say {} is just generally a syntax block, but it's not quite analogous to it's usage in Object Oriented which is probably why I would want to jump to syntax blocks anyway.

Ultimately LaTeX has a lot more in common with markup than object oriented programming from what I've seen, so making a comparison with OOP might not always make sense here. That said, I can't help but notice a functional analogy with \command{something} being a function call (or even a C macro usage perhaps) essentially, which is probably getting into defining commands a bit too. I'd be interested in understanding if this has any connection for LaTeX command definition or if that's purely coincidental similarity and there's a completely different meaning and explanation for that.

31

LaTeX uses { ... } to denote a required argument to a command, whilst [ ... ] is optional. The use of { ... } comes through from the underlying TeX set up (or at least how it is usually set up following ideas from D. E. Knuth). Thus for example both

\documentclass{article}

and

\documentclass[11pt]{article}

are valid LaTeX as the first argument is optional (square brackets), whereas the second one is required. As a result, you get a (low level) error if you do something like

\documentclass[11pt] % Oops, no mandatory argument!
\begin{document}

As noted in a comment, the pairings ( ... ) and < ... > are seen in some contexts, as they provide a way of marking up particular types of argument. In the LaTeX kernel, ( ... ) is used to specify co-ordinates for material in picture mode, and this is picked up by other packages. The < ... > use is possibly best known in beamer, which uses these delimiters to show an argument related to the overlay nature of commands when making presentation slides.


On commands, note that LaTeX is written in TeX, a macro expansion language, and uses various primitives (built-ins), some of which work by expansion. As a result, whilst one sometimes talks about 'functions' in LaTeX, this is informal and should not be taken literally.

  • 4
    I wonder if it's worth mentioning that the choice of delimiter is also somewhat up to the package author. There are some packages that use <..> and (...) as well. – Alan Munn Mar 7 '17 at 22:57
12

From a user point of view (I am not a programmer), as Joseph Wright have explained, the main point is that {} is usually a mandatory argument, whereas [] is the optional argument, but beside exceptions (e.g.: \only<2>{...} in Beamer), I will remark also that {...} could be not the argument of any command, but a group delimited by tokens of category codes 1 and 2 respectively, that can limit the scope of any command, unless explicitly indicated otherwise, so that outside the group the old values are restored.

In other words, { and } are not only argument delimiters of macros, but the equivalent of \bgroup and \egroup commands, that are similar (but not equivalents) to TeX primitives \begingroup and \endgroup.

For example, the command \em do not take any optional or mandatory argument. Just affect any following text, but you can enclose this effect in a group, so {\em text} text is equivalent to \bgroup\em text \egroup text and \emph{text} text but not to \em{text} text. The last case look like a command with mandatory argument but is simply a command without arguments followed by a group.

The effect of grouping and nested grouping is not always evident for novices. Some like "word {word} word" have no any effect, as well as "word {word }word" but is not the same that word {word } word, and most important, one should understand why some code as \def\xx{yy} \xx or \def\xx{yy} {\xx} work, but {\def\xx{yy}} \xx produce a fatal error.

For more information about groups, run texdoc texbytopic and see the chapter 10 (Grouping, page 105).

On the other hand, square brackets are confusing at first, when are not the optional argument of some command by comparison with the {...} grouping, because unlike the curly brackets, they are not active characters usually, so a [ ... ] could be surprising for a novice because is not some kind of grouping, but simply the text "[...]". As Sigmund Freud would say, sometimes a bracket is just a bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.