One of my last posts was on how to define the width of a column in a table (see here).
The parbox (p), which is used by Latex when you define the column width, will by default align its content on the left. This can be changed, but is a bit tricky.
The keyword is:
\raggedleft, \centering or \raggedright
Raggedleft will align text on the right-hand side, leaving whatever white space remains (ragged) on the left.
For a better understanding, let’s define right alignment of the text as a new column type, which can than be used in the very same way as pre-defined column types.
Defining a new column type needs the following package:
\usepackage{array}
\newcolumntype{x}[1]{%
>{\raggedleft\hspace{0pt}}p{#1}}%
The argument is the width of the column.
Now simply use “x{2cm}” instead of “p{2cm}”, for columns which align text on the right. By changing \raggedleft to \centering, you can align text in the centre.
Example:
\begin{table}\centering
\begin{tabular}{|l|x{4.5cm}|x{4.5cm}|}\hline
Nb. & Advantage & Disadvantage\tabularnewline\hline
1 & a & b \tabularnewline\hline
2 & b & a \tabularnewline\hline
\end{tabular}
\end{table}
Note:
An important last thing to mention, you cannot end lines with “\\”, as you defined your own column type. Therefore, I am using \tabularnewline in the example. If you want to save time, you might define your own command as follows:
\newcommand{\tn}{\tabularnewline}
Or even:
\newcommand{\tnhl}{\tabularnewline\hline}
July 18th, 2008 at 11:28 am
\newcolumntype{x}[1]{% >{\raggedleft\hspace{0pt}}p{#1}%leaves me with error message:
Paragraph ended before (backslash)newcol@ was complete
Any ideas?
July 28th, 2008 at 9:55 pm
Hi Dirk,
Thanks for your comment, there was a parenthesis missing, I corrected it.
Cheers,
Tom
October 13th, 2008 at 10:23 pm
Hi Tom,
\\ would work like \tabularnewline again if you insert the command \arraybackslash, for instance:
\newcolumntype{x}[1]{% >{\raggedleft\arraybackslash}p{#1}}%It’s defined in array.sty:
\def\arraybackslash{\let\\\tabularnewline}Stefan
September 2nd, 2010 at 6:59 pm
Thanks Tom and Stephan. It worked for me
October 29th, 2008 at 12:30 am
I have a problem with tables in LaTeX. I want the text in the first row to be centered and the text in the second row to be flushed left. Hope you can help me. Thanks in advance.
October 30th, 2008 at 9:03 pm
Hi,
You can control the alignment within a cell using “\multicolumn”. Let me give you a simple example:
\begin{table}[ht] \centering \begin{tabular}{lrr} & \multicolumn{1}{c}{Exp. 1} & \multicolumn{1}{c}{Exp. 2}\\ Setting A& 5.43498289 & 4.309872395\\ Setting B& 5.7098429109890 & 4.10983901\\ \end{tabular} \end{table}%If you need separation lines, you have to use \vline and \hline between cells.
Tom.
March 19th, 2009 at 10:10 am
Thanks a lot for this post. It helped me out!
Hans
March 25th, 2009 at 1:57 pm
You are a hero!!!!!!!
June 19th, 2009 at 12:17 am
Thanks for this post; I’ve been trying to figure this out for a while and you’re the only person who has had an answer so far.
February 8th, 2010 at 10:46 am
Thanks a lot. Your post was exactly what I needed.
May 14th, 2010 at 8:16 am
Thanks a lot! It really helped me. But can you explain what should I do, if I want to center my text not only horizontally but vertically?
May 14th, 2010 at 5:25 pm
Thanks for this question. That’s an easy one
. Use ‘m’ instead of ‘p’ to vertically align your text within a cell. Hence your column type definition would look like this:
\newcolumntype{x}[1]{% >{\raggedleft\hspace{0pt}}m{#1}}%Cheers,
Tom.
May 14th, 2010 at 5:34 pm
It works – thanks a lot again!
March 26th, 2011 at 3:11 pm
This was a great help, thank you.