[Tex/LaTex] Missing \endcsname inserted when trying to make subtables

captionserrorstables

I'm trying to make 2 subtables with 2 different captions inside of a table, but I keep getting the following error:

Missing \endcsname inserted.
<to be read again>
                   \relax
l.9 \begin
          {tabular}{| r | r | r | r |}
?

Here is the code:

\documentclass [12pt,letterpaper]{report}
\usepackage{subcaption}
\begin{document}

\begin{table}[htbp]
\begin{center}

\begin{subtable}[htbp]
\begin{tabular}{| r | r | r | r |}
\hline
    A & B & C & D \\ \hline
    1 & 2 & 3 & 4 \\ \hline
\end{tabular}
\caption{Caption A}
\end{subtable}

\begin{subtable}[htbp]
\begin{tabular}{| r | r | r | r |}
\hline
    A & B & C & D \\ \hline
    1 & 2 & 3 & 4 \\ \hline
\end{tabular}
\caption{Caption B}
\end{subtable}

\caption{Caption of both A and B}
\end{center}
\end{table}

\end{document}

Stuff I read on google said that this error is usually caused by an extra \ in an environment name, but I double checked and can't find any examples of this. Any solutions must work for the report document class.

Best Answer

The subtable environment doesn't take a positioning optional argument like table, but wants instead a mandatory argument stating the width of the subtable.

You probably want something like

\documentclass [12pt,letterpaper]{report}
\usepackage{subcaption}
\begin{document}

\begin{table}[htbp]
\centering

\begin{subtable}{.3\textwidth}
\centering

\begin{tabular}{| r | r | r | r |}
\hline
    A & B & C & D \\ \hline
    1 & 2 & 3 & 4 \\ \hline
\end{tabular}
\caption{Caption A}
\end{subtable}\hfil
\begin{subtable}{.3\textwidth}
\centering
\begin{tabular}{| r | r | r | r |}
\hline
    A & B & C & D \\ \hline
    1 & 2 & 3 & 4 \\ \hline
\end{tabular}
\caption{Caption B}
\end{subtable}

\caption{Caption of both A and B}
\end{table}

\end{document}

enter image description here

Note that you should use \centering and not the center environment.