5

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:

enter image description here

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?

user4035
  • 5,035
  • 6
  • 40
  • 57

3 Answers3

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

enter image description here

David Carlisle
  • 757,742
  • So, I'll have to calculate the margins by hand :( – user4035 May 07 '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 07 '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: http://tex.stackexchange.com/questions/7208/how-to-vertically-center-the-text-of-the-cells but it's so complicated. – user4035 May 07 '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 07 '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 07 '13 at 15:56
1

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.

Troy
  • 13,741
mafp
  • 19,096
  • 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 07 '13 at 15:30
1

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.

enter image description here

\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}
David Carlisle
  • 757,742
jub0bs
  • 58,916
  • 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 07 '13 at 15:48
  • @user4035 Yes, you're right. I'll do that right now. Call me Jubobs, by the way :) – jub0bs May 07 '13 at 15:48