42

I have the code:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}
\begin{tabularx}{\textwidth}{ | X | c | }
  \hline
  \lipsum[1] & top\\
  \hline
  \lipsum[1] & center\\
  \hline
  \lipsum[1] & bottom\\
  \hline
\end{tabularx}
\end{document}

Gives this:

enter image description here

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

1

4 Answers 4

34

The vertical adjustment of the row "c" is related to the definition of the columntype X which uses the specifier p.

You need m for a centered adjustment and b for bottom. This can be achieved by \multicolumn, whereby the line width must be saved (I don't know a good solution).

Here is an example:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}

\begin{tabularx}{\textwidth}{ | X | c | }
  \hline
  \lipsum*[1]\xdef\tempwidth{\the\linewidth} & top\\\hline
  \multicolumn{1}{|m{\tempwidth}|}{\lipsum*[1]} & center\\\hline
  \multicolumn{1}{|b{\tempwidth}|}{\lipsum*[1]} & bottom\\\hline
\end{tabularx}
\end{document}

enter image description here

6
  • 3
    @MarcoDaniel Why column c vertical alignment is defined by changing column X?
    – user4035
    May 7, 2013 at 19:24
  • @user4035: Because the column type X is responsible for the baseline. May 7, 2013 at 19:26
  • 1
    @MarcoDaniel What if we have 3 columns: X|c|c. And want the 1-st c to be top aligned, and 2-nd c to be bottom aligned?
    – user4035
    May 7, 2013 at 19:31
  • @user4035: you can use the same trick or use David's trick May 7, 2013 at 19:49
  • @Marco May this changed over they year but based on my tests you are wrong. The the column is of type X is irrelevant. The important fact is that it contains the large multi-line text.
    – Robert
    Sep 5, 2019 at 15:02
34

enter image description here

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{ | X | c | }
  \hline
  \lipsum[1] & top\\
  \hline
  \noindent\parbox[c]{\hsize}{\lipsum[1]} & center\\
  \hline
  \noindent\parbox[b]{\hsize}{\lipsum[1]} & bottom\\
  \hline
\end{tabularx}

\end{document}
7
  • 2
    I absolutely love this simple solution, but for my liking there should be a bit of empty space between the cells. When I applied this to my table the cells are so close to each other that it looks kinda awkward. Is there any way to add a bit of empty space on the top and bottom of the cell?
    – Cadbon
    Apr 4, 2015 at 14:43
  • @Cadbon \renewcommand\arraystretch{2} or whatever number works for you Apr 4, 2015 at 15:02
  • 2
    Unfortunately I couldn't solve my issues with that method as my top row has only one row of text and the arraystretch command stretches all cells. This means that my top row only has a single row of text but a very large cell which looks silly.
    – Cadbon
    Apr 6, 2015 at 10:22
  • Any idea how I can center the lipsum text horizontally with this? I tried \parbox[c][][c] but that doesn't seem to help...
    – gghuffer
    Oct 5, 2015 at 2:05
  • @gghuffer as in any parbox or p or X column,, just use \centering Oct 5, 2015 at 6:58
14

Another way, using a minipage environment instead of a tabularx package:

\documentclass{article}
\usepackage{lipsum}
\usepackage[a4paper]{geometry}
\begin{document}

\begin{tabular}{|l|c|}
  \hline
  \begin{minipage}[t]{0.85\textwidth}\lipsum[1]\end{minipage} & top\\
  \hline
  \begin{minipage}{0.85\textwidth}\lipsum[1]\end{minipage} & center\\
  \hline
  \begin{minipage}[b]{0.85\textwidth}\lipsum[1]\end{minipage} & bottom\\
  \hline
\end{tabular}

\end{document}

enter image description here

7

My new LaTeX3 package tabularray has two new h and f alignment types for putting cell text at the head and foot of the row respectively:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\noindent
\begin{tblr}{
  colspec = {|X|c|}, rowspec = {Q[h]Q[m]Q[f]},
}
  \hline
  \lipsum[1] & top\\
  \hline
  \lipsum[1] & center\\
  \hline
  \lipsum[1] & bottom\\
  \hline
\end{tblr}
\end{document}

enter image description here

1
  • While this package is very new (might not be in TeXLive/etc. by default), it has several nice features: h/f valign option (which is closer to top/bottom vertical align in word processors), individual cell alignment with \SetCell (however, unfortunately there's no corresponding for middle-align i.e. halfway between the head and the foot, you mostly have to middle-align the whole row). For other features see the doc
    – user202729
    Nov 16, 2021 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.