# Vertical alignment of *all* cells when multirow is used to join some cells

I'm trying to achieve a good vertical alignment for a table whose cells contain alternatively an image or text, but I'm confused by the interaction of adjustbox with multirow.

The MWE is the following, in which using \adjustbox{valign=c}{\imma} instead of \imma allows a good alignment with the textual non-multirow cells. Without it, the \multirow{2}*{\imma} gets good alignment with the various\imma, but the textual cells are bottom-aligned.

Since this table mainly contains figures, and short text, the question is basically _how do I get all cells centered vertically? (Horizontal alignment through r, l, c, ... column types should not be affected, preferrably.)

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\newcommand{\imma}{\includegraphics[height=1cm,width=3cm]{example-image-a}}
\begin{tabular}{@{}lcllc@{}}
\toprule
\multirow{2}*{0} & c2f & \multirow{2}*{\imma} & \imma &  \multirow{2}*{\imma} \\
\cmidrule(lr){2-2}
\cmidrule{4-4}
& f2c & & \imma &   \\
\midrule
\multirow{2}*{1} & c2f &  \imma & \imma &  \multirow{2}*{\imma} \\
\cmidrule(l){2-4}
& f2c &  \imma & \imma &   \\
\midrule
\cmidrule(l){2-3}
& f2f &  \adjustbox{valign=c}{\imma} &  &   \\
\bottomrule
\end{tabular}
\end{document}


which produces

Here's a fundamental concept to understand: When you wish to align content at the vertical center, you're adjusting the anchor point. As a tangible example, consider aligning these two elements vertically:

x \includegraphics[height=1cm,width=3cm]{example-image}


Both items above are aligned (vertically) at their baseline. Intuitively one would want to set a vertical alignment for x so it sits at the vertical centre of the image, as in

\adjustbox{valign=c}{x} \includegraphics[height=1cm,width=3cm]{example-image}


However, this does not yield the desired result. Instead, you should adjust the anchor point for the image to its centre (thanks to \usepackage[export]{adjustbox}):

x \includegraphics[height=1cm,width=3cm,valign=c]{example-image}


In fact, it would be better to use

\adjustbox{valign=c}{x} \includegraphics[height=1cm,width=3cm,valign=c]{example-image}


(but it's not always necessary and may depend on both elements).

With this in mind, here is a slightly different way of constructing the same table without multirow:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\newcommand{\img}{\includegraphics[height=1cm,width=3cm,valign=c]{example-image}}

\begin{tabular}{ @{} l *{4}{@{} c } @{} }
\toprule
\vphantom{\img}c2f \\
\midrule
\vphantom{\img}f2c
\end{tabular} & \img & \begin{tabular}{ c }
\img \\
\midrule
\img
\end{tabular} & ~\img \\
\midrule
\adjustbox{valign=c}{1~} & \multicolumn{3}{@{} c @{}}{\begin{tabular}{ *{3}{c} }
c2f & \img & \img \\
\midrule
f2c & \img & \img
\end{tabular}} & ~\img \\
\midrule
\adjustbox{valign=c}{2~} & \multicolumn{2}{@{} c @{}}{\begin{tabular}{ *{2}{c} }
c2f & \img \\
\midrule
f2c & \img
\end{tabular}} & & ~\img \\
\bottomrule
\end{tabular}

\end{document}

• Two sub-question: 1. Is there any reason why you preferred nesting tabulars wrt using multirow? Most importantly: how does valign=c work? I mean, all boxes on the same line are aligned wrt their central line, instead of baseline? In this case, I would expect your last 1-line example (valign in both text and image) is the only correct one, but it seems that you mean something else... – Enrico Maria De Angelis Oct 26 '18 at 18:09
• @EnricoMariaDeAngelis: 1) \multirow inside a tabular that uses booktabs rules are not always aligned exactly, because the rules inserted via \midrule and \cmidrule uses extra space above/below. Instead, I just set each component on their own rather than in a big all-inclusive tabular. tabulars are necessarily vertically aligned at the centre. 2) valign=c changes the anchor point from whatever it is for the object to the vertical centre of that object. – Werner Oct 26 '18 at 18:21
• would you be so kind to wrote something more about but it's not always necessary and may depend on both elements? – Enrico Maria De Angelis Oct 28 '18 at 12:57
• @EnricoMariaDeAngelis: Some elements, like tabular (or minipage) already have a vertically centred anchor point. See x \begin{tabular}{c} a \\ b \\ c \end{tabular}. So, in these cases it's not necessary to \adjustbox{valign=c}`. – Werner Oct 28 '18 at 17:06