# Vertical alignment in tabular cells with fixed height

I have the code:

\documentclass{article}
\usepackage[a5paper]{geometry}
\begin{document}
\begin{tabular}{ | c | }
\hline
top\\[8ex]
\hline
center\\[8ex]
\hline
bottom\\[8ex]
\hline
\end{tabular}
\end{document}


Gives this:

The top column is already aligned top. How can I vertically align the center column at the center? And the bottom column at the bottom?

-

A somewhat more automated solution, which doesn't require the user to specify the width of the \parbox manually. The new command \parboxc accepts three arguments:

1. the vertical alignment specification: t, c, or b;
2. the height of the \parbox;
3. the contents of the \parbox.

\documentclass{article}
\usepackage[a5paper]{geometry}

\newlength\mytemplength

\newcommand\parboxc[3]{%
\settowidth{\mytemplength}{#3}%
\parbox[#1][#2]{\mytemplength}{\centering #3}%
}

\begin{document}
\begin{tabular}{ | c | }
\hline
\parboxc{t}{8ex}{top}\\
\hline
\parboxc{c}{8ex}{center}\\
\hline
\parboxc{b}{8ex}{bottom}\\
\hline
\end{tabular}
\end{document}

-
Sir, I see, that your macroes differ only by 1 letter: t, c, b. Does it seem reasonable, to set it as the 3-rd parameter and make 1 macro? This will make the code simpler. –  user4035 May 7 '13 at 15:48
@user4035 Yes, you're right. I'll do that right now. Call me Jubobs, by the way :) –  Jubobs May 7 '13 at 15:48
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{verbatimbox}
\begin{document}
\begin{tabular}{ | c | }
\hline
\hline
\hline
\hline
\end{tabular}
\end{document}


-
So, I'll have to calculate the margins by hand :( –  user4035 May 7 '13 at 15:32
@user4035 What margins? Vertical? In your example, you specified the vertical margins. What is it you intended? I realize the purpose of an MWE is to make the problem small enough to be easily comprehended by the reader. But is there another constraint to your problem, other than top/middle/bottom alignment? –  Steven B. Segletes May 7 '13 at 15:35
I mean, that for the center column I'll have to divide the height by two. If I change the height, I'll have to redivide. Also, shouldn't I broaden my question by adding another example without the hard coded row height? I looked here: tex.stackexchange.com/questions/7208/… but it's so complicated. –  user4035 May 7 '13 at 15:40
@user4035 It would be vital to know, for example: if you specify the overall table height, or if it is allowed to grow with contents; Whether you will always be a 1-column vertical table or not; whether there is always one row which is the largest in vertical extent? Obviously, one might choose not to constrain anything, but that could end up making the successful algorithm more complex than necessary, if there are known constraints to the data. (In essence, if you remove the 8ex constraint, then you haven't constrained the problem enough) –  Steven B. Segletes May 7 '13 at 15:45
Yes, for the variable height table it would be more complex. I'd better make a new question for that case. –  user4035 May 7 '13 at 15:56

You could use simple parboxen in the table cells:

\documentclass{article}
\usepackage[a5paper]{geometry}
\begin{document}
\begin{tabular}{ | c | }
\hline
\parbox[t][8ex]{8ex}{\centering top}\\
\hline
\parbox[c][8ex]{8ex}{\centering center}\\
\hline
\parbox[b][8ex]{8ex}{\centering bottom}\\
\hline
\end{tabular}
\end{document}


Unfortunately you have to specify the width yourself, the second 8ex in those calls.

-
parbox is bad, because of the hard-coded width. If I change the text I want it to stretch the cells. But with the parbox this won't happen - the width is set in \parbox. –  user4035 May 7 '13 at 15:30