0

Following this example, I try to extend it a little bit.

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

I got this:

enter image description here

I use @{}c@{} in the third column. Why I have two different looks of nested table?

  • 3
    because the nested table in the second row is wider than in the first row and column width is accommodated to wider table. – Zarko May 22 '18 at 12:50
  • 3
    That's because the the nested tables do not have the same width. – Bernard May 22 '18 at 12:51
  • 1
    @Zarko: Great minds think together :o) – Bernard May 22 '18 at 12:51
  • @Bernard, its look like :-), however you use fewer words than me to express our thoughts ... – Zarko May 22 '18 at 12:59
1

Here some possible solutions, choose what you prefer.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.