5

I have troubles wrapping text in my table headers even though I specify the column width with p{1cm}. How can I force a table cell width?

The table main body is generated by stata and ideally the solution would not touch this part.

\documentclass{article}
\usepackage{booktabs, graphicx}

\begin{document}
\begin{table}[h]\caption{This is a test}
\resizebox{!}{!}{\begin{tabular}{l p{1cm} *{8}{l p{1cm}}} \toprule

% Autogenerated Stata code starts    
&\multicolumn{8}{c}{}                                                           \\
&  CEO age &  CFO age &CEO fixed salary/total comp & CEO bonus/ total comp & CEO long term/total comp&CFO fixed salary/total comp&CFO bonus/total comp&CFO long> term/total comp\\
 \midrule
 CEO age         &        1&         &         &         &         &         &         &         \\
CFO age         &    0.259&        1&         &         &         &         &         &         \\
CEO fixed salary/total comp&   0.0241&  -0.0543&        1&         &         &         &         &         \\
CEO bonus/total comp&   0.0279&   0.0818&   -0.439&        1&         &         &         &         \\
CEO long term/total comp&  0.00776&   0.0724&   -0.315&   0.0119&        1&         &         &         \\
CFO fixed salary/total comp&  -0.0775&  -0.0854&    0.672&   -0.306&   -0.280&        1&         &         \\
CFO bonus/total comp&   0.0736&   0.0509&   -0.378&    0.794&  -0.0367&   -0.359&        1&         \\
CFO long term/total comp&   0.0742&   0.0382&   -0.319&  -0.0245&    0.814&   -0.325&  -0.0412&        1\\
% Autogenerated Stata code ends

\bottomrule
\end{tabular}}\end{table}

\end{document}

enter image description here

3
  • it's a hyphenation problem of salary/total and others -- TeX doesn't know hyphenation so can't break it.
    – masu
    Nov 19, 2013 at 11:53
  • But why can it break the 3rd category (CEO fixed salary/total comp) but not the 4th (CEO bonus/ total comp) - there should be enough white-space to break the text
    – Yimmer
    Nov 19, 2013 at 11:56
  • Because you have *{8}{l p{1cm}}, and that column is l. Wasn't that intentional (of course not... to many columns...)? If you change that to *{8}{p{1cm}} it will work. (Hyphenation still needed.)
    – masu
    Nov 19, 2013 at 12:00

2 Answers 2

3
  1. It's a problem of your column definition. Why do you even have l in *{8}{l p{1cm}}? Change it to *{8}{p{1cm}} and your columns will be similar to each other.

  2. It's a hyphenation problem of salary/total and others -- TeX doesn't hyphen text with '/' in it. Use \slash instead to make it break.

3
  • Thanks masu! That works well! Do you also know how I could get the leftmost label column to wrap as well?
    – Yimmer
    Nov 19, 2013 at 12:20
  • 1
    Getting TeX to insert a line break between salary/ and total is not a matter of permitting hyphenation. Instead, the problem is that TeX won't insert a line break if one uses the / (slash) symbol. Use the macro \slash instead of / and TeX will happily insert line breaks.
    – Mico
    Nov 19, 2013 at 12:49
  • @Mico you're right. I thought "total/salary" was a logical group here (didn't really check). You can use \- to hyphen it, but it's not too elegant...
    – masu
    Nov 19, 2013 at 13:25
3

Don't use the / ("slash") symbol if you want to permit a line break right after it. Use the command \slash instead.

I believe there's a separate issue that needs to be fixed: The argument of the tabular environment should probably be

\begin{tabular}{l *{8}{p{1cm}}

rather than

\begin{tabular}{l p{1cm} *{8}{l p{1cm}}

With these changes in place you should get a reasonable looking table. :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .