-1

I'm trying to create a table that each cell is a table of 4 mini-cells (Presented in the figure). I have tried to copy the code of a table into the cell. However, there is a compilation error.

How can I do this?

Thanks, Noam.

Example

NM2
  • 107

4 Answers4

3

enter image description here

\documentclass{article}
\usepackage{array, multirow}
\setlength\extrarowheight{2pt}

\begin{document}
by merging columns

\begin{tabular}{|c| c|c | c|c |}
    \hline
                    &   \multicolumn{2}{c|}{A}   
                            &   \multicolumn{2}{c|}{B}   \\
    \hline
\multirow{2}{*}{1}  & 1 & 2 & 1 & 2 \\    \cline{2-5} 
                    & 3 & 4 & 3 & 4 \\
    \hline
\end{tabular}

\medskip
by inserting tables

\begin{tabular}{|c|  @{}c @{}  |  @{}c@{} |}
    \hline
                    &   A   &   B   \\
    \hline
\multirow{1.3}{*}{1}  &   \begin{tabular}{l|l}
                            1 & 2 \\    \hline
                            3 & 4 \\
                        \end{tabular}
                            &   \begin{tabular}{l|l}
                                    1 & 2 \\    \hline
                                    3 & 4 \\
                                \end{tabular}   \\
    \hline
\end{tabular}
\end{document}
Zarko
  • 296,517
2

What compilation error are you getting? It compiles for me. Just nest your tabular environments, remembering that each is a cell and separate them by & with \\ at the end of each line:

\documentclass{article}
\begin{document}
\begin{tabular}{ll}
    \hline
    \begin{tabular}{ll}
        1 & 2 \\
        3 & 4 \\
    \end{tabular} &
    \begin{tabular}{ll}
        1 & 2 \\
        3 & 4 \\
    \end{tabular} \\
    \hline
    \begin{tabular}{ll}
        1 & 2 \\
        3 & 4 \\
    \end{tabular} &
    \begin{tabular}{ll}
        1 & 2 \\
        3 & 4 \\
    \end{tabular} \\
    \hline
\end{tabular}
\end{document}

I think the correct solution, however, isn't nesting tables, but remembering that you've really got four times as many cells as you think you do; so use \multirow and \multicolumn:

\documentclass{article}
\usepackage{multirow}
\thispagestyle{empty}
\begin{document}
\begin{tabular}{lllll}
    \hline
    {} & \multicolumn{2}{c}{First Col.} & \multicolumn{2}{c}{Second Col.} \\
    \multirow{2}{*}{1st Column} & 1 & 2 & 1 & 2 \\
    {} & 3 & 4 & 3 & 4 \\
    \hline
    {} & \multicolumn{2}{c}{First Col.} & \multicolumn{2}{c}{Second Col.} \\
    \multirow{2}{*}{1st Column} & 1 & 2 & 1 & 2 \\
    {} & 3 & 4 & 3 & 4 \\
    \hline
\end{tabular}
\end{document}

This second one will give you this:

Results

Which one of these better represents your actual data will, of course, depend on your data.

Hope that helps.

dgoodmaniii
  • 4,270
  • 1
  • 19
  • 36
1

I have two examples, which demonstrate two different approaches to meet your requests. Example 1 defines one wide and twelve narrow columns, row 1 twice as high as row 2 to 5. The height of the cells in column 1 (except the first one) is just the an illusion created by the shorter rule.

In example 2, I have defined seven columns and three rows of equal width and height. The four mini cells in row 2 and 3, column 2 to 13 are created by nested tabulars. The nested tabulars have two columns and two rows, of equal width and height.

Example 1

I start by loading the package array, and define three new column types - D(ouble) - S(ingle) – Z(ero) width

The wide column (D) is 1/7 line width – but we have to take into consideration the width of the column separator (tabcolsep) and width of the rules.

The narrow columns are 1/14 line width since you have 12 narrow columns in addition to one of double wide. That adds up to 14. We have to correct for tabcolseps and the width of all the vertical rules to avoid overfull boxes.

The width of the last column is 0 pt – and I remove the column separator to the right (the ‘side bearing’). We inject a zero width rule as high as the width of the narrow cells into each cell in this column. This will force the cells to be squares, forming the four mini cells.

I define a new command \hd to format the table headings. The headings span two columns using the \multicolumn-command. This is just a short hand to reduce typing and clutter.

In the first row, we set a zero-width rules as high as the width of the wides column (`D). That will force width and height of the the cells in the heading row be to equal, but push the text to the bottom of the cell. This is typographically correct, but if this is not what you want, look at example 2 below.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{array}

% Define three column types - D(ouble) - S(ingle) - Z(ero) width
\newcolumntype{D}{wc{\dimexpr(\linewidth/7)-(2\tabcolsep+2\arrayrulewidth)}}
% Wide column is 1/7 linewidth - minus the distance between the column and width of the rules
\newcolumntype{S}{wc{\dimexpr(\linewidth/14)-(2\tabcolsep+2\arrayrulewidth)}}
% Narrow column is 1/14 linewidth - minus the distance between the column and width of the rules
\newcolumntype{Z}{@{}>{\rule{0pt}{\dimexpr((\linewidth/14)-\tabcolsep)}}wc{0pt}@{}}
% The last column is 0 pt width - and the side bearing has to be removed. We induce a zero width rule into all cells, and the rule is as high as the width of the cell.

% Defining a command to be used as Table Heading
\newcommand\hd[1]{%
\multicolumn{2}{D|}{\Large\bfseries#1}}


\begin{document}

\sffamily
\noindent\begin{tabular}{|>{\Large\bfseries}D*{12}{|S}|Z} 
\hline
&\hd{A}
&\hd{B}
&\hd{C}
&\hd{D}
&\hd{E}
&\hd{F}
&\rule{0pt}{\dimexpr((\linewidth/7)-\tabcolsep)}\\  % Force heading row to be as high as its width
\hline
&1&2&&&&&&&&&&&\\
\cline{2-13}
1&3&4&&&&&&&&&&&\\
\hline
&2&3&&&&&&&&&&&\\
\cline{2-13}
2&1&4&&&&&&&&&&&\\
\hline\end{tabular}

\end{document}

Example 2

Here, I define all cells to be of double width and height. In rows 2 and 3, columns 2 to 6, I nest small tabulars, 2x2, to create the four mini cells.

Instead of injecting zero width rules in a zero width column, I define two commands DD and ss, which do the same using \vphantom-commands. However, those I use in the beginning of each rows, also in the nested tabulars. The code is adapted from Yiannis Lazarides’ answer to another question.

I define command \hd to format the text in the heading row. Also, I define a shorthand for the nested tabulars. It reduces typing and clutter.

As you will see, the text in the tabular is both vertically and horizontally aligned.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{array}

\newcolumntype{D}{wc{\dimexpr(\linewidth/7)-(2\tabcolsep+2\arrayrulewidth)}}
\newcolumntype{S}{wc{\dimexpr(\linewidth/14)-(2\tabcolsep+2\arrayrulewidth)}}

\def\DD{\vphantom{\parbox[c][\dimexpr(\linewidth/7)]{10pt}{-}}}
\def\ss{\vphantom{\parbox[c][\dimexpr(\linewidth/14)]{10pt}{-}}}

\newcommand\hd[1]{\Large\bfseries#1}
\newcommand\ntab[4]{%
\multicolumn{1}{D|}{\centering\begin{tabular}{S|S}{\ss#1}&{#2}\\\hline\ss#3&#4\end{tabular}}}

\begin{document}
\sffamily

\noindent\begin{tabular}{|>{\Large\bfseries}D*{6}{|D}|} 
\hline
\DD
&\hd{A}
&\hd{y}
&\hd{C}
&\hd{g}
&\hd{E}
&\hd{m}\\

\hline
1
&\ntab{a}{ffj}{fj}{K}
&\ntab{1}{2}{4}{3}
&\ntab{4}{1}{3}{2}
&\ntab{3}{4}{2}{1}
&\ntab{2}{3}{1}{4}
&\ntab{1}{2}{4}{3}\\
\hline
2
&\ntab{1}{2}{4}{3}
&\ntab{4}{1}{2}{3}
&\ntab{3}{4}{2}{1}
&\ntab{2}{3}{1}{4}
&\ntab{1}{2}{4}{3}
&\ntab{4}{1}{3}{2}\\\hline
\end{tabular}

\end{document}
Sveinung
  • 20,355
0

I have left the text out as an exercise

Please see if it will suffice

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{multirow}
\begin{document}

\begin{tabular}{|*7{p{15mm}|}}
\hline
                       &  \multicolumn{2}{c|}{A}    &&&&        \\ 
\hline
                       &                         &  &&&&        \\ \cline{2-7}
                       &                         &  &&&&        \\
\hline

\end{tabular}
\end{document}

enter image description here

js bibra
  • 21,280