# Tables as commands

I have to do a little work but I have to use frequently the same settings for a table and I put the following code

\newcommand{\tab41}[4]{\begin{center}
\begin{tabular}{|c|l||c|l||c|l||c|l|}
\hline
(A) & #1 & (B) & #2 (C) #3 & (D) & #4\\
\hline
\end{tabular}
\end{center}}


and it fails. What am I doing wrong?

-
The reason is that numbers are not allowed in macro names. –  Martin Scharrer Jan 8 '13 at 16:56
–  tohecz Jan 8 '13 at 16:56
Please always include the error message(s) you get. Here I get ! You can't use macro parameter character #' in restricted horizontal mode., you too, right? –  Martin Scharrer Jan 8 '13 at 16:58
Is there a way to solve it? Sorry for my mistakes, I'm new here. –  Diego Silvera Jan 8 '13 at 16:59
@DiegoSilvera Do not use digits in macro names. Use for example \tabIVone. –  Qrrbrbirlbel Jan 8 '13 at 17:05

The reason this doesn't work is because macro names may not include numbers and other non-letters (_, ^ and usually not @ except where this gets changed, like inside packages and classes).

You should simply avoid having numbers in macro names. Often people use Roman numerals instead, e.g. \mymacroiv instead of \mymacro4. There are however some ways to overcome this, see:

You could use the TeX (not LaTeX) way to define macros to have 41 as part of the parameter text, i.e the stuff the macro awaits after its name. This way you have a \tab macro which awaits and removes a 41 direct after its name. This works only if you don't have any other \tab<number> macros, however.

\documentclass{article}

\begin{document}

\newcommand\tab{}% to get an error if \tab is already defined
\def\tab41#1#2#3#4{\begin{center}
\begin{tabular}{|c|l||c|l||c|l||c|l|}
\hline
(A) & #1 & (B) & #2 (C) #3 & (D) & #4\\
\hline
\end{tabular}
\end{center}}

\tab41{a}{b}{c}{d}

\end{document}


--

If you really want multiple \tab<number> macro then you could define a \tab macro which reads the numbers as two arguments (not { } required) and calls the correct macro which got defined using \@namedef, where the macro name is provided as text and therefore can include all printable characters. Because \@namedef is an internal LaTeX macro you need to use \makeatletter and \makeatother around its usage.

\documentclass{article}

\makeatletter
\newcommand\tab[2]{\@nameuse{tab#1#2}}

\@namedef{tab41}#1#2#3#4{\begin{center}
\begin{tabular}{|c|l||c|l||c|l||c|l|}
\hline
(A) & #1 & (B) & #2 (C) #3 & (D) & #4\\
\hline
\end{tabular}
\end{center}}

\@namedef{tab25}#1#2#3#4{\begin{center}
\begin{tabular}{|c|l||c|l||c|l||c|l|}
\hline
(Other) & #1 & (table) & #2 (C) #3 & (D) & #4\\
\hline
\end{tabular}
\end{center}}

\makeatother

\begin{document}

\tab41{a}{b}{c}{d}

\tab25{a}{b}{c}{d}

\end{document}

-
I understood, thank a lot. –  Diego Silvera Jan 8 '13 at 17:08
@DiegoSilvera Now that you understand, avoid \tab41. Better safe than sorry. ;-)` –  egreg Jan 8 '13 at 17:09