1

I am trying to built a 6x6 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?

1
  • 1
    How should the column appear?
    – egreg
    Dec 20, 2012 at 7:45

1 Answer 1

4

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

4
  • For the last example you don't need \multirow at all; each column can be a nested tabular.
    – egreg
    Dec 20, 2012 at 8:48
  • @egreg That is true. I’ve updated my answer with that option. Thank you. Dec 20, 2012 at 9:29
  • maybe even \let\ms\multispan\def\cell#1 {\vfil\hbox spread 1em{$\vphantom{\big(}#1$\hfil}\vfil}\valign{&\cell{#} \cr\ms6\cell500 \cr\ms3\cell410 &\ms3\cell401 \cr\ms2\cell320 &\ms2\cell302 &\ms2\cell311 \cr&203&230&221&212\cr&104&140&122&131&113\cr005&041&014&032&023&050\cr}\bye
    – morbusg
    Dec 20, 2012 at 12:33
  • Thank you very much for your answer!!! I believe that the last example is indeed the best looking and I am going to use it! Thank you very much for your help!!!
    – Thanos
    Dec 20, 2012 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .