6

I want to create table like this in Latex.

enter image description here

This is my script for create table.

\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|l|} \hline
\multirow{3}{*}{DEF}&\multicolumn{3}{|c|}{ ABC} \\ \hline
A&B&C\\ \hline
1& 4 & 7 \\ \hline
2& 5 & 8 \\ \hline
3& 6 & 9 \\ \hline
\end{tabular}
\end{table}

When I run this script it show error like this.

! Extra alignment tab has been changed to \cr.<template> \endtemplate ...row{3}{}{DEF}&\multicolumn{3}{|c|}{ ABC}

How to create table like this picture?

1
  • 1
    Hey, and welcome to tex.SX. Please provide compilable code (i.e. with \begin{document}. This will make it easier for us to help you
    – sheß
    Mar 6, 2019 at 17:51

3 Answers 3

3

You can decipher the following template.

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{array,multirow}

\let\mc=\multicolumn
\let\mr=\multirow
\let\cl=\cline

\begin{document}
\begin{tabular}{|*6{c|}l}
\cl{3-6}
\mc{2}{c|}{\mr{2}{*}{empty}} & \mc{4}{c|}{Primes} \\\cl{3-6}
\mc{2}{c|}{}                 & 2 & 3 & 5 & 7 \\\cl{1-6}
\mr{2}{*}{Powers}            & 504 & 3 & 2 & 0 & 1 \\\cl{2-6}
                             & 540 & 2 & 3 & 1 & 0 \\\cl{1-6}
\mr{2}{*}{Powers}            & HCF & 2 & 2 & 0 & 0 & min \\\cl{2-6}
                             & LCM & 3 & 3 & 1 & 1 & max \\\cl{1-6}
\end{tabular}
\end{document}

enter image description here

Warning

\mr can be nested in \mc but the reverse is not possible!

References

My code above was inspired by an example from Wikipedia as follows.

\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c  }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 &     \\ \cline{2-6}
\multicolumn{1}{ |c  }{}                        &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 &     \\ \cline{1-6}
\multicolumn{1}{ |c  }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c  }{}                        &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}

However, as you can see and compare, my code is much much simpler and well refactored yet still readable, right?

3

What you try to do can be done with this:

\documentclass{standalone}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c} \hline
& & \multicolumn{3}{|c|}{ ABC} \\ \hline
& & A&B&C\\ \hline
\multirow{3}{*}{DEF} & D &1& 4 & 7 \\ \cline{2-5}
&E&  2& 5 & 8 \\ \cline{2-5}
&F&  3& 6 & 9 \\ \cline{2-5}
\end{tabular}
\end{document}

But! LaTeX can make tables that are much nicer that this Excel-style layout. Consider doing something like this, which still nice to look at if you have more than 3 columns.

enter image description here

\documentclass{standalone}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lcccccc} 
\toprule
& \multicolumn{3}{c}{ ABC} & \multicolumn{3}{c}{ ABC} \\\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& A&B&C & A2&B2&C2\\ \midrule
DEF:\\
\;D &1& 4 & 7 &1& 4 & 7 \\ 
\;E&  2& 5 & 8 &1& 4 & 7 \\ 
\;F&  3& 6 & 9 &1& 4 & 7 \\ 
XYZ:\\
\;X &1& 4 & 7 &1& 4 & 7  \\ 
\;Y&  2& 5 & 8 &1& 4 & 7 \\ 
\;Z&  3& 6 & 9 &1& 4 & 7  \\ \bottomrule
\end{tabular}
\end{document}
2

\multirow command, needs to be act on its own column... So you need to add an extra column on the left side in each column and thus an extra & needed on each left side of every row of your table and an extra columntype (lets say c) at the beginning of your tabular's arguments.

Also, multicolumn in your example will should be 3 rows tall and should start from the row (1,4,7). But then an \hline on one of this or the next rows, would add a line in the "multirowed" cell too, and this is not wanted. So, we have to change some \hlines with \cline{2-4} that is a line from column 2 to column 4 and not from 1st to last.

Finally, you can add empty \multicolumn{1}{c|}{} as first element of your first two rows, in order to change their behavior from |c| to c| and remove this way the unwanted vertical line from their left side:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|c|l|} \cline{2-4}
\multicolumn{1}{c|}{}&\multicolumn{3}{|c|}{ ABC} \\ \cline{2-4}
\multicolumn{1}{c|}{}&A&B&C\\ \hline
\multirow{3}{*}{DEF}&1& 4 & 7 \\ \cline{2-4}
&2& 5 & 8 \\ \cline{2-4}
&3& 6 & 9 \\ \hline
\end{tabular}
\end{table}
 \end{document}

PS: Tried to show you what was your mistakes and not to find a way to show your table more beautiful.

enter image description here

You must log in to answer this question.

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