[Tex/LaTex] Subfigure moves figures on the top of page

subfloats

I tried to put two figures in one row and I found that subfigure package could help me.
But code:

\documentclass[11pt, a4paper, draft]{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}
\section{Example }
Lorem ipsum

\begin{figure}
        \centering
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{./2pol-hist}
                \caption{A gull}
        \end{subfigure}

        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{./2pol-hist}
                \caption{A tiger}
        \end{subfigure}
        \caption{Pictures}
\end{figure}

\end{document}

reorganize my document, placing figures on the top of page. Why figures aren't below the section, as I specified in latex code?

Best Answer

Try this:

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

\begin{document}
\section{Example}
Lorem ipsum

\begin{figure}[hbp]
        \centering
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{./2pol-hist}
                \caption{A gull}
        \end{subfigure}\hfill%
        \begin{subfigure}[b]{0.3\textwidth}
                \centering
                \includegraphics[width=\textwidth]{./2pol-hist}
                \caption{A tiger}
        \end{subfigure}
        \caption{Pictures}
\end{figure}

\end{document}

enter image description here

You had a blank line between the subfigure environment so the second environment was starting a new paragraph; I removed the spurious blank line and replaced it with an \hfill to get some spacing between the subfigures (not in this particular case, but you also need to be careful with possible spurious blank spaces). I also used [hbp] as placement specifier for the figure environment; this (in this case) prevents the floating object to appear before the section title and tries to place it where it was declared in the code or in the bottom of the page or in a float page (a dedicated page containing only floats).

The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

Using the draft class option your actual figures won't be included (you'll only see a rectangle and the name of the files); I suppressed that option from my example.