3

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?

David Carlisle
  • 757,742

1 Answers1

3

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}
David Carlisle
  • 757,742
Martin Scharrer
  • 262,582