4

This question already has an answer here:

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 ?

marked as duplicate by gernot, Stefan Pinnow, ebosi, user13907, CroCo Jan 28 '17 at 10:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Welcome to TeX.SE! \caption had to be inside figure float environment: \begin{figure} \includegraphics{...}\caption{...}\label{...}\end{figure} – Zarko Jan 28 '17 at 8:30
  • 1
    If you want to have a table, figure, picture with a numbered caption that stays at a fixed place, use the caption package. See this answer for an explanation. If you are happy to use the figure or table numbering already provided by LaTeX, then \captionof{figure}{This is the caption.} does the job (having included \usepackage{caption} into the preamble). – gernot Jan 28 '17 at 8:38
2

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}

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