3

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?

1 Answer 1

7

center is not a floating environment, hence the error. You need the figure environment instead, i.e.

\begin{figure}
\centering
\includegraphics{..}
\caption{..}
\label{..}
\end{figure}

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.

\begin{figure}
Lorem ipsum dolor sit amet, etc., etc.

\centering
\includegraphics{..}
\caption{..}
\label{..}
\end{figure}

If you don't need the image to float, you can continue using center instead of figure, 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?

\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{center}
\includegraphics{..}
\captionof{figure}{...\label{..}}
\end{center}
\end{document}

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 a minipage instead of center, e.g.

\documentclass{article}
\usepackage{caption}
\begin{document}
\noindent\begin{minipage}{\textwidth}
\centering
\includegraphics{..}
\captionof{figure}{...\label{..}}
\end{minipage}
\end{document}

The \noindent is to remove the standard paragraph indentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .