# Table creation with multirow

I don't know what is wrong with this tabular code

\begin{tabular}{ |p{2cm}|p{3cm}|p{1cm}|p{1cm}| }
\centering
\hline
A & B & #Items & #Elements \\
\hline
\multirow{2}{4em}{MMM}     & AA  &  2 &  11 \\
& BB   &  1 &  26 \\
\hline
\caption{Name}
\label{n}
\end{tabular}


Which produces

The table width is more than one column which is why you see [4] and [5]. I used this code based on the example here.

How can I fix that?

• Does it help to (a) replace #Items and #Elements with \#Items and \#Elements, respectively, (b) execute \centering before rather than after \begin{tabular}, (c) execute \caption{Name} \label{n} after rather than before \end{tabular}, and (d) encase the entire code block in a table environment?
– Mico
Jun 23 at 13:42
• It changed but it is still buggy. See i.stack.imgur.com/iLHAJ.png Jun 23 at 13:47
• I wouldn't call the fact that the final column is not wide enough a "bug". Instead, it's a simple mistake on your part, of not having made the final column wide enough. I'd try doubling the column's width.
– Mico
Jun 23 at 14:36

You can't use \centering inside a table like that (or if you do it will only work locally). If you want the columns centered, you need to add that to the colspec.

The character # is special in LaTeX and must be escaped.

A caption and label cannot be inside a tabular. It should either be in a table environment wrapping the tabular, if you want a float, or else you can use the \captionof command from the \caption package.

The contents you have don't fit in the widths you've assigned to the columns. I've adjusted the widths of the last two columns, but if you have more content, you may need to do more adjusting.

You can use = as the second argument to multirow to get the natural width.

Either (will float):

\documentclass{article}
\usepackage{array,multirow}
\begin{document}
\begin{table}[htb]
\begin{tabular}{%
|>{\centering\arraybackslash}p{2cm}%
|>{\centering\arraybackslash}p{3cm}%
|>{\centering\arraybackslash}p{2cm}%
|>{\centering\arraybackslash}p{2cm}|}
\hline
A & B & \#Items & \#Elements \\
\hline
\multirow{2}={\centering MMM}     & AA  &  2 &  11 \\
\cline{2-4}% if you want a partial horizontal line here
& BB  &  1 &  26 \\
\hline
\end{tabular}
\caption{Name}\label{n}
\end{table}
\end{document}


Or (will not float):

\documentclass{article}
\usepackage{array,multirow}
\usepackage{caption}
\begin{document}
\begin{tabular}{%
|>{\centering\arraybackslash}p{2cm}%
|>{\centering\arraybackslash}p{3cm}%
|>{\centering\arraybackslash}p{2cm}%
|>{\centering\arraybackslash}p{2cm}|}
\hline
A & B & \#Items & \#Elements \\
\hline
\multirow{2}={\centering MMM}     & AA  &  2 &  11 \\
\cline{2-4}% if you want a partial horizontal line here
& BB  &  1 &  26 \\
\hline
\end{tabular}

\captionof{table}{Name}\label{n}

\end{document}


Is this what you would like?

     \documentclass{article}
\usepackage{array,multirow}
\usepackage{caption}
\begin{document}
\begin{center}
\begin{tabular}{ |p{2cm}|p{3cm}|p{1cm}|p{2cm}|  }
\hline
A & B & Items & Elements \\
\hline
\multirow{2}{4em}{MMM} & AA & 2 & 11 \\
& BB & 1 & 26 \\
\hline
\end{tabular}

\captionof{table}{Name}\label{n}

\end{center}
\end{document}


• Your code produces error: LaTeX Error: \caption outside float. Jun 23 at 14:34
• It seems to work for me. I must be doing something wrong but I can't see it as it renders what I wanted. Jun 23 at 14:47
• It's supposed to give you this error because \caption{} is not allowed outside environments such as table or figure etc. I checked your code on Overleaf and locally and the error is the same. Jun 23 at 14:53
• Your screenshot does not show the correct caption, that is Table 1: Name as it's supposed to, it only shows Name. It doesn't work! Jun 23 at 14:56
• Thank you for your comments, I was only trying to help. I think I have resolved the issue. Jun 23 at 15:32