# Alternate multicolumn

Is there a nice way to use \multicolumn in alternate rows in a table. i.e.,

\begin{tabular}{| l  r |}
\hline
item 1  & item 2\\
\multicolumn{2}{|c|}{item3}\\

item 1  & item 2\\
\multicolumn{2}{|c|}{item3}\\

item 1  & item 2\\
\multicolumn{2}{|c|}{item3}\\
\hline
\end{tabular}


Expected output:

-
You can define \newcommand{\C}[1]{\multicolumn{2}{|c|}{#1} and use \C{item3}. – egreg Jul 15 '12 at 11:17
or if you want to think of it as a layout for a three item row, define \newcommand\abc[3]{#1&#2\\\multicolumn{2}{|c|}{#3}\\} then \abc{item 1}{\item 2}{item 3} \abc{item 1}{\item 2}{item 3}.. ... – David Carlisle Jul 15 '12 at 11:48
@david-carlisle, your idea is a nice one. Is it possible to do it with 'collcell' by applying 'multicol' to the third column? – anandogc Jul 15 '12 at 11:51

Here is some code that will only work if every line ends with \\.

\documentclass{article}
\usepackage{array}
\begin{document}
\def\newlinemulticol#1\\%
{%
\omit\\%
\multicolumn{3}{c}{#1}\\%
}
\begin{tabular}{cc<{\aftergroup\aftergroup\aftergroup\newlinemulticol}c}
Hello, world & Cheers & Middle \\
abc & def & mid \\\hline
\end{tabular}
\end{document}


But really, you should use \collectcell.

-
Thank you very much. This is exactly what I needed. – anandogc Jul 15 '12 at 14:10
Can you please explain why are so many \afrergroup required. – anandogc Jul 15 '12 at 14:17
@anandogc The c column in a tabular roughly means \hskip\tabcolsep{\hfil\ignorespaces#\unskip\hfil}\hskip\tabcolsep, where # stands for the cell contents, \hskip\tabcolsep, \hfil, \ignorespaces and \unskip have to do with spacing. The group, {...} is there to match the array environment, where the math mode $...$ creates a group. <{...} puts its material after #, hence my three \aftergroup go inside the group. The first puts the second after }. The third puts the \newlinemulticol after }. The remaining \aftergroup moves the macro after & where it belongs. – Bruno Le Floch Jul 15 '12 at 18:57