表格中的单元格未与 tabularray 垂直对齐

表格中的单元格未与 tabularray 垂直对齐

我遇到了一个似乎找不到解决方案的问题。我正在使用包tabularray,因为我有一些长表。我的表格混合了数字和文本,并且我希望最左边的列与中间对齐。但是,我尝试过使用column{1} = {valign=m}cell{2}{1} = {m}以及\SetRow{m},以及其他选项(renewcommand,使用包adjustbox),但都无济于事。

下面是简化的代码和输出。

\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}

输出

答案1

使用tabularray包时,应使用其语法,该语法与“经典”表包的语法完全不同。有关详细信息,请参阅包文档:

\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}

在此处输入图片描述

如果您希望图像下方的文本水平居中,请使用以下命令colspec

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

为了增加行中的垂直填充,您可以添加到表格前言选项中rowsep=<desired amount>。默认值为2pt。同样,您可以更改列分隔colsep=<desired amount>(默认为 6pt)。例如,考虑列和行之间的相等分隔,可得出:

\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}

在此处输入图片描述

答案2

在此处输入图片描述

可以使用包获取所需NiceTabularXnicematrix输出

\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}

相关内容