27

I'd like to gain some intuition as to the choices of brackets versus braces in function arguments. As in [...yadda...] versus {...yadda...}.

I'm a long-time user of LaTeX but am poorly educated as to the fundamentals. Whenever I try to guess/recall an old function I always seem to confuse if it should be: \somefunction{argument1}[argument2] or something like \somefunction{argument}{argument}.

Is it really ad hoc, like Herbert says here, or is there at least a semblance of logic to the patterns?

EDIT: Thanks all for your lucid responses. I found the discussion here to also be very helpful. I'm especially grateful for the links to TeX by Topic by Victor Eijkhout.

4
  • 4
    The rule "brackets for optional arguments, braces for mandatory" applies, but there are exceptions, and it's not always easy to know if an argument is optional or not. However, one thing is certain : syntax like \somefunction{aaa}[bbb] are very rare : usually optional come first, mandatory after. Note that you can format sourcecode by enclosing it in ` (backticks).
    – T. Verron
    Nov 28, 2012 at 18:42
  • 2
    I think the main reason for choosing braces for macro arguments are that they play the least important role in math typesetting as seen by Knuth. Nov 28, 2012 at 18:44
  • @T.Verron, you may think that's very rare syntax, but the macro (I guess? I can see how it's not a function...) that's probably the most-often used in my hands goes \begin{figure}[float-specifiers]. Maybe that's why I think my intuition's off -- the pattern I see most often is not representative?
    – Ryan
    Nov 28, 2012 at 19:06
  • 3
    I guess \begin can be technically seen as a macro, but it is definitely a particular case. In that case, the float-specifiers are optional arguments to the figure environment, rather than to the \begin "macro". And by the way, this specific placement of the optional after the mandatory leads to erratic behavior, such as the bugs of array for example if the first cell starts with a [.
    – T. Verron
    Nov 28, 2012 at 19:10

2 Answers 2

22

You are correct that it is not always easy to remember which is which1. There is no real reason behind it - except that an optional argument is by convention and is -- by now become an unwritten rule -- made up of square brackets. So if you know the argument is optional, in 99% of the cases you will be safe to use a square bracket. It is also the first argument, as it makes sense to look ahead to see, if there is an optional argument first.

Sometimes confusion arises with environment type of commands such as:

  \begin{figure}[htbp]

The mnemonic to use here is that the command is really

  \figure[htbp]

which again follows the rule that the [] represent an optional argument.

Patience and texdoc package name are required for some complicated patterns such as the wrapfig package commands and the like, where you end with commands such as:

  \begin{wrapfigure}[6]{r}[..]{12cm}

but even again, here the pattern is [optional]{argument}.

The curly bracket convention, has its roots in early computer languages where they were used to denote begin and end. In Pascal, which was the language that Knuth used to program TeX, curlies denoted the arguments of function definitions and it probably made sense to use the same for TeX.

The original TeX did not have any build-in mechanisms for optional arguments, as we know them today. A macro was and is still defined as:

  \def\name#1#2{#1 #2}

It also makes sense to use curlies for denoting argument calls, as you really telling the parser, begin reading this argument until you encounter the end. One could argue that using normal brackets would be better and a macro like the one above would follow more modern paradigms if written as:

\name(...)(...)

As a matter of fact it is very easy to write TeX macros to accept input in such a way. However, if you consider that we are dealing mostly with textual input, normal brackets are common in such text, which would start imposing on us the need to delineate strings with inverted commas or some other convention.

The LaTeX Team build up more complicated top functions to make life easier and especially since, the key-value concept was introduced a more standardized syntax styling became possible.

But overall I agree with you that overall (all)TeX does not use a lot of computer memory, but it does tax our own.

  1. I have the same problem with markdown's links.
3
  • "The mnemonic to use here is that the command is really \figure[htbp]" Thanks for that; very helpful.
    – Ryan
    Nov 28, 2012 at 22:39
  • Technically \begin{figure}[htbp] opens a group, then expands \figure[htbp]. \end{figure} expands \endfigure then closes the group. Dec 4, 2012 at 21:26
  • Really? Pascal uses () to delimit function arguments, and {} to delimit comments. C uses {} for begin/end though.
    – user202729
    Jul 17, 2022 at 15:50
18

Really a postscript to @Yiannis answer but the rule about the [] arguments being optional isn't unwritten it is explicit in the LaTeX Book that that is the LaTeX syntax.

Using the primitive \def facilities it is possible to define commands with essentially arbitrary syntax but commands defined in all of the core LaTeX distribution and most contributed packages follow these rules.

There are three argument types (for environments and commands)

Mandatory arguments are surrounded by {}. The number of such arguments is always fixed for a particular command. (So the number of mandatory arguments never depends on the value of an optional argument for example.)

Optional arguments are surrounded by [] there may be any number of these. As they are determined positionally it is uncommon to have more than 2 or 3. To avoid the need to have too many of these it is common in newer packages (but never in the base latex distribution) to use a key=value syntax within a single optional argument \includegraphics[width=3cm,clip]{...} in the graphicx version of the command for example.

Picture mode arguments denote coordinates or sizes in in picture mode units and are surrounded by () for example \makebox(10,10){...}

In addition there is the *-form which is documented as being a different command (as in in the case of environments) but for commands is technically a special argument that can be thought of a boolean flag (star or not star). So the tabular and tabular* environments are two separate environments with separate names, whereas \section and \section*, while documented in a similar way are technically the single command \section which takes a special optional * flag (which determines whether the section is numbered in this case).

0

You must log in to answer this question.

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