3

I know I can use \multicolumn{}{}{} to center a single cell within a table with non-centered cells, but what is the best way to center all the cells in a given row?

\documentclass{article}
\begin{document}
    \begin{tabular}{*6{l}}
    Name1 & Name2 & Name3 & Name4 & Name5 & Name6\\
    Type1 & Type2 & Type3 & Type4 & Type5 & Type6\\ % <- center this row
    Item1 & Item2 & Item3 & Item4 & Item5 & Item6\\
    Item7 & Item8 & Item9 & Item10 & Item11 & Item12\\
    \end{tabular}
\end{document}
Moriambar
  • 11,466
Sverre
  • 20,729

2 Answers2

5

One way would be to define a macro that accepts 6 parameters and applies a \multicolumn to each parameter:

enter image description here

Code:

\documentclass{article}

\newcommand{\Mc}[1]{\multicolumn{1}{c}{#1}}% \newcommand{\CenterRow}[6]{% \Mc{#1}&\Mc{#2}&\Mc{#3}&\Mc{#4}&\Mc{#5}&\Mc{#6}% }%

\begin{document} \begin{tabular}{*6{l}} Name1 & Name2 & Name3 & Name4 & Name5 & Name6\ \CenterRow{Type1}{Type2}{Type3}{Type4}{Type5}{Type6}\ % <- center this row Item1 & Item2 & Item3 & Item4 & Item5 & Item6\ Item7 & Item8 & Item9 & Item10 & Item11 & Item12\ \end{tabular} \end{document}


Alternatively, if you want to keep the same syntax you could define a macro that accepts parameters delimited by &.

Note:

  • This yields identical results as above, however I am not sure what other issues this might cause.

Code:

\documentclass{article}

\newcommand*{\Mc}[1]{\multicolumn{1}{c}{#1}}%

\newcommand*{\CenterRow}[6]{}% Make sure we not overwriting an existing macro \def\CenterRow #1 & #2 & #3 & #4 & #5 & #6\{% \Mc{#1}&\Mc{#2}&\Mc{#3}&\Mc{#4}&\Mc{#5}&\Mc{#6} \% }%

\begin{document} \begin{tabular}{*6{l}} Name1 & Name2 & Name3 & Name4 & Name5 & Name6\ \CenterRow Type1 & Type2 & Type3 & Type4 & Type5 & Type6 \ % <- center this row Item1 & Item2 & Item3 & Item4 & Item5 & Item6\ Item7 & Item8 & Item9 & Item10 & Item11 & Item12\ \end{tabular} \end{document}

Peter Grill
  • 223,288
  • The second option is very risky, since you have to match the parameter text exactly. For example, dropping a space, or inserting a space between #6 and \\ would yield problems. – Werner Feb 21 '13 at 19:46
  • @Werner: Yep, that is why I added the "Note" -- This is the first time I have defined a non-standard delimited macro so was surprised that it worked, at least for this one case. – Peter Grill Feb 21 '13 at 19:48
2

For such tables, ConTeXt provides a much nicer interface. For example using natural tables, you can either use

\startsetups table:setup
  \setupTABLE[align=flushleft, frame=off]
  \setupTABLE[row][2][align=middle]
\stopsetups

\starttext
  \startTABLE[setups={table:setup}]
    \NC LongName1 \NC LongName2 \NC LongName3 \NC LongName4 \NC LongName5 \NC LongName6\NC\NR
    \NC Type1 \NC Type2 \NC Type3 \NC Type4 \NC Type5 \NC Type6\NC\NR % <- center this row
    \NC Item1 \NC Item2 \NC Item3 \NC Item4 \NC Item5 \NC Item6\NC\NR
    \NC Item7 \NC Item8 \NC Item9 \NC Item10 \NC Item11 \NC Item12\NC\NR
  \stopTABLE
\stoptext

or if you are not sure where row you want to center-align upfront, use

\starttext
\startTEXpage[offset=3mm]
  \bTABLE[align=flushleft, frame=off]
    \bTR 
       \bTD LongName1 \eTD \bTD LongName2 \eTD \bTD LongName3 \eTD \bTD LongName4 \eTD \bTD LongName5 \eTD \bTD LongName6\eTD
    \eTR
    \bTR[align=middle] 
       \bTD Type1 \eTD \bTD Type2 \eTD \bTD Type3 \eTD \bTD Type4 \eTD \bTD Type5 \eTD \bTD Type6\eTD
    \eTR 
    \bTR 
       \bTD Item1 \eTD \bTD Item2 \eTD \bTD Item3 \eTD \bTD Item4 \eTD \bTD Item5 \eTD \bTD Item6\eTD
     \eTR
    \bTR 
       \bTD Item7 \eTD \bTD Item8 \eTD \bTD Item9 \eTD \bTD Item10 \eTD \bTD Item11 \eTD \bTD Item12\eTD
    \eTR
  \eTABLE
\stopTEXpage
\stoptext

both of these give

enter image description here

Aditya
  • 62,301