Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
6  
The reason is that numbers are not allowed in macro names. –  Martin Scharrer Jan 8 '13 at 16:56
    
Welcome to TeX.SE! –  tohecz Jan 8 '13 at 16:56
2  
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
1  
@DiegoSilvera Do not use digits in macro names. Use for example \tabIVone. –  Qrrbrbirlbel Jan 8 '13 at 17:05

1 Answer 1

up vote 3 down vote accepted

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}
share|improve this answer
    
I understood, thank a lot. –  Diego Silvera Jan 8 '13 at 17:08
1  
@DiegoSilvera Now that you understand, avoid \tab41. Better safe than sorry. ;-) –  egreg Jan 8 '13 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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