Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Not related to your question: If you use the float environment table isntead of figure for your tabular you will get a “Table” caption and the \listoftables and \listoffigures would have the right contents. –  Qrrbrbirlbel Nov 22 '12 at 5:33
add comment

3 Answers

up vote 3 down vote accepted

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

share|improve this answer
add comment

One work around is to load array package by typing \usepackage{array} in the preamble then put >{\raggedright\arraybackslash} before p{1.5cm} in your last column.

Here is the output.

enter image description here

Another option is to define a newcolumntype like the one I did below.

\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\raggedright\arraybackslash}p{1.5cm}}
\begin{document}
\begin{figure}
\centering
  \begin{tabular}{| l | p{1.5cm} | L| }
      \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}
share|improve this answer
add comment

Though this will cause slight alignment issues, I would put your text in a parbox:

\parbox{1.5cm}{\raggedright{}key size increase}

It looks to me like the only reason you're using the column style p{1.5cm} is to create a box for the column titles. I'd approach this differently by writing

\documentclass{article}
\begin{document}
\begin{figure}
  \begin{tabular}{| l | c | c | }
      \hline
     \rule[-0.5ex]{0pt}{0.5ex}    & \parbox[b]{1.5cm}{\raggedright{}percentage difference} & \parbox[b]{1.5cm}{\raggedright 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}

This way, I've created two parboxes aligned on their baselines. The \rule[-0.5ex]{0pt}{0.5ex} creates a vertical strut so that the boxes don't lie too close to the hline below them.

share|improve this answer
    
Welcome to TeX.sx! –  hpesoj626 Nov 22 '12 at 4:21
    
In this instance \rule doesn't change anything due the [b] aligning. I'd recommend \rule[-.3\normalbaselineskip]{0pt}{2\normalbaselineskip} here. –  Qrrbrbirlbel Nov 22 '12 at 5:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.