# Arguments to Commands: […] vs {…}

Perhaps this is something that has been answered before, but a cursory search didn't turn anything up.

Is there any logic to how commands take arguments? Some things want the arguments to the command to be enclosed in square brackets, for example \begin{figure}[h!]... but others want the argument in curly brackets a la \begin{multicolor}{2}.... I constantly get this wrong and have to go back and look them up, or refer back to an old document. Is there any way develop sense of what typically goes in which type of argument? Or do I just have to know (or more realistically, put a whole ton more snippets into sublime?)

-
[] are optional, and {} are mandatory :) –  cmhughes May 3 '13 at 17:28
This might clear up some thoughts tex.stackexchange.com/questions/29973/… –  percusse May 3 '13 at 17:57

In order to answer your question we can wear different kinds of glasses.

Standard glasses

The standard behaviour of LaTeX is very simple:

• [] is used for optional arguments,
• {} is used for mandatory arguments.

The name "optional argument" means that you don't need to use it. The command will work even without it.

A simple example is the command \section which has one mandatory and one optional argument. That means \section{title} works and \section[title in toc]{title} works too.

However there are several commands that use also () for their mandatory arguments. Normally the round brackets are used for coordinates. The command \put needs a coordinate pair which must be passed with (). Graphical packages like TikZ or PSTricks do the same.

However every author is free in the choice of brackets.

xparse glasses

The (relatively) new frontend package of expl3 allows every user to set up the braces for mandatory and optional arguments in a very simple way. This can lead to various commands with no clear syntax. Normally all authors are working with the method explained above.

-

The usage of these delimiters may vary*, but predominantly they are used to designate optional [..] and mandatory {..} arguments. Optional arguments imply that you may omit them without loss of functionality, while mandatory implies that they are required in order to successfully compile your project.

Formerly, the sole use of arguments for different macro parameters meant that users could create, for example,

\newcommand{\mymacro}[9]{%
% Do something with #1, ..., #9
}


without the end user specifically knowing what the meaning of argument #5 is (say). As such, you would constantly have to go back to the documentation and read what goes where what type of argument it is (optional or mandatory). This still happens today. As an example, consider the wrapfig package's wrapfigure environment. From the wrapfig documentation you have

While I love the package, I always have to go back to the documentation to figure out what goes where and why. Other use cases also exist, and if you forget, you have to go back and figure it out. That's just the reality.

If you're uncertain about the type of argument, you could use a quick \show\mymacro within your document and see the construction of macro. This only works in certain cases, of course, since definitions using \newcommand with an optional argument doesn't provide much value when using \show. Here are some examples:

\documentclass{article}
\newcommand{\mymacroA}[2][0]{Do stuff}
\def\mymacroB#1{Do stuff}
\makeatletter
\def\mymacroC{\@ifnextchar[\@mymacroC{\@mymacroC[0]}}
\def\@mymacroC[#1]#2{Do stuff}
\makeatother
\begin{document}
\ttfamily
\meaning\mymacroA \par
\meaning\mymacroB \par
\meaning\mymacroC
\end{document}


In the above example, \mymacroA and \mymacroC provide the optional argument/interface, and therefore can by used as \mymacro[<optarg>]{<manarg>} or just \mymacro{<manarg>}. It's not that clear from the meaning of the macros that they require it, but they do.

As a matter of instilling good practice, using key-values is always a clean way of passing arguments that could take varied inputs. Using the wrapfig example above, an interface like the following would, perhaps, be more intuitive:

\begin{wrapfigure}[numlines=12,placement=r,overhang=34pt,width=5cm]
%<figure>
\end{wrapfigure}


where the keys numlines, placement, overhang and width are have some predefined default.

*The xparse package provides the means to adjust the type of delimiter quite easily. Moreover, using basic TeX commands, you can also create your own parameter text combinations that could be considered optional or mandatory, using a combination of \@ifnextchar commands, as in the above MWE.

-
If only wrapfigure was as sensible as your last code block. Thanks for the great answer! –  Drew Christianson May 3 '13 at 18:47

Braces {} are used for introducing arguments inside. To use your example:

\begin{figure}

\end{figure}


Whereas [] are used for passing options:

\begin{figure}[t] %command: begin.  argument: figure.  option: top
\includegraphics[width=100mm]{filename} %command: includegraphics. argument: filename.  option: width=100mm.
\end{figure}

-