[Tex/LaTex] The rhyme and reason of function argument syntax (brackets versus braces)

syntax

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.

Best Answer

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.