0

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

enter image description here

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?

3
  • 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
    – mahmood
    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

2 Answers 2

3

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}

table example

1

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}

 
8
  • 1
    Your code produces error: LaTeX Error: \caption outside float.
    – Celdor
    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.
    – Paul A
    Jun 23 at 14:47
  • 1
    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.
    – Celdor
    Jun 23 at 14:53
  • 1
    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!
    – Celdor
    Jun 23 at 14:56
  • Thank you for your comments, I was only trying to help. I think I have resolved the issue.
    – Paul A
    Jun 23 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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