[Tex/LaTex] captions not appearing below subfigure

captionssubcaptionsubfloats

Code:

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

\begin{document}
\begin{figure}[H]
    \centering
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=6cm]{initial_normal.eps}
        \caption{Normal Distribution}
        \label{fig1a}
    \end{subfigure}%
    \begin{subfigure}{0.5\textwidth}[H]
        \includegraphics[width=6cm]{initial_uniform.eps}
        \caption{Uniform Distribution}
        \label{fig1b}
    \end{subfigure}
\caption{distributions}
\end{figure}
\end{document}

The captions should come below the figure but I am getting them to the right of the figures and the caption text is aligned centrally with respect to the page instead of the subfigure.
Am I making a mistake here?

Best Answer

You should not put [H] after \begin{subfigure}{\textwidth}[H] Further, these points may be noted:

  1. Don't put a blank line in between the subfigures.
  2. The sum of widths of two subfigure should not exceed \textwidth
  3. Put a % after the first subfigure if you specify a width of 0.5\textwidth for both subfigures. If the sum is less than \textwidth, you may put a \hfill in between.

Now your code:

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

\begin{document}
\begin{figure}[H]
%    \centering
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=6cm]{initial_normal.eps}
        \caption{Normal Distribution}
        \label{fig1a}
    \end{subfigure}%   %% This % is needed when you use 0.5\textwidth
    %  Don't leave the blank line
    \begin{subfigure}{0.5\textwidth}%[H]   %% Don't put this here
        \centering
        \includegraphics[width=6cm]{initial_uniform.eps}
        \caption{Uniform Distribution}
        \label{fig1b}
    \end{subfigure}
\end{figure}
\end{document}

enter image description here

May be the subcaption package manual is useful here. You can access it by typing texdoc subcaption from command line/prompt. The correct usage for subfigure is

\begin{subfigure}[<pos>]{<width>}

... content like \includegraphics and caption

\end{subfigure}

Here <pos> is t or b c (default). Both [<pos>]{<width>} are same as that for a minipage.