2

I would like to vertically align several table cells, one of which includes an array. Consider the following MWE:

\documentclass{article}

\usepackage{graphicx}
\usepackage{array}

\begin{document}

\begin{tabular}{*{5}{m{0.2\textwidth}}}
    Some text &
    \includegraphics[width=15mm]{example-grid-100x100pt} &
    \(\left(\begin{array}{cc}
        1 & 0 \\
        0 & 1
    \end{array}\right)\) &
    \((1,0,1,0)\) &
    \rule{15mm}{15mm}
\end{tabular}

\end{document}

Which yields (green reference line added by me):

actual output

The text, image, simple inline math, and rule are all correctly aligned vertically, but the array is pushed down. Instead I would have expected the following:

expected output

Why is the array not centered vertically? What is the easiest way to fix this?

2

Problem is in your \rule (where it has baseline?). If you encapsulate it in a some box, to which you can define baseline, for example with help of the macro \valign from the \adjustbox, the same do for image in the second column, than all elements of tabular in p columns type will have baseline vertical centered:

\documentclass{article}
\usepackage[export]{adjustbox} % also load graphicx package
\usepackage{array}

\begin{document}
\noindent%
\begin{tabular}{*{5}{>{\centering\arraybackslash}p{\dimexpr0.2\textwidth-2\tabcolsep}}} 
    Some text &
    \includegraphics[width=15mm,valign=c]{example-grid-100x100pt} &
    \(\left(\begin{array}{cc}
        1 & 0 \\
        0 & 1
    \end{array}\right)\) &  \((1,0,1,0)\) & \adjustbox{valign=c}{\rule{15mm}{15mm}}
    \end{tabular}
\end{document}

enter image description here

(red lines indicate text border)

As you can see, I also change columns width that table can fit in text area.

| improve this answer | |
  • Thanks, this solves my problem! Maybe it's also worth mentioning in your answer that you changed the table layout from m to p, which I overlooked at first. With m, the vertical centering is still off, but using p and \adjustbox works perfecty fine! – einbandi Nov 4 '19 at 15:36
  • @einbandi, you are right. I should mentioned this too. I will extend my explanation in answer asap. – Zarko Nov 4 '19 at 15:40

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.