嵌套表中的 Latex 文本对齐

嵌套表中的 Latex 文本对齐

我希望将“未找到原因”文本左对齐,以便它与其他行保持一致。

\begin{table}%[htb!]
    \caption{Reasons}
    \centering
    \begin{tabular}{|p{2cm}|l|l|l|l|}
        \hline
        \textbf{Scheme} & \textbf{R } & \textbf{Type} & \textbf{State} & \textbf{P} \\ \hline
        \multirow{2}{*}{V (3)} 
        & M (2) & \multirow{4}{*}{S } & Accepted & 2 \\ \cline{2-2} \cline{4-5} 
        & No M. (1) &  & Rejected & 1 \\ \cline{1-2} \cline{4-5} 
        
        \multirow{2}{*}{P (2)} & M (1) &  & Accepted & 1 \\ \cline{2-2} \cline{4-5} 
        & No M (1) &  & Rejected & 1 \\ \hline
        
        \multirow{5}{*}{              
            \begin{tabular}{l}Reason\\not found\\(38) \end{tabular}
        } & & \multirow{2}{*}{S} & Accepted & 32 \\ \cline{4-5} 
        &  &  & Rejected & 3 \\ \cline{3-5} 
        &  & P & Accepted & 2 \\ \cline{3-5} 
        &  & I & Accepted & 1 \\   \hline  % \cline{2-5} 
        
    \end{tabular}
\end{table}

在此处输入图片描述

答案1

{NiceTabular}您可以轻松地使用创建表格nicematrix

在该环境中,您可以使用命令垂直和水平合并单元格\Block。键hvlines绘制除这些块之外的所有规则。

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{lllll}[hvlines]
\textbf{Scheme} & \textbf{R } & \textbf{Type} & \textbf{State} & \textbf{P} \\ 
\Block{2-1}{V (3)} 
& M (2) & \Block{4-1}{S} & Accepted & 2 \\ 
& No M. (1) &  & Rejected & 1 \\ 
\Block{2-1}{P (2)} & M (1) &  & Accepted & 1 \\ 
& No M (1) &  & Rejected & 1 \\ 
\Block{4-1}{Reason\\ not found\\ (38)}
& \Block{4-1}{} & \Block{2-1}{S} & Accepted & 32 \\ 
&  &  & Rejected & 3 \\ 
&  & P & Accepted & 2 \\
&  & I & Accepted & 1 \\
\end{NiceTabular}

\end{document}

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

上述代码的输出

答案2

在此处输入图片描述

我不会将表格嵌套在单元格中,而是定义第一列的宽度,而不是

\multirow{5}{*}{\begin{tabular}{l}Reason\\not found\\(38) \end{tabular}

使用

\multirow{4}{=}{Reason not found (38)}

它可以自动拆分单元格文本。这样我就可以避免单元格内容对齐的问题。

完成 MWE:

\documentclass{article}
\usepackage{multirow}
\usepackage[skip=1ex]{caption}

\begin{document}
\begin{table}[ht]
    \caption{Reasons}
    \centering
    \begin{tabular}{|p{2cm}|l|c|l|l|}
        \hline
\textbf{Scheme} & \textbf{R } & \textbf{Type} & \textbf{State} & \textbf{P} \\ \hline
        \multirow{2}{*}{V (3)}
        & M (2) & \multirow{4}{*}{S } & Accepted & 2 \\ \cline{2-2} \cline{4-5}
        & No M. (1) &  & Rejected & 1 \\ \cline{1-2} \cline{4-5}
\multirow{2}{*}{P (2)} & M (1) &  & Accepted & 1 \\ \cline{2-2} \cline{4-5}
        & No M (1) &  & Rejected & 1 \\ \hline

\multirow{4}{=}{Reason not found (38)}
        &   & \multirow{2}{*}{S} & Accepted & 32    \\ \cline{4-5}
        &   &                    & Rejected & 3     \\ \cline{3-5}
        &   & P & Accepted       & 2 \\ \cline{3-5}
        &   & I & Accepted       & 1 \\   \hline  % \cline{2-5}
    \end{tabular}
\end{table}
\end{document}

附录:

今天,我将用包中定义的环境来代替旧的tabular表环境。使用它,表代码会更加清晰和简短:tblrtabularray

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}

\begin{document}
    \begin{table}[ht]
\caption{Reasons}
\centering
    \begin{tblr}{hline{1,Z}=1pt, hline{2}=0.6pt, hline{3-Y}=0.2pt, 
                 vlines,
                 colspec= {*{5}{Q[l,m]}},
                 row{1} = {font=\bfseries}
                 }
Scheme  & R     & Type                  & State     & P     \\
\SetCell[r=2]{l}    V (3) 
        & M (2) & \SetCell[r=4]{l}  S   & Accepted  & 2     \\  
        & No M. (1) &                   & Rejected  & 1     \\
\SetCell[r=2]{l}    P (2)
        & M (1) &                       & Accepted  & 1     \\
        & No M (1) &                    & Rejected  & 1     \\
\SetCell[r=4]{l}  {Reason not\\ found (38)}
        & \SetCell[r=4]{l}       
                & \SetCell[r=2]{l}  S   & Accepted  & 32    \\
        &       &                       & Rejected  & 3     \\
        &       & P                     & Accepted  & 2     \\ 
        &       & I                     & Accepted  & 1     \\
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

相关内容