嵌套表格对齐问题

嵌套表格对齐问题

下列的例如,我尝试稍微扩展一下。

\begin{table}
\centering
\begin{tabular}{ |c|c|@{}c@{}| }% <-- aded @{}
\hline
no test & no test & \begin{tabular}{c|c|c} 1 & 2 & 3 \\ \hline 4 & 5 & 6 \\ \end{tabular} \\
\hline
no test & no test & 
    \begin{tabular}{c|c|c} D1 & 1.23 & 5 \\ \hline D2 & 1.23 & 6 \\ \end{tabular} \\
\hline
\end{tabular}
\end{table}

我懂了:

在此处输入图片描述

@{}c@{}我在第三列中使用。为什么我有两种不同的嵌套表外观?

答案1

这里有一些可能的解决方案,请选择您喜欢的。

\documentclass{book}

\usepackage{array}
\renewcommand{\arraystretch}{1.3}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\usepackage{calc}
\newlength{\myone}
\newlength{\mytwo}
\newlength{\mythree}

\usepackage{multirow}

\usepackage{caption}

\begin{document}
\begin{table}
    \caption{Table with nested tables with column width 
        as wide as the largest text of the column}
    \centering
    \setlength{\myone}{\widthof{D1}}% larger text of the first column
    \setlength{\mytwo}{\widthof{1.23}}% larger text of the second column
    \setlength{\mythree}{\widthof{6}}% larger text of the second column
    \begin{tabular}{ |c|c|@{}c@{}| }% <-- aded @{}
        \hline
        no test & no test & \begin{tabular}{C{\myone}|C{\mytwo}|C{\mythree}} 1 & 2 & 3 \\ \hline 4 & 5 & 6 \\ \end{tabular} \\
        \hline
        no test & no test & 
        \begin{tabular}{C{\myone}|C{\mytwo}|C{\mythree}} D1 & 1.23 & 5 \\ \hline D2 & 1.23 & 6 \\ \end{tabular} \\
        \hline
    \end{tabular}
\end{table}
\begin{table}
    \caption{Table with nested tables with fixed column width}
    \centering
    \begin{tabular}{ |c|c|@{}c@{}| }% <-- aded @{}
        \hline
        no test & no test & \begin{tabular}{C{2em}|C{2em}|C{2em}} 1 & 2 & 3 \\ \hline 4 & 5 & 6 \\ \end{tabular} \\
        \hline
        no test & no test & 
        \begin{tabular}{C{2em}|C{2em}|C{2em}} D1 & 1.23 & 5 \\ \hline D2 & 1.23 & 6 \\ \end{tabular} \\
        \hline
    \end{tabular}
\end{table}
\begin{table}
    \caption{Table without nested tables with multirow and variable column widths}
    \centering
    \begin{tabular}{ |*5{c|} }
        \hline
        \multirow{2}{*}{no test} & \multirow{2}{*}{no test} & 1 & 2 & 3 \\ 
        \cline{3-5} 
        & & 4 & 5 & 6 \\ 
        \hline
        \multirow{2}{*}{no test} & \multirow{2}{*}{no test} & D1 & 1.23 & 5 \\ 
        \cline{3-5}
        & & D2 & 1.23 & 6 \\ 
        \hline
    \end{tabular}
\end{table}
\begin{table}
    \caption{Table without nested tables with multirow and fixed column widths}
    \centering
    \begin{tabular}{ |*2{c|}*3{C{2em}|} }
        \hline
        \multirow{2}{*}{no test} & \multirow{2}{*}{no test} & 1 & 2 & 3 \\ 
        \cline{3-5} 
        & & 4 & 5 & 6 \\ 
        \hline
        \multirow{2}{*}{no test} & \multirow{2}{*}{no test} & D1 & 1.23 & 5 \\ 
        \cline{3-5}
        & & D2 & 1.23 & 6 \\ 
        \hline
    \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

相关内容