Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
How should the column appear? –  egreg Dec 20 '12 at 7:45

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
For the last example you don't need \multirow at all; each column can be a nested tabular. –  egreg Dec 20 '12 at 8:48
    
@egreg That is true. I’ve updated my answer with that option. Thank you. –  Qrrbrbirlbel Dec 20 '12 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 '12 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 '12 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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