[Tex/LaTex] Meaning of {} and [] in LaTeX syntax

latex-miscmacrossyntaxtex-general

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.

Best Answer

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.

Related Question