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 want my table headers to be centered, aligned at the top & without any manual adjustments.

After alot of searching i was able to achieve most of it. As u can see i still have the problem that when we have more than one line, the bottom gap sticks to the text whereas it should be as big as the top gap. Is there a way to fix this programatically? If i do it manually like \\[1cm] i have to be a) eyeballing and b) redo it incase i change the arraystretch. table header

UPDATE: Note that id like to have normal linebreaks in the header and the rest should be adjusted according to the arraystretch. If i use another tabular as suggested and another renewcommand like \newcommand{\ct}[1]{\renewcommand*{\arraystretch}{1}\tabular[t]{@{}c@{}}#1\endtabular} there will also be a misaligned header.

\documentclass{article}
\usepackage{booktabs}

\usepackage{pbox}
\begin{document}
%\newcommand{\ct}[1]{\renewcommand*{\arraystretch}{1}\tabular[t]{@{}c@{}}#1\endtabular}
\newcommand{\ct}[1]{\pbox[t]{3cm}{\relax\ifvmode\centering\fi#1}}

\begin{table} \centering
\renewcommand*{\arraystretch}{6}
{\small
\begin{tabular}{ccc}

\midrule
 {\ct{Data}}   &  { \ct{aa \\aaaaa } }  &  { \ct{A nice heading \\is worth (\%)\\alot}  } \\
 \midrule
 {cc}   &  { aa }  &  { ss } \\
  {cc}   &  { aa }  &  { ss } \\
   {cc}   &  { aa }  &  { ss } \\

 \midrule
\end{tabular}}
\end{table}


\end{document}
share|improve this question
1  
\newcommand{\ct}[1]{\tabular[t]{@{}c@{}}#1\endtabular} –  egreg Dec 2 '12 at 20:45
    
Ah, i already tried that before but i didnt realize that u can always adjust the arraystretch before a \tabular env. Therefore i can use your snippet and place e.g \renewcommand*{\arraystretch}{1}within. For the rest i can leave another arraystretch value active. –  bioslime Dec 2 '12 at 20:48
    
Mh unfortunately, that didnt work out as i thought. I updated my post. –  bioslime Dec 2 '12 at 21:04
    
Why are you enclosing each table cell in braces? It's surely not necessary. –  egreg Dec 2 '12 at 21:07
add comment

1 Answer 1

up vote 3 down vote accepted

You have to reset \arraystretch, but also to add some depth at the end of the multiline entries. Here's a way:

\documentclass{article}
\usepackage{booktabs}

\makeatletter
\newcommand{\ct}[1]{%
  \edef\ct@depth{\the\dp\@arstrutbox}%
  \renewcommand{\arraystretch}{1}%
  \tabular[t]{@{}c@{}}#1\vrule width\z@ depth\ct@depth\relax\endtabular}
\makeatother

\begin{document}
\renewcommand\arraystretch{2}
\begin{tabular}{ccc}
\toprule
Data & \ct{aa \\aaaaa}  & \ct{A nice heading \\is worth (\%)\\alot} \\
\midrule
1 & 2 & 3 \\
4 & 5 & 6 \\
\bottomrule
\end{tabular}
%%% The following one is for comparison
\begin{tabular}{ccc}
\toprule
Data & aa & alot \\
\midrule
1 & 2 & 3 \\
4 & 5 & 6 \\
\bottomrule
\end{tabular}
\end{document}

enter image description here

However my advice is not to set \arraystretch to big values; better not to set it at all.

share|improve this answer
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.