17

I am using following code to import two images next to each other. When \caption{} is used, LaTeX gives me error caption outside float.

\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig1.eps}
\caption{text for fig1}
\label{fig:fig1}            
\end{minipage}
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig2.eps}
\caption{text for fig2}
\label{fig:fig2}            
\end{minipage}

When I remove the \caption from both places, the code compiles correctly.

\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig1.eps}
\label{fig:fig1}            
\end{minipage}
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig2.eps}
\label{fig:fig2}            
\end{minipage}

But both the codes do not generate any label naming the figure. (something like Figure 21).

Am I missing anything?

2 Answers2

30

To place a caption outside a floating environment, you can use \captionof from the capt-of or caption packages::

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}

\noindent\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig1.eps}
\captionof{figure}{Some figure}
\label{fig:fig1}            
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{fig2.eps}
\captionof{figure}{Some figure}
\label{fig:fig2}            
\end{minipage}

\end{document}

Notice the \noindent and % character I added to prevent undesired spacing.

Marco Daniel
  • 95,681
Gonzalo Medina
  • 505,128
3

You can also wrap a figure environment around your minipage, that way you do not need to use captionof or capt-of:

\begin{figure}[!htbp]
    \begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Images/img1.JPG}
        \caption{cap1}
        \label{fig:img1}
    \end{minipage}\hfill
    \begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Images/img2}
        \caption{cap2}
        \label{fig:img2}
    \end{minipage}
\end{figure}