[Tex/LaTex] Caption out of float error when using caption

captionserrorsgraphics

I'm adding captions for pictures but it shows this error :

\caption outside float. }
My code is like this

\begin{center}
    \href{http://m.technologijos.lt/cat/7994/article/S-18917}{
        \includegraphics[scale=1]{2}\\
        \caption{Pav. 2}
    }
\end{center}

What is wrong here and what i should fix ?

Best Answer

You have two issues with your code:

  • inside \href can not be nested \caption
  • outside of figure \caption{...} cannot be used. Instead of it you can use \captionof{figure}{...}. For this you need to use package caption or capt-of.

Correct code for both cases:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{hyperref}

\begin{document}

\begin{center}
    \href{http://m.technologijos.lt/cat/7994/article/S-18917}{
        \includegraphics[scale=1]{2}
    }
    \captionof{figure}{Pav. 2}
\end{center}

\begin{figure}[htb]
\centering
    \href{http://m.technologijos.lt/cat/7994/article/S-18917}{
        \includegraphics[scale=0.5]{2}
        }
        \caption{Pav. 2}
\end{figure}
\end{document}