[Tex/LaTex] \newcommand and its intricacies

conditionalsmacros

I recently came across the following command:

\newcommand{\minitab}[2][c]{\begin{tabular}{#1}#2\end{tabular}}

Well, this is close to the syntax laid out on p.845 of TLC, but I still don't understand it. My interpretation is this:

A newcommand, minitab, is being defined. The command has an optional argument of 2 (the first
pair of square brackets) and a default argument of c (the second pair of square brackets).

Whilst the optional argument is 2, it could be anything from 0 to 9.

The default argument, c, only becomes operative when the optional argument has been (a) specified, or (b) does not exist.

That default argument, c, does not of itself mean center. It could, for example, be used as a power: a$^{c}$.

So then,

  1. Have I got this right thus far? If so,

  2. How does the tabular command tell (a) from (b)?

  3. What's the significance of the non-curly bracketed argument within tabular?

An example would be nice.

Best Answer

Attention: there is an error in your code. You missed closing brace } before [2].

The code

\newcommand{\minitab}[2][c]{\begin{tabular}{#1}#2\end{tabular}}

defines the command \minitab which has two arguments.

The 1st is optional and has default value c (which will be used if you don't use another value). It is used to determine the alignment of the single column of tabular. So it could be also right or left.

The second argument is obligatory and it is in fact the content for the tabular.

So you can use the command as

\minitab{foo and foo}

or

\minitab[l]{foo and foo}

Recall that options for a command are passed in brackets and mandatory arguments in braces. \command[<optional>]{<mandatory>}.

Edit:

command name: \minitab

number of arguments: [2] (so the #1 is optional; the #2 is mandatory)

default value for #1: [c] (will be overridden if you pass another valid value)

what to do with the arguments? use #2 inside the tabular as its contents; use #1 for the column alignment in tabular.

(!) so you have to use {#1} in the definition. Also, for example, if you want to insert a right border you should use {#1|}. The braces are important to group.

Recall that the right way to use a tabular is:

\begin{tabular}{cols}
content...
\end{tabular}

In resume: \minitab[l]{a lot of text} will be the same as

\begin{tabular}{l}
a lot of text
\end{tabular}