[Tex/LaTex] Multirow Nested Table

multirownestingtables

I am trying to built a 6×6 table which contains mutlirows. My code is

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{multicol}
\usepackage{multirow}

\begin{document}
\begin{table}[H]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|} \hline
\multicolumn{6}{|c|}{$\mathbf{n_1n_2n_3}$}\\ \hline
\multirow{6}{*}{$500$} & \multirow{3}{*}{$410$} & \multirow{2}{*}{$320$} & {} & {}& $005$\\
{} & {} & {} & $203$ & $104$ & $041$\\
{} & {} & \multirow{2}{*}{$302$} & $230$ & $140$ & $014$\\
{} & \multirow{3}{*}{$401$} & {} & $221$ & $122$ & $032$\\
{} & {} & \multirow{2}{*}{$311$} & $212$ & $131$ & $023$\\
{} & {} & {} & {} & $113$ & $050$\\
\hline
\end{tabular}
\end{center}
\caption{Table}
\label{tab:matrix}
\end{table}
\end{document}

Mu output is

As you can see the 4th and 5th column from left are not fancy. How can I make this table a bit better-loking?

Best Answer

You can nest a tabular environment inside \multirow:

In the following code, I defined

  • \innerTab only for the first example, to clean up the actual tabular (1st example);
  • \mRowTab[<opt>]{<content>} which produces a \multirow of <opt> rows (default: 2) with its content <content> wrapped inside a centered-column tabular (2nd and 3rd example);
  • \innerTabular{<content>} which puts <content> into a one-columned vertically centered tabular (4th example).

As it stands the \mRowTab definition could be replaced by

\renewcommand*{\mRowTab}[2][2]{\multirow{#1}{*}{#2}}

which means that only one-rowed <text> is allowed (no tabular).

The second example is a copy of the first one with the new \mRowTab.

The third example is, in my opinion, “better-looking”.

The fourth example (not pictured) is a copy of the third one, only without \multirow but with \innerTabular Note that the first row doesn’t actually need \innerTabular.

Code

\usepackage{multicol}
\usepackage{multirow}
\newcommand*{\innerTab}{\begin{tabular}{@{}c@{}}
    $104$ \\
    $140$ \\
    $122$ \\
    $131$ \\
    $113$ \\
\end{tabular}}
\newcommand*{\mRowTab}[2][2]{\multirow{#1}{*}{\begin{tabular}{@{}c@{}}#2\end{tabular}}}
\renewcommand*{\mRowTab}[2][2]{\multirow{#1}{*}{#2}}
\newcommand*{\innerTabular}[1]{{\begin{tabular}[c]{@{}c@{}}#1\end{tabular}}}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multicolumn{6}{|c|}{$\mathbf{n_1n_2n_3}$}                                                           \\ \hline
                           &       & \multirow{2}{*}{$320$} &       & \multirow{6}{*}{\innerTab} & $005$ \\
                           & $410$ &                        & $203$ &                            & $041$ \\
    \multirow{2}{*}{$500$} &       & \multirow{2}{*}{$302$} & $230$ &                            & $014$ \\
                           &       &                        & $221$ &                            & $032$ \\
                           & $401$ & \multirow{2}{*}{$311$} & $212$ &                            & $023$ \\
                           &       &                        &       &                            & $050$ \\ \hline
\end{tabular}

\bigskip
\begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multicolumn{6}{|c|}{$\mathbf{n_1n_2n_3}$}                                  \\ \hline
                    &       & \mRowTab{$320$} &       & \mRowTab{$104$} & $005$ \\
                    & $410$ &                 & $203$ & \mRowTab{$140$} & $041$ \\
    \mRowTab{$500$} &       & \mRowTab{$302$} & $230$ & \mRowTab{$122$} & $014$ \\
                    &       &                 & $221$ & \mRowTab{$131$} & $032$ \\
                    & $401$ & \mRowTab{$311$} & $212$ & \mRowTab{$113$} & $023$ \\
                    &       &                 &       &                 & $050$ \\ \hline
\end{tabular}

\bigskip
\begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multicolumn{6}{|c|}{$\mathbf{n_1n_2n_3}$}                                  \\ \hline
                    &       &                 &       & \mRowTab{$104$} & $005$ \\
                    &       & \mRowTab{$320$} & $203$ & \mRowTab{$140$} & $041$ \\
    \mRowTab{$500$} & $410$ & \mRowTab{$302$} & $230$ & \mRowTab{$122$} & $014$ \\
                    & $401$ & \mRowTab{$311$} & $221$ & \mRowTab{$131$} & $032$ \\
                    &       &                 & $212$ & \mRowTab{$113$} & $023$ \\
                    &       &                 &       &                 & $050$ \\ \hline
\end{tabular}

\bigskip
\begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multicolumn{6}{|c|}{$\mathbf{n_1n_2n_3}$}                                  \\ \hline
    \innerTabular{500} & \innerTabular{410\\410} & \innerTabular{320\\302\\311} & \innerTabular{203\\230\\221\\212} & \innerTabular{104\\140\\122\\131\\113} & \innerTabular{005\\041\\014\\032\\023\\050} \\\hline
\end{tabular}
\end{document}

Ouput

enter image description here

Related Question