[Tex/LaTex] Control errors with subcaption subfigure

captionssubcaptionsubfloats

Code:

\documentclass[fleqn,11pt,a4paper]{article}
\usepackage{geometry,array,graphicx,float,caption}
\usepackage{subcaption}

\begin{document}
\begin{figure}[H]
    \centering
    \begin{subfigure}
        \includegraphics[width=5cm]{initial_uniform.eps}
        \caption{Uniform Distribution}
        \label{fig1a}
    \end{subfigure}
\end{figure}

I have the caption package installed and subcaption.sty is detected. However, when I use subfigure I am getting these errors:

! Missing number, treated as zero.
<to be read again> 
                   \let 
l.72        \includegraphics
                       [width=5cm]{initial_uniform.eps}

! Illegal unit of measure (pt inserted).
<to be read again> 
                   \let 
l.72        \includegraphics
                       [width=5cm]{initial_uniform.eps}

and more control errors.

When I comment out the lines for begin and end subfigure i.e use just normal figure, everything works just fine. I can't understand what is wrong here.

Best Answer

Your usage of subfigure is missing the width. Use

\begin{subfigure}{0.5\textwidth}

Second mandatory argument is the width of the sub figure.

Your code with this correction is

\documentclass[fleqn,11pt,a4paper,demo]{article}   %% remove demo
\usepackage{geometry,array,graphicx,float,caption}
\usepackage{subcaption}

\begin{document}
\begin{figure}[H]
    \centering
    \begin{subfigure}{0.5\textwidth}
        %\centering  %% may be you need this
        \includegraphics[width=5cm]{initial_uniform.eps}
        \caption{Uniform Distribution}
        \label{fig1a}
    \end{subfigure}
\end{figure}
\end{document}

enter image description here