tabularx 和 multirow:垂直居中不正确

tabularx 和 multirow:垂直居中不正确

我正在尝试制作一个表格,其中左列中的文本可以包含换行符,并且相对于三列的高度垂直居中。具体来说,我有以下最小示例:

\documentclass{article}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\renewcommand{\arraystretch}{1.4}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{table}[]
    \centering
    \begin{tabularx}{\linewidth}{P{1.5cm}P{2.6cm}X}
        \toprule
        \textbf{Method}  & \textbf{Frame} & \textbf{Headline}                  \\ 
        \midrule
        \multirow{3}{=}{mylabel}&    sometext (some information)           & this is text that is somewhat longer than the first two columns      \\ 
         &      sometext (some information)        & this is text that is somewhat longer than the first two columns. it's also longer than the text in the first column. much much much much much much much much longer actually!                          \\
        &     sometext (some information)          & this is text that is somewhat longer than the first two columns          \\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document}

请注意,在生成的 PDF 中,左标签没有垂直居中。 在此处输入图片描述

我知道下面列出的各种相关问题,但没有一个问题解决这个错误垂直居中的左文本的特定问题:

答案1

在 环境中{NiceTabular}nicematrix您可以使用命令\Block来垂直和水平合并单元格。块的内容位于块的数学中心。这意味着,当您想要垂直合并时(如您的示例所示),您必须指定逻辑行数(而不是像 那样指定物理行数\multicolumn)。

但是,没有{NiceTabularX}环境(可能在后续版本中)。这意味着您必须“手动”计算第三列的宽度。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}


\begin{document}
\begin{table}[]
    \centering
    \begin{NiceTabular}{P{1.5cm}P{2.6cm}P{\dimexpr \linewidth -4.1cm - 6 \tabcolsep}}
        \toprule
        \textbf{Method}  & \textbf{Frame} & \textbf{Headline}                  \\ 
        \midrule
        \Block[l]{3-1}{mylabel}&    sometext (some information)           & this is text that is somewhat longer than the first two columns      \\ 
         &      sometext (some information)        & this is text that is somewhat longer than the first two columns. it's also longer than the text in the first column. much much much much much much much much longer actually!                          \\
        &     sometext (some information)          & this is text that is somewhat longer than the first two columns          \\
        \bottomrule
    \end{NiceTabular}
\end{table}
\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

第一个代码的输出

您可以\\输入命令\Block。例如:

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}


\begin{document}
\begin{table}[]
    \centering
    \begin{NiceTabular}{P{1.5cm}P{2.6cm}P{\dimexpr \linewidth -4.1cm - 6 \tabcolsep}}
        \toprule
        \textbf{Method}  & \textbf{Frame} & \textbf{Headline}                  \\ 
        \midrule
        \Block[l]{3-1}{mylabel\\ 2nd line}&    sometext (some information)           & this is text that is somewhat longer than the first two columns      \\ 
         &      sometext (some information)        & this is text that is somewhat longer than the first two columns. it's also longer than the text in the first column. much much much much much much much much longer actually!                          \\
        &     sometext (some information)          & this is text that is somewhat longer than the first two columns          \\
        \bottomrule
    \end{NiceTabular}
\end{table}
\end{document}

第二段代码的输出

如果您希望在 中自动换行\Block,则必须\parbox在块中放置一个(具有合适的宽度:也许这会在 的后续版本中自动实现nicematrix)。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}


\begin{document}
\begin{table}[]
    \centering
    \begin{NiceTabular}{P{1.5cm}P{2.6cm}P{\dimexpr \linewidth -4.1cm - 6 \tabcolsep}}
        \toprule
        \textbf{Method}  & \textbf{Frame} & \textbf{Headline}                  \\ 
        \midrule
        \Block[l]{3-1}{\parbox{1.5cm}{some text, other text, other text}}&    sometext (some information)           & this is text that is somewhat longer than the first two columns      \\ 
         &      sometext (some information)        & this is text that is somewhat longer than the first two columns. it's also longer than the text in the first column. much much much much much much much much longer actually!                          \\
        &     sometext (some information)          & this is text that is somewhat longer than the first two columns          \\
        \bottomrule
    \end{NiceTabular}
\end{table}
\end{document}

上述代码的输出

答案2

\multirow是为单行单元格和默认的 \arraystretch 设计的。对于您的情况,您必须计算三行中的总数lines作为 \multirow 第一个参数的起点,并通过反复试验来调整此参数。请注意,此参数可能有小数部分以进行微调。

\documentclass{article}

\usepackage{multirow}
\usepackage{tabularx}
\usepackage{booktabs}

\renewcommand{\arraystretch}{1.4}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}

\begin{table}[]
    \centering
    \begin{tabularx}{\linewidth}{P{1.5cm}P{2.6cm}X}
        \toprule
        \textbf{Method} & \textbf{Frame} & \textbf{Headline} \\
        \midrule
        \multirow{7.4}{=}{mylabel}& sometext (some information) & this is text that is somewhat longer than the first two columns \\
         & sometext (some information) & this is text that is somewhat longer than the first two columns. it's also longer than the text in the first column. much much much much much much much much longer actually! \\
        & sometext (some information) & this is text that is somewhat longer than the first two columns \\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述

相关内容