2

How do I define a column type so that, in a table, in every cell of every column of that type, the contents is bottom-aligned? (This contents is some text, not an image.)

I don't want every cell in the table bottom-aligned. I want the contents of every cell of the leftmost column top-aligned (which indeed happens because it is the default; no problem there). But by contrast I want the contents of every cell of the rightmost column bottom-aligned.

I have tried to adapt suggested methods in three answers, as shown below. The MWE below shows three attempts. In each, I restrict the second column's width and put so much text in the cell that it spills over to a second line. This makes bottom-alignment in the rightmost column distinguishable from top-alignment. I want the rightmost column's type defined so that the rightmost cell's contents is bottom-aligned. However, in each case the text is set too high.

Just in case it's relevant: I'm typesetting using LuaLaTeX.

MWE:

\documentclass[12pt,a4paper]{article}
\usepackage{array, tabularx}

% See https://tex.stackexchange.com/a/395359
% This is worse for 2 reasons: 1) the \hline at the bottom of the table doesn't compile 2) the s cells' contents is even higher than the other cells'.
\newcolumntype{s}{>{\centering}b{.2\textwidth}}

% See https://tex.stackexchange.com/a/105671
% This top-aligns cell 3's contents within its cell, rather than bottom-aligning it.
%\def\tabularxcolumn#1{b{#1}}
% See https://tex.stackexchange.com/a/244213 re col type X and right-hand col. \centering horizontally centres the contents between left and right, but the contents is still top-aligned.
\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}b{#1}}

\begin{document}

\begin{tabular}{lp{5cm}|b{3cm}|}
\hline
first & using the built-in tabular environment & last\\
\hline
\end{tabular}

\begin{tabular}{lp{5cm}|s|}
\hline
first & using a newly-defined column type s & last\\
\end{tabular}

\begin{tabularx}{\textwidth}{lp{5cm}|X|}
\hline
first & using the package tabularx and its col type X & last\\
\hline
\end{tabularx}

\end{document}
3
  • you want b not m or p (I don't understand the comment above the commented out b use) Sep 29, 2023 at 13:16
  • @DavidCarlisle I tried b in my MWE's first {tabular} environment; the result is that the word "last" vertically aligns to the other cells' top lines, whereas I want it to align with the bottom line. I will correct a mistaken m to a b; however, even with m, the cell's contents vertically align to the other cell's top line rather than midway down.
    – Rosie F
    Sep 29, 2023 at 13:51
  • if you use b for columns 2 and 3 then the text in the column 1 will align with both their bottom lines Sep 29, 2023 at 14:04

3 Answers 3

2

If you use b for the columns 2 and 3 you get

enter image description here

which is what I think you want, although if you want first aligned with the top and last aligned with the bottom, it's a bit harder ad the middle column only records one reference point.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx}


\renewcommand\tabularxcolumn[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{tabular}{wl{1cm}b{5cm}|b{3cm}|}
\hline
&
\noindent\makebox[0pt][l]{\hspace{-\dimexpr1cm+2\tabcolsep}first}%
using the built-in tabular environment & last\\
\hline
\end{tabular}


\end{document}
2
  • Your latter is indeed what I want: top-alignment in the leftmost column and bottom-alignment in the rightmost. I didn't realise that combining two sorts of vertical alignments in columns in the same table was so tricky. I will edit the OP to clarify what I want.
    – Rosie F
    Sep 29, 2023 at 14:19
  • 1
    @RosieF the thing to remember is that b or m or p do not refer to how that cell aligns with the other columns, they are just about where the reference point of this column is. so if the middle column is p its reference point is its first baseline, so the l baseline in column 1 is on that line of the first line of column 2, but if you then make column 3 be b its reference point is on its bottom line, so the bottom of column 3 is aligned with the top of column 2,which is logical but not usually what you want. Sep 29, 2023 at 14:23
1

As supplement to nice @David Carlisle answer (+1).

With use of the tabularray package, it is simple to achieve what you after:

\documentclass[12pt,a4paper]{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{colspec = {Q[l,h, 3cm] Q[l,h, 5cm] | Q[l,f, 3cm]|}}
    \hline
first (top)     
    &   using the built-in tabular environment 
        & last (bottom \\
    \hline
\end{tblr}
\end{document}

enter image description here

Option h in column specification push text in cells to the top and option f to the bottom.

1

Here is a solution with the environment {NiceTabular} of nicematrix (recommended only if you use other features of nicematrix...).

\documentclass[12pt,a4paper]{article}
\usepackage{nicematrix}
\usepackage{collcell}

\begin{document}

\newcommand{\bBlock}{\Block[B]{}}
\newcolumntype{B}{>{\collectcell\bBlock}c<{\endcollectcell}}

\begin{NiceTabular}{cp[l]{5cm}B}[hvlines]
first (top)     
    &   using the environment \verb|{NiceTabular}| of \verb|nicematrix|
        & last (bottom) \\
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .