1

I am trying to insert two images side-by-side in LaTex, but instead of being side-by-side, they end up being on top of each other.

I tried many ways, but none of them seem to work. The code is below:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\graphicspath{{./images/}}


\title{Inserting Images Latex}
\author{Lorem Ipsum}
\date{February 2020}



\begin{document}

\maketitle

A demonstration of how to have two figures side by side.

\bigskip


\begin{figure}
    \centering

    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{universe} % first figure itself
        \caption{Universe}
    \end{minipage}\hfill

    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{figure} % second figure itself
        \caption{Bar Plot}
    \end{minipage}

\end{figure}




\begin{figure}[h]
    \includegraphics[width=.24\textwidth]{universe}\hfill
    \caption{Universe}
    \includegraphics[width=.24\textwidth]{figure}\hfill
    \caption{Bar Plot}
   %  \includegraphics[width=.24\textwidth]{example-image}\hfill
   %  \includegraphics[width=.24\textwidth]{example-image}
   %  \\[\smallskipamount]

\end{figure}



\begin{figure}[h]
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{universe}
         \caption{Universe}
         \label{fig:1}
    \end{subfigure}


     \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{figure}
         \caption{Bar Plot}
         \label{fig:2}
    \end{subfigure}
\end{figure}




\begin{figure}[h]
  \begin{minipage}[b]{0.4\textwidth}
    \includegraphics[width=\textwidth]{universe}
    \caption{Universe}
    \label{fig:1}
  \end{minipage}

  \begin{minipage}[b]{0.4\textwidth}
   \includegraphics[width=\textwidth]{figure}
    \caption{Bar Plot}
    \label{fig:2}
  \end{minipage}
\end{figure}


This is how you have multiple images in one figure.

\end{document}

What's going wrong?

Thanks

  • 3
    Comment out or remove the empty line between \end{minipage}\hfill and \begin{minipage}{0.45\textwidth}. The same will also work with the subfigure environment. Which of them to use depends on the numbering style you would like to have. – leandriis Feb 29 at 8:47
2

As a comment has already pointed out, in your first example, the line break is caused by the multiple new lines (multiple line breaks create a new paragraph in LaTeX). It can simply be fixed by either commenting out the line between the minipages or removing the line altogether:

\begin{figure}
    \centering
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{universe} % first figure itself
        \caption{Universe}
    \end{minipage}\hfill
    % Remove or comment out this line
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{figure} % second figure itself
        \caption{Bar Plot}
    \end{minipage}
\end{figure}

Result: enter image description here The same goes for the rest of your examples, except the one where you do not use minipages nor subfigures.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.