[Tex/LaTex] aligning text in a table

tables

I have problem with aligning text in a table. Here is my example:

\documentclass{article}
\begin{document}
\begin{figure}
  \begin{tabular}{| l | p{1.5cm} | p{1.5cm}| }
      \hline
         & percentage difference &  key size increase \\ \hline
      N = 90 trials &  2.1 \% &  76 \% \\ \hline
      N = 180 trials & 1.3 \% &  40 \% \\ \hline
  \end{tabular}
  \caption{Caption here.}
\end{figure}                     
\end{document}

enter image description here

the problem is a cell with text 'key size increase', there is big space between
words 'key' and 'size', how can I fix it.

Best Answer

A \parbox justifies its content. That’s the reason why there's so much space between “key” und “size”.

I find the table column specifier p{<width>} in this case very unsatisfying as the resulting column has a minimum width.

I'd rather insert manually line-breaks with one of the following solutions:

  • \lbCell: An inner tabular that exists of only one column (I prefer a centered heading),
  • \pCell: a \pbox[<vertical alignment>]{<maximum width>}{<content>}.
    The \pbox macro is provided by the pbox package. The resulting box collapses to the minimal required width.

The first optional argument of both \*Cell macros denote the vertical alignment of the insertes tabular/pbox and is per default top.

Not related to you question, I also uses the booktabs package to provide a more appealing output (so many lines!) of your tabular.

Code

\documentclass{article}
\usepackage{booktabs}
\newcommand*{\lbCell}[2][t]{%
    \begin{tabular}[#1]{@{}c@{}}%
    #2%
    \end{tabular}%
}
\usepackage{pbox}
\newcommand*{\pCell}[2][t]{% needs manual line-breaks!
    \pbox[#1]{\linewidth}{#2\strut}%
}
\begin{document}
\begin{tabular}{lcc}
    \toprule
                                 & \lbCell{percentage\\difference} & \lbCell{key size\\increase} \\ \midrule
    $N = \hphantom{1}90$ trials  &             2.1 \%              &            76 \%            \\
    $N = 180$ trials             &             1.3 \%              &            40 \%            \\ \bottomrule
\end{tabular}

\begin{tabular}{lcc}
    \toprule
                                 & \pCell{percentage\\difference} & \pCell{key size\\increase} \\ \midrule
    $N = \hphantom{1}90$ trials  &             2.1 \%             &           76 \%            \\
    $N = 180$ trials             &             1.3 \%             &           40 \%            \\ \bottomrule
\end{tabular}
\end{document}

Output

enter image description here