120

Possible Duplicate:
Aligning inside tabular environment, specific cell

I have a simple table with a given align. I want to know if there is a way to specify a different align for a cell. For example, in the following table, how can I make Item 3 align right?

\begin{table}
    \begin{tabular}{|l|r|}
        \hline
        Header1 & HeaderX \\ \hline
        Item1   & X1      \\ 
        Item2   & X2      \\ 
        Item3   & X3      \\
        \hline
    \end{tabular}
\end{table}
  • Side note: if you use an extended tabular environment with paragraph-style columns (e.g. \begin{tabularx}{15cm}{|p{3cm}|X|X|m{3cm}|}), then you can use this solution. Unfortunately, this does not seem to work with simple l, c or r columns. – Josse Apr 18 '19 at 14:29

1 Answers1

176

Use \multicolumn -- it also works for single cells. Note that you have to specify any vertical rules also in the second argument of \multicolumn.

\documentclass{article}

\begin{document}

\begin{table}
    \begin{tabular}{|l|r|}
        \hline
        Header1 & HeaderX \\ \hline
        Item1   & X1      \\ 
        Item2   & X2      \\ 
        \multicolumn{1}{|r|}{Item3}   & X3      \\
        \hline
    \end{tabular}
\end{table}

\end{document}

enter image description here

David Carlisle
  • 757,742
lockstep
  • 250,273
  • 6
    You can use 'c' to justify centre. For example, \begin{tabular}{|l|c|} – timothyjgraham Jun 28 '16 at 02:16
  • 3
    The multicolumn method messes up the column width if it occurs in the first row. – A Fog Feb 25 '21 at 10:46
  • Similar problem to @AFog, If it is the first row and not the rightmost column it is fine, but if it is the rightmost column - it would mess it up. – Long May 24 '21 at 12:48