[Tex/LaTex] Putting Figures Side-By-Side Using Minipage

graphicshorizontal alignmentminipage

I am trying to put two figures next to each other. However, it is not working. Here is my code:

\begin{figure}[h]
\centering

\begin{minipage}{1.0\textwidth}
\centering
\includegraphics[width=0.3\linewidth, height=0.15\textheight]{prob1_6_2}
\caption{$dt=0.1$}
\label{fig:prob1_6_2}
\end{minipage}

\begin{minipage}{1.0\textwidth}
\centering
\includegraphics[width=0.3\linewidth, height=0.15\textheight]{prob1_6_1}
\caption{$dt =$}
\label{fig:prob1_6_1}
\end{minipage}

\end{figure}

In my preamble I have \usepackage{minipage}. But it hasn't worked. And yes, I have searched through other posts.

Best Answer

Blank lines tell LaTeX to start a new paragraph, which is one of the reasons that your code puts the figures on their own lines; the other reason is that the sum of the width of your two minipages is 2\textwidth, which is wider than \textwidth.

There are a few things that we can do to fix this:

  • remove the blank line between the first \end{minipage} and the second \begin{minipage}...
  • change the width argument of each minipage so that the combined width is less than or equal to \textwidth; of course, if you'd prefer to overflow the page margins, then you can crank it up higher
  • add a % after the first \end{minipage} which removes the little bit of horizontal space that is automatically inserted by the minipage environment.

Referring to this line of your code:

\includegraphics[width=0.3\linewidth, height=0.15\textheight]{prob1_6_2}

This will refer to the current linewidth, i.e, the width of the current minipage in which it resides. If we make each minipage have a width of .5\textwidth, then the width of this graphic will actually be .15\textwidth wide. Adjust as you see fit- typically, I specify the width of the minipage, and then prefer to use \includegraphics[width=\textwidth...

Finally, there are some concerns about using \begin{figure}[h]. Float placement has been discussed in great detail at the following links (in order of recommended reading- the last one is epic):

Here is a complete (modified) version of your code that implements the changes described above.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage[demo]{graphicx}

\begin{document}

\begin{figure}[!htb]
    \centering
    \begin{minipage}{.5\textwidth}
        \centering
        \includegraphics[width=0.3\linewidth, height=0.15\textheight]{prob1_6_2}
        \caption{$dt=0.1$}
        \label{fig:prob1_6_2}
    \end{minipage}%
    \begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics[width=0.3\linewidth, height=0.15\textheight]{prob1_6_1}
        \caption{$dt =$}
        \label{fig:prob1_6_1}
    \end{minipage}
\end{figure}
\end{document}

For further options about putting figures side by side, have a look at LaTeX figures side by side and the links within- there are a lot of options available.

My final comment is to consider using descriptive labels- using \label{fig:prob1_6_1} is ok, but it doesn't tell you much about what the figure actually shows you. This may not matter when you're writing the document and you are intimately familiar with it, but if you come back to it at a later time, you might wish for more details; for example, if the figure shows solution to a Bernoulli equation , you might label it fig:bernoulli.