Graphics – Fixing Broken Vertical Alignment in Tabularray Tables with Graphics

graphicstabularrayvertical alignment

Example:

\documentclass[10pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{tabularray}
\UseTblrLibrary{varwidth}

\newcommand{\mymacro}{This macro expands \\ onto two lines}

\begin{document}

\begin{center}
\begin{tblr}{
        measure=vbox,
        width=0.9\linewidth,
        colspec={X[l,m] X[2,c,m]},
        hlines, vlines
    }
    {
        A single line of text \\
        Some additional text \\
        \mymacro
    } &
    \includegraphics[width=\linewidth, height=0.3\linewidth]{example-image}
\end{tblr}
\end{center}

\end{document}

The image may have variable aspect ratio; my immediate case is a wider one. Also showing h/vlines for illustration I don't need them.

Text left of image, vertically misaligned

To be precise, I want the two cells to align their bounding boxes on vertical centers, independent of contents.

I managed to figure out the macro expansion (this doesn't need expand, it needs varwidth!), but I can't find anything that controls image alignment. Wrapping the image in a \begin{tblr}{c}...\end{tblr} nearly works, but it spams a zillion warnings, and the h-spacing is wrong. (I don't see anything about nested tblrs, is this even allowed ever?)

Possibly related: tabularray, including \linewidth graphics in X columns causes large vertical space
(Is adjustbox fixing it?)

Best Answer

Update. It seems setting the first cell to h correctly aligns both elements.


You need to correct the baselines of both items.

I think \SetCell{h} sets the baseline of the text to its first line.

Then, as @UlrikeFischer suggests, use \raisebox{}{} to drag the image downwards by the height of an image minus some correction (e.g. height of one line of text). By doing so you effectively move its baseline upwards.

Then baselines of the two elements will match and you will get the desired alignment.

image

\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{tabularray}
    \UseTblrLibrary{varwidth}

\newcommand{\mymacro}{This macro expands\\onto two lines}

\begin{document}
\centering
\begin{tblr}{
        colspec={X[h] X[2]},
        measure=vbox,
        width=0.9\linewidth,
    }
    {A line of text\\Some additional text\\\mymacro}
        & \includegraphics[width=\linewidth]{example-image}
\end{tblr}
\end{document}

EDIT. Middle vertical alignment

enter image description here

\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{tabularray}
    \UseTblrLibrary{varwidth}

\newcommand{\mymacro}{This macro expands\\onto two lines}

\setlength{\fboxsep}{0pt}

\begin{document}
\centering
\begin{tblr}{
        colspec={X[h,m] X[2,m]},
        measure=vbox,
        width=0.9\linewidth,
    }
    \parbox{\linewidth}{A line of text\\Some additional text\\\mymacro}
        & \raisebox{-0.5\totalheight}{\includegraphics[width=\linewidth]{example-image}}
\end{tblr}
\end{document}