[Tex/LaTex] Why can’t I put a caption in this table

captions

I don't understand why I can't have a caption here:

\begin{table}[H]
\begin{tabular}{cc}
\hline
\textbf{Ejemplo 3} &  \includegraphics[width=8cm,height=4cm]{figura/07.jpg} \\
\includegraphics[width=6cm,height=4cm]{figura/07_1.jpg} & \includegraphics[width=6cm,height=4cm]{figura/07_2.JPG} \\
\hline 
\textbf{Foto 7} & \includegraphics[width=8cm,height=4cm]{figura/48.jpg}\\
\includegraphics[width=6cm,height=4cm]{figura/48_1.jpg} & \includegraphics[width=6cm,height=4cm]{figura/48_2.JPG}\\
\hline
\label{casosop2}
\end{table}
\caption{caption}
\end{tabular}

I get the following error:

! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.84 \end{table}

If I remove the caption line, I get no problems.

Best Answer

A couple of things:

  • Grouping follows a hierarchy: If you open up a table and inside it a tabular, you have to close the tabular before closing the table.

  • Your \caption should be inside the table environment, not inside the tabular. That's just how it is, unless you're using longtable.

  • Place your \label after the \caption (and not inside the tabular). Here's what generates your table:

enter image description here

\documentclass{article}

\usepackage{graphicx,float}

\begin{document}

\begin{table}[H]
  \begin{tabular}{cc}
    \hline
    \textbf{Ejemplo 3} &  \includegraphics[width=8cm,height=4cm]{example-image-a} \\
    \includegraphics[width=6cm,height=4cm]{example-image-b} & \includegraphics[width=6cm,height=4cm]{example-image-c} \\
    \hline
    \textbf{Foto 7} & \includegraphics[width=8cm,height=4cm]{example-image-a}\\
    \includegraphics[width=6cm,height=4cm]{example-image-b} & \includegraphics[width=6cm,height=4cm]{example-image-c}\\
    \hline
  \end{tabular}
  \caption{caption}
  \label{casosop2}
\end{table}

\end{document}