3

I've run into a problem that I can't seem to find a solution. I am using package tabularray, since I have some long tables. My table has a mix of figures and text, and I would like the left-most column to be aligned in the middle and center. However, I have tried using column{1} = {valign=m}, cell{2}{1} = {m} and also \SetRow{m}, and other options (renewcommand, using package adjustbox) to no avail.

Below is a simplified code and the output.

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\usepackage{tabularray}
\NewColumnType{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{graphicx}

\begin{document}
\begin{longtblr}[
    caption = {My Table},
    ]{width=\textwidth,
        colspec = {| M{0.15\textwidth} | M{0.4\textwidth} | M{0.4\textwidth}|},
        rowhead = 1,
        row{even} = {gray9},
        row{1} = {white},
        cell{2}{1} = {m},
%       column{1} = {valign=m},
    }
    \hline
    \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} \\ \hline
    
    This cell should be centered vertically
    & {\includegraphics[width=0.35\textwidth]{example-image} \\ Text that can contain multiple lines but maybe not}
    & {\includegraphics[width=0.35\textwidth]{example-image} \\ Text that can contain multiple lines but maybe not} \\ \hline
    
\end{longtblr}

More text here.
\end{document}

Output

2 Answers 2

3

Using tabularray package, you should use its syntax, which is quit different form syntax of " classic" table packages. For details see package documentation:

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\usepackage{tabularray}
\usepackage{graphicx}

\begin{document}
    \begin{longtblr}[
caption = {My Table},
  label ={tab:l??}]{hlines, vlines,
                    colspec = {X[0.4,c,m] X[h,j] X[h,j]},
                    rowhead = 1,
                    row{even} = {gray9},
                    row{1} = {font=\bfseries, bg=white},
                    }
Column 1    & Column 2  & Column 3                          \\
This cell should be centered vertically
    &   \includegraphics[width=\linewidth]{example-image}\par
        Text that can contain multiple lines but maybe not
    &   \includegraphics[width=\linewidth]{example-image}\par
        Text that can contain multiple lines but maybe not  \\
\end{longtblr}

More text here.
\end{document}

enter image description here

If you prefer that text below image is horizontally centered, that use the following colspec:

colspec = {X[0.4,c,m] X[h,c]| X[h,c]},

For mor vertical padding in rows, you can add to table preamble option rowsep=<desired amount>. Default value is 2pt. Similarly you can change column separation by colsep=<desired amount> (default is 6pt). For example, considering equal separation between columns and rows gives:

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\usepackage{tabularray}
\usepackage{graphicx}

\begin{document}
    \begin{longtblr}[
caption = {My Table},
  label ={tab:l??}]{hlines, vlines,
                    colspec = {X[0.4,c,m] X[h,j] X[h,j]},
                    rowhead = 1,
                    row{even} = {gray9},
                    row{1} = {font=\bfseries, bg=white},
                    %
                    colsep=4pt, rowsep=4pt,  % <-----
                    }
Column 1    & Column 2  & Column 3                          \\
This cell should be centered horizontaly and vertically
    &   \includegraphics[width=\linewidth]{example-image-duck}\par
        Text that can contain multiple lines but maybe not
    &   \includegraphics[width=\linewidth]{example-image-duck}\par
        Text that can contain multiple lines but maybe not  \\
\end{longtblr}

More text here.
\end{document}

enter image description here

1
  • Thank you! That worked perfectly as I wanted.
    – Chaiane
    Commented Nov 8, 2021 at 18:27
2

enter image description here

It is possible to get the needed output using NiceTabularX from nicematrix package

\documentclass{article}

\usepackage{xcolor}

\usepackage{nicematrix}

\begin{document}

\begin{table}[htp]

    \caption{My Table}

    \begin{NiceTabularX}{\textwidth}[hvlines,cell-space-limits=1.1mm,code-before=\rowcolor{gray!10}{2}]{m[c]{1.8cm} X[1, c, m] X[1, c, m]}
        
        \RowStyle[bold]{} Column 1 & Column 2 & Column 3
        \\
        
        This cell is now centered vertically & \Block{1-1}{\includegraphics[width=\linewidth]{example-image} \\ Text that can contain multiple lines but maybe not} & \Block{1-1}{\includegraphics[width=\linewidth]{example-image} \\ Text that can contain multiple lines but maybe not}
        \\
        
    \end{NiceTabularX}

\end{table}

\end{document}

You must log in to answer this question.

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