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 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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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}
share|improve this answer
    
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
  \addvbuffer[0ex 8ex]{top}\\
  \hline
  \addvbuffer[4ex]{center}\\
  \hline
  \addvbuffer[8ex 0ex]{bottom}\\
  \hline
\end{tabular}
\end{document}

enter image description here

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

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

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.