10

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.

10

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}
  • Well, yes, but the thing that threw me(and still does) is that there's an argument in the code that has neither. – william Jun 11 '15 at 7:13
  • @william, sorry. What do you mean by has neither? Which argument are you talking about? – Sigur Jun 11 '15 at 7:23
  • I mean the {#1}#2 part of the code. Shouldn't it be {#1}{#2}? – william Jun 11 '15 at 7:28
  • @william, no. You don't need to use braces to tell what to do with the arguments inside the command. The braces around #1 is necessary because tabular needs it. When you define the alignment for a column you have to use {c} or {l}. But when you write the contents of the tabular you don't need to write it within braces. – Sigur Jun 11 '15 at 7:32
  • Sigur, I think that's got it. On reflection, what threw me was the fact that there was part of a command without braces and, never having seen such an animal, I though something was wrong. Your explanation makes sense. – william Jun 11 '15 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.