3

as the title states, I've had trouble centering text vertically in a LaTeX table. Something that should be really simple; I can do it with only click in Word, and every answer I've found online is either extremely complicated, or doesn't work for me.

A lot of the questions are very very specific, but I've tried my best to simplify the question as much as possible.

\documentclass[12pt]{article}
\usepackage{graphicx} % For example picture

\begin{document}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|}
    \hline
    \textbf{1} & \textbf{2} & \textbf{3} \\ \hline
        a & \includegraphics[width=0.15\linewidth]{example-image-a} & b \\ \hline
        c & \includegraphics[width=0.15\linewidth]{example-image-b} & d \\ \hline
    \end{tabular}
\end{table}
\end{document}

This prints a table like this:

I'd like the text in column 1 and 3 to be centered vertically - ideally also the picture (which borders the top of the cell), but I can do without it.

Thanks in advance!

1

1 Answer 1

5

With the help of cellspace for the spacing above and below the image and valign=c from the adjustbox package to vertically center the contents:

enter image description here

\documentclass[12pt]{article}
\usepackage{graphicx} % For example picture
\usepackage[export]{adjustbox}
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{\tabcolsep}
\setlength{\cellspacebottomlimit}{\tabcolsep}

\begin{document}
\begin{table}[]
    \centering
    \begin{tabular}{|0c|0c|0c|}
    \hline
    \textbf{1} & \textbf{2} & \textbf{3} \\ \hline
        a & \includegraphics[width=0.15\linewidth, valign=c]{example-image-a} & b \\ \hline
        c & \includegraphics[width=0.15\linewidth, valign=c]{example-image-b} & d \\ \hline
    \end{tabular}
\end{table}
\end{document}
3
  • Thank you, this did exactly what I wanted! However, I ended up with an error (see link), do you know exactly where I should use \tabulinesep ? link Jun 21, 2020 at 21:50
  • 1
    Do you use the tabu package in your actual document? If so, please be warned that this package is currently unmaintained and can cause some undesired outputs/side effects. I therefore encourage you to not continue using this package. As far as I know most, if not all, features of this package can also be achieved with other, maintained and more compatible packages as well.
    – leandriis
    Jun 21, 2020 at 21:57
  • I do, sorry, I missed that. I've removed that package, I wasn't actually using it anymore. Sorry for the confusion, and thanks again for your help! Jun 21, 2020 at 22:15

You must log in to answer this question.

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