I'm using the following packages on Overleaf:
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
When I try adding a caption there's a compilation error (caption outside float). Here's what I'm doing:
\begin{center}
\label {tab1}
\includegraphics[scale=0.3]{fig.PNG} \caption{cap_text}
\end{center}
How can I fix that problem?
By the way I need two texts, one above, one under the figure. Should I just add \caption{text}
before and after it?
Best Answer
center
is not a floating environment, hence the error. You need thefigure
environment instead, i.e.I'm assuming only one of the texts need to be part of the numbered caption, for the other just add some text in the appropriate location, e.g.
If you don't need the image to float, you can continue using
center
instead offigure
, but add\usepackage{caption}
to the preamble, and use\captionof{figure}{caption text \label{...}}
instead of\caption
. Note that the\label
has to be placed after or within the\caption
, otherwise cross-references wont work, see e.g. Why does an environment's label have to appear after the caption?As pagebreaks are allowed inside a
center
environment, you could here end up with image and caption on separate pages, which is probably not wanted. An alternative is to use aminipage
instead ofcenter
, e.g.The
\noindent
is to remove the standard paragraph indentation.