# Tabular* Alignment of Columns

I've read about the tabular* environment, and the same example pops up over and over again. What I'd like to know is when the example is converted, why do columns 2 and 3 lose their centering? I've seen solutions using tabularx and array, but the solution in the example seems close to being correct, so I'd like to know the extra step to get it as expected.

The link to the example and its output is: WikiBooks LaTeX Tables. It's the second half of this section.

If anyone also has tips for how to search for tabular* in a search engine without the * being taken as a wildcard, I'd appreciate that, too.

-
–  Peter Grill Oct 28 '12 at 5:02
It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. Just copy the code that you are referring to and make it a compilable example. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving problem rather than following external links and hoping that we have chosen the correct one that you are referring to. –  Peter Grill Oct 28 '12 at 5:05

I believe you are talking about the following table:

\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill}} | c | c | c | r | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1  & item 2  & item 3  & item 4  \\
\hline
\end{tabular*}


The @{\extracolsep{\fill}} tells the compiler to add extra rubber length (\fill) to the left of every subsequent column (starting from column two) so that the final table is of correct width. This spacing gets added last column too, but you do not see the difference because it is already right aligned. If you use cellcolor command from colortbl package, you will notice the extra space added is not counted as contents of the cell but as column separator (as in the example below.)

\documentclass{article}
\usepackage{colortbl}
\begin{document}
\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill}} | c | c | c | r | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1  &\cellcolor{green} item 2  & item 3  & \cellcolor{green} item 4  \\
\hline
\end{tabular*}
\end{document}


One way to fix it would be to use the p-column specifier, which allows for fixed width columns.

\begin{tabular*}{0.75\textwidth}{| c *3{| p{0.175\textwidth}} | }
\hline
label 1 & \centering label 2 & \centering label 3 & \raggedleft label 4 \tabularnewline
\hline
item 1  & item 2  & item 3  & item 4  \tabularnewline
\hline
\end{tabular*}


But there are two problems with this:

1. You need to get the width of the column right by trial and error process (as task best left to the computer).
2. Since the p-columns are by default left aligned. So you need to specify the alignment of each cell separately (as I have done in the example above). This mixes up table contents with lot of formatting commands, something I prefer to keep separate.

A easier way to get the right table would to use the tabularx or array package as suggested in the wikibooks article

Here we can use the X-column definition to automagically adjust the column widths. Again since X-columns are set as p-columns, they are left aligned. We need to specify the correct alignment. Here we can use the '\newcolumntype' command to define our custom column definitions.

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{tabularx}{0.75\textwidth}{ | c | C | C | R | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1  & item 2  & item 3  & item 4  \\
\hline
\end{tabularx}
\end{document}


You can also do some neat tricks with the X-columns like specify relative widths of two column. I would suggest having a look at the manual for more details.

-