[Tex/LaTex] Problem with using TikZ pictures as subfigures

errorssubfloats

I tried to create a figure which consists of two subfigures, each of which is a TikZ picture.

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath,amssymb,amsfonts,amsthm,tikz,caption,subcaption}

\begin{document}

\begin{figure}
\begin{center}

\begin{subfigure}
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P1} \label{fig:M1}
\end{subfigure}

\begin{subfigure}
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P2} \label{fig:M2}
\end{subfigure}


\end{center}
\end{figure}

\end{document}

But compiling this file gives the error

! Missing \endcsname inserted.
<to be read again> 
                   \relax 
l.10 \begin
           {tikzpicture}[scale=1]

Why is that?

Best Answer

The subfigure environment has a mandatory argument specifying the width:

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath,amssymb,amsfonts,amsthm,tikz,caption,subcaption}

\begin{document}

\begin{figure}
\begin{subfigure}{.5\linewidth}
\centering
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P1} \label{fig:M1}
\end{subfigure}%
\begin{subfigure}{.5\linewidth}
\centering
\begin{tikzpicture}[scale=1]

  \path (0,0) coordinate (P0) node[right=0.1cm] {P0};
  \fill (P0) circle (2pt);

\end{tikzpicture}
\caption{Hello P2} \label{fig:M2}
\end{subfigure}
\end{figure}

\end{document}

enter image description here

I used .5\linewidth for each subfigure so each one will occupy half the available horizontal space and the two will appear side-by-side; of course, you can adjust those settings according to your needs. I also used \centering to center the objects, since the center environment adds (usually unnecessary) vertical spacing inside a float.