# Aligning Subfigures in Latex

Trying to get 2 figures, with captions under each one but only counting as a single figure

\begin{figure}
\begin{subfigure}{0.31\textwidth}
\includegraphics[scale=0.25]{B2B1}
\caption{First subfigure} \label{fig:1a}
\end{subfigure}
\hspace*{\fill}
\begin{subfigure}{0.31\textwidth}
\includegraphics[scale=0.25]{B2B2}
\caption{Second subfigure} \label{fig:1b}
\end{subfigure}
\caption{A figure that contains three subfigures} \label{fig:1}
\end{figure}


However it keeps coming out on the next level (it says 3 subfigures but it will only be 2)

I understand the following list of packages od theorems and packages is long

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\usepackage{amsthm}
\usepackage{float}
\usepackage[round]{natbib}
\usepackage{lastpage}
\usepackage{indentfirst}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{upgreek}
\usepackage{amssymb}
\usepackage[margin=0.9in, top=5.3em, bottom=5.3em]{geometry}
\usepackage{epstopdf}
\usepackage{enumerate}
\usepackage{mathtools, chngcntr}
\usepackage{url}
\usepackage{subfig}
\usepackage{subcaption}
\counterwithin{figure}{section}
\theoremstyle{definition}
\newtheorem{definition}[figure]{Definition}
\newtheorem{theorem}[figure]{Theorem}
\newtheorem{corollary}[figure]{Corollary}
\newtheorem{lemma}[figure]{Lemma}
\newtheorem{algorithm}[figure]{Algorithm}
\newtheorem{example}[figure]{Example}
\renewcommand{\footrulewidth}{1pt}
\newcommand{\R}{\mathbb{R}}
\renewcommand{\labelenumii}{\roman{enumii}}

• Please provide a complete example. Multiple packages define subfigure or can define it, and some of them interconnect if loaded with particular options or in specific combinations. Moreover, you class may also affect this. Basically, anybody who tries to help has to guess what you might have done. Essentially, whistle in the dark. Mysteries can be fun, but this one probably won't be. – cfr Mar 14 '17 at 2:52
• To put this another way: your code is absolutely correct, if the rest of your document is a certain way, but how on earth would you expect it to work, if it is a second certain way, and this is only to be expected, if it is a third certain way. Space does not permit a description of the fourth and further possibilities. – cfr Mar 14 '17 at 2:55
• Would the appropriate thing be now too show you all packages i am currently using? @cfr – Sam Harper Mar 14 '17 at 3:16
• Make the smallest document you can which demonstrates the problem and post the code. – cfr Mar 14 '17 at 3:49
• Don't use subfig and subcaption. – cfr Mar 14 '17 at 3:50

For two independent side-by-side figures, you can use two minipages inside a figure enviroment; for two subfigures, I would recommend the subcaption package with its subfigure environment; here's an example showing both approaches:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image1}
\caption{A subfigure}
\label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image1}
\caption{A subfigure}
\label{fig:sub2}
\end{subfigure}
\caption{A figure with two subfigures}
\label{fig:test}
\end{figure}

\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image1}
\captionof{figure}{A figure}
\label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image1}
\captionof{figure}{Another figure}
\label{fig:test2}
\end{minipage}
\end{figure}

\end{document}


The demo option for graphicx was used only to make my example compilable for everyone; you shouldn't use that option in your actual code.