Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
add comment

1 Answer

up vote 11 down vote accepted

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.

share|improve this answer
    
The command captionof is provided by every KOMA class, the package capt-of and the package caption –  Marco Daniel May 26 '12 at 14:03
    
so I have to add a package using \usepackage{caption}? because as it is, it is giving undefined control sequence error. –  maths-help-seeker May 26 '12 at 14:07
    
@maths-help-seeker yes, either caption (as I did in my example) or capt-of . –  Gonzalo Medina May 26 '12 at 14:08
1  
@maths-help-seeker: Yes, but you can only do that in the document preamble. See Documents with typical LaTeX errors. –  Werner May 26 '12 at 14:09
    
ok thanks! it worked. It is also now showing the labels. :) –  maths-help-seeker May 26 '12 at 14:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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