[Tex/LaTex] LaTeX figures side by side

floatsgraphicspositioning

I want to place 2 images side by side in LaTeX. I have 2 .png files and I don't understand how to do it in LaTeX. I have tried many ways but could not get a good result.

Best Answer

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}

enter image description here

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.

The % (between \end{subfigure} and \begin{subfigure} or minipage) is really important; not suppressing it will cause a spurious blank space to be added, the total length will surpass \textwidth and the figures will end up not side-by-side.