2

I am trying to vertically center some text in a cell in a table. I have found some similar examples that make use of \rotatebox. However, I can't seem to get vertical centering to work. Here is an example:

\def \figwidth {0.25\textwidth}
\setlength{\tabcolsep}{2pt}
\begin{figure*}[h]
    \centering
    \begin{tabular}{cccc}
       \toprule
        Velocity & s & q & r\\
        \midrule
        \rotatebox[origin=c]{90}{First row} & 
        \includegraphics[width=\figwidth]{example-image-a} & 
        \includegraphics[width=\figwidth]{example-image-b} & 
        \includegraphics[width=\figwidth]{example-image-c} \\
        \rotatebox[origin=c]{90}{Second} & 
        \includegraphics[width=\figwidth]{example-image-a} & 
        \includegraphics[width=\figwidth]{example-image-b} & 
        \includegraphics[width=\figwidth]{example-image-c} \\
        \bottomrule
    \end{tabular}
    \caption[EG]{Some example}
    \label{tbl:eg}
\end{figure*}

My output looks like this:

enter image description here

In case it isn't clear, the issue is with where it says "First row" and "Second", which should be aligned vertically with the first and second rows of the table. Many thanks!

1
  • 1
    Welcome to TeX.SE. – Mico Oct 27 '20 at 4:54
1

I suggest you use the m column type, which is provided by the array package, instead of the c column type for columns 2, 3, and 4 of the table. Not only are the contents of m-type columns centered vertically; the contents of other cells in the row will be centered vertically as well.

A quick remark about the code snippet \def \figwidth {0.25\textwidth}: It just happens to work for your code as the macro evaluates to a length. However, in other situations it might not work quite right, leading to much wasted time as you try to track down cryptic error messages. To avoid such outcomes, it would be better to define \figwidth as a length variable directly, say, via the instructions

\newlength\figwidth
\setlength\figwidth{0.25\textwidth}
 

enter image description here

\documentclass[twocolumn]{article}
\usepackage{graphicx,booktabs}

\newlength\figwidth
\setlength\figwidth{0.25\textwidth}

\usepackage{array}
% Set up a centered version of 'm' column type:
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}
\begin{figure*}[h]
\setlength{\tabcolsep}{2pt}
    \centering
    \begin{tabular}{@{} c *{3}{C{\figwidth}} @{}}
    \toprule
    Velocity & s & q & r\\
    \addlinespace %\midrule
    \rotatebox[origin=c]{90}{First row} & 
    \includegraphics[width=\linewidth]{example-image-a} & 
    \includegraphics[width=\linewidth]{example-image-b} & 
    \includegraphics[width=\linewidth]{example-image-c} \\
    \rotatebox[origin=c]{90}{Second row} & 
    \includegraphics[width=\linewidth]{example-image-a} & 
    \includegraphics[width=\linewidth]{example-image-b} & 
    \includegraphics[width=\linewidth]{example-image-c} \\
    \bottomrule
    \end{tabular}
    \caption[EG]{Some example} \label{tbl:eg}
\end{figure*}
\end{document}
1
  • Thanks for the comment regarding \def \figwidth {0.25\textwidth}, always good to learn something new :) – Mr Squid Nov 6 '20 at 21:43
1

I suggest to use tabularx for table , define new column type C as \newcolumntype{C}{>{\centering\arraybackslash}X} and insert images by use of \adjustbox and move images baseline to they vertical center by valign=c defined in the adjustbox package:

\documentclass[twocolumn]{article}
\usepackage[export]{adjustbox}% it load graphicx too
\usepackage{booktabs, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{figure*}
\setlength{\tabcolsep}{3pt}
\adjustboxset{width=\linewidth,valign=c, margin=0pt 3pt 0pt 3pt}
    \begin{tabularx}{\linewidth}{@{} c *{3}{C} @{}}
    \toprule
Velocity & s & q & r\\
    \midrule
\rotatebox[origin=c]{90}{First row} &
    \adjustimage{}{example-image-a} &
    \adjustimage{}{example-image-b} &
    \adjustimage{}{example-image-c} \\
\rotatebox[origin=c]{90}{Second row} &
    \adjustimage{}{example-image-a} &
    \adjustimage{}{example-image-b} &
    \adjustimage{}{example-image-c} \\
    \bottomrule
    \end{tabularx}
\caption[EG]{Some example} 
\label{tbl:eg}
    \end{figure*}
\end{document}

enter image description here

2
  • Is the adjustbox-based route needed? Won't \includegraphics[width=\linewidth]{example-x} work equally well? – Mico Oct 27 '20 at 10:03
  • @Mico, with it the code is slightly shorter (you not need to repeat width=..., valign=c at each \includegraphicx where valign push image baseline to vertical middle of the images). I just like to show another possible way. – Zarko Oct 27 '20 at 10:08

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.