1. Dirk

\newcolumntype{x}[1]{%
&gt;{\raggedleft\hspace{0pt}}p{#1}%

leaves me with error message:

Paragraph ended before (backslash)newcol@ was complete

Any ideas?

2. Hi Dirk,

Thanks for your comment, there was a parenthesis missing, I corrected it.

Cheers,
Tom

3. stefan08

Hi Tom,

\\ would work like \tabularnewline again if you insert the command \arraybackslash, for instance:

\newcolumntype{x}[1]{%
&gt;{\raggedleft\arraybackslash}p{#1}}%

It’s defined in array.sty: \def\arraybackslash{\let\\\tabularnewline}

Stefan

• Christiaan

Thanks Tom and Stephan. It worked for me

4. nguyenminhhaivn

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.

5. 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}
&amp; \multicolumn{1}{c}{Exp. 1}
&amp; \multicolumn{1}{c}{Exp. 2}\\
Setting A&amp; 5.43498289 &amp; 4.309872395\\
Setting B&amp; 5.7098429109890 &amp; 4.10983901\\
\end{tabular}
\end{table}%

If you need separation lines, you have to use \vline and \hline between cells.

Tom.

6. Hans

Thanks a lot for this post. It helped me out!

Hans

7. James

You are a hero!!!!!!!

8. Norm

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.

9. Thanks a lot. Your post was exactly what I needed. 🙂

10. Al

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?

• 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]{%
&gt;{\raggedleft\hspace{0pt}}m{#1}}%

Cheers,
Tom.

• Al

It works – thanks a lot again!

11. Alp

This was a great help, thank you.

12. Thanks for all the tips here!

13. Arthur

Hey Tom! It’s been some years, but your solution with \newcolumntype works nicely! However, I want to use such approach with the “booktabs” package. But if I use “x{0.2cm}” on the –last– column, it gives me the error “Misplaced \noalign. (\midrule …). That “midrule” is a horizontal line from the booktabs package.

If I use “…x{0.20cm}x{0.20cm}p{0.20cm}” (i.e. the last column has the regular “p” instead of “x”) it works flawlessly.

Any idea how I can get a working “booktabs” table with defined column-widths and right-aligned?

• tom

Hi Arthur,

Thanks for your comment. I think the problem is not with booktabs. When you use the fixed-width columns as defined in the array package and set the alignment, you’ll have to reset the newline macro \\ in the last column. This can be done using \arraybackslash.

The updated new column definition below should fix the problem:

\newcolumntype{x}[1]{%
>{\raggedleft\arraybackslash\hspace{0pt}}p{#1}}%

Best, Tom

• Arthur

Thanks Tom, and sorry for not paying attention to that part! 🙂

14. Line

Thank you so much for this helpful post! It was just what I needed!