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.

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:

share|improve this question
3  
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

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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

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.