1

I've been test driving David Carlisle's solution to vertically aligning individual cells in a table, but I find that it disrupts the vertical spacing to the row above (see MWE below). Is there some smarter or more elegant way to solve this than just manually estimating the the required the interline spacing adjustment?

\documentclass[12pt]{article}
\usepackage{tabularx}
\begin{document}
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{X r}
      A row heading & 123 \\
      A second row heading that is very long and will surely break & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
  \clearpage
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{X r}
      A row heading & 123 \\
      \parbox[b]{\hsize}{A second row heading that is very long and will surely break} & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
\end{document}

Animated MWE

Fredrik P
  • 1,386
  • 2
    the nested parbox was just to show different alignment on different rows, if you want the whole column to use a [b] parbox just use an X column and configure it to use a b column – David Carlisle Aug 07 '21 at 20:22
  • Please clarify what your formatting objective is. In particular, where should the number "456" be placed? At the top, the middle, or the bottom? – Mico Aug 07 '21 at 20:23
  • @Mico The number 456 should be at the bottom. The lengthy row heading should be exactly as the first table. – Fredrik P Aug 07 '21 at 20:34
  • 1
    @DavidCarlisle Using the array package's > and < functionality you mean? Wouldn’t that just ensure that the entire table displayed this vertical jump? – Fredrik P Aug 07 '21 at 20:40
  • 3
    no use an X column but add \renewcommand{\tabularxcolumn}[1]{b{#1}} before the table, see the tabularx doc. – David Carlisle Aug 07 '21 at 20:47

2 Answers2

2

The height of the \parbox in the second table exceeds the height of the strut which makes the distance between baselines of texts of cells consisting of single lines of text. Therefore, this \parbox is not aligned so that the baseline of its first text line is level with the baselines of adjacent cells, but the upper boundary line of this \parbox abuts the lower boundary line of the box of the table-cell above it.

As long as you don't play with \arraystretch and/or \extrarowheight, you can prepend and append \struts to the content of the \parbox for ensuring proper height and depth:

\documentclass[12pt]{article}
\usepackage{tabularx}
\begin{document}
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{X r}
      A row heading & 123 \\
      A second row heading that is very long and will surely break & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
  \clearpage
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{X r}
      A row heading & 123 \\
      \parbox[b]{\hsize}{\strut A second row heading that is very long and will surely break\strut} & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
\end{document}

enter image description here

A better approach might be loading the package array and defining a column-type for bottom-alignment—\TX@col@width is the width of X-columns:

\documentclass[12pt]{article}
\usepackage{tabularx, array}
\makeatletter
\newcolumntype{B}{b{\TX@col@width}}%
\makeatother
\begin{document}
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{X r}
      A row heading & 123 \\
      A second row heading that is very long and will surely break & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
  \clearpage
  \begin{table}[h]
    \begin{tabularx}{0.5\linewidth}{B r}
      A row heading & 123 \\
      A second row heading that is very long and will surely break & 456 \\
      A third row heading & 789 \\
    \end{tabularx}
  \end{table}
\end{document}

enter image description here

Ulrich Diez
  • 28,770
2

If alignment along the bottom baseline of X-type cells is desired for all rows of the table, I suggest you run

 \renewcommand\tabularxcolumn[1]{b{#1}}

after loading the tabularx package. No need for \parbox wrappers.

enter image description here

\documentclass[12pt]{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{b{#1}} % <-- new

\begin{document} \begin{table}[h] \begin{tabularx}{0.5\linewidth}{X r} A row heading & 123 \ A second row heading that is very long and will surely break & 456 \ A third row heading & 789 \ \end{tabularx} \end{table} \end{document}

Mico
  • 506,678