I'm using a custom command to format all tables in a document. Some tables share properties other than style, such as column definitions and headers, which is why this answer inspired me to even more reduce repeated code.
Inserting header data into the table command did not cause any problems, while for inserting column definitions I had to find out how to expand the column definitions before insertion. \expandafter solved that problem.
Now, I'd like to combine parameters given as commands and others given as literals, e.g. the table's data. If the to-be-expanded command that contains the column definitions happens to be the first argument, this works fine. What do I need to do if conversely a literal is the first argument? How do I make \expandafter bypass a pair of curly braces instead of a command?
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\newcommand{\colDef}{{cc}}
\newcommand{\tableBody}{a & b\\}
\newcommand{\myTabA}[2]{%
\begin{tabular}{#1}
1 & 2\\\midrule
#2
\end{tabular}
}
%same command with #1 and #2 switched
\newcommand{\myTabB}[2]{%
\begin{tabular}{#2}
1 & 2\\\midrule
#1
\end{tabular}
}
%draw table A
\expandafter\myTabA\colDef{\tableBody}
\expandafter\myTabA\colDef{c & d\\}
%draw table B
\expandafter\myTabB\expandafter\tableBody\colDef
%produces error
%\expandafter\myTabB\expandafter{c & d\\}\colDef
\end{document}
etextools's documentation did not look like a geocities website! All that is missing is a<blink>tag. – Aditya May 22 '12 at 17:52