2

Quite often I have table headers which need to be wrapped across several lines and I have not find out how to typeset those nicely.

The best I have found is using a \pbox like in the example below to get right-aligned wrapping text.

But how can I get all table header cells to align at the bottom of that row?

\documentclass{article} 
\usepackage{booktabs,pbox} 
\begin{document}
\begin{table}
\centering
   \begin{tabular}{l*{3}{r}}
    \toprule
    1 & \pbox{1.5cm}{\raggedleft first column contents} &  \pbox{1.5cm}{\raggedleft second column contents even longer} & third\\
    \midrule
    2 & 10 & 15 & 10\\
    \bottomrule
   \end{tabular}
\end{table}
\end{document}
0

2 Answers 2

0

It's easy to do that with makecell: its \thead and \makecell commands allow for line breaks inside cells and common formatting of cells. So it's up to you to decide where you have linebreaks:

\documentclass{article}

\usepackage{booktabs,pbox}
\usepackage{makecell}
\renewcommand\theadalign{rb}

\begin{document}

\begin{table}
\centering
\begin{tabular}{l*{3}{r}}
\toprule
1 & \thead{first column\\ contents} & \thead{second column \\ contents\\ even longer} & \thead{third} \\
\midrule
2 & 10 & 15 & 10\\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here

1
  • I didn't see this package before. Although I thought having a maximum width is nice and simple, I guess you are right: it might be actually better to explicitly decide on line breaks.
    – Bob
    Oct 26, 2014 at 16:22
1

I don't think \pbox is really helping much, I just use \parbox here using b to align on its bottom row.

\documentclass{article} 
\usepackage{booktabs,array} 
\begin{document}
\begin{table}
\centering
   \begin{tabular}{l*{3}{r}}
    \toprule
    1 & 
\parbox[b]{1.5cm}{\raggedleft first column contents} & 
 \parbox[b]{1.5cm}{\raggedleft second column contents even longer} &
 third\\
    \midrule
    2 & 10 & 15 & 10\\
    \bottomrule
   \end{tabular}
\end{table}
\end{document}

enter image description here

3
  • That does a right-bottom align, great! Unfortunately, I forgot to specify that I also would like to retain the feature that the columns sizes are adapted to the content like with \pbox if any possible. I should have been more precise.
    – Bob
    Oct 25, 2014 at 17:18
  • \pbox has a b option so you could change \parbox back to \pbox if you want (varwidth is probably a more robust package for that sort of thing). or you could just use a tabulary R column Oct 25, 2014 at 17:25
  • Somehow, the b option of the \pbox command does not work the same way as it does with the \parbox command. I did not see the varwidth package before which also looks helpful for this.
    – Bob
    Oct 26, 2014 at 16:20

You must log in to answer this question.

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