2

I'm using this code for pictures, because \begin{figure} puts it in a bad place for me. How can I add a caption to this picture?

\begin{center}
\includegraphics[width=13.7cm, height=8.2cm]{pictures/c25k.png}
\end{center}
1
  • Load the caption package (if you're not using a KOMA-Script class) and use \captionof{figure}{...}
    – cgnieder
    Nov 29, 2015 at 11:36

3 Answers 3

1

To solve this problem you can use something like this:

\begin{center}
\includegraphics[width=13.7cm, height=8.2cm]{pictures/c25k.png}
\begin{figure}[!h]
\caption{Here the caption.}
\end{figure}
\end{center}

This may works for what you are asking. But remind that if you want a figure in a specific place you can use the following command:

\begin{figure}[!h]

In the case, when compiling, the figure move in a different place/position this mean in the document there was not enough space for place the figure (this probably will happen also with the solution I proposed to you).

3
  • 2
    This solution doesn't seem suitable in general...
    – Werner
    Nov 28, 2015 at 20:45
  • Why nesting center and figure environments? Also, why \includegraphics outside figure environment? Nov 29, 2015 at 9:10
  • 1
    @MattAllegro, Werner, yes you both are right. This solution is not suitable in general. But the question asked a particular solution for a particular case.
    – rebatoma
    Nov 29, 2015 at 9:48
8

To force a figure to the current position, add

\usepackage{float}

to your preamble and use

\begin{figure}[H]
  \centering
  \includegraphics[<options>]{<image>}
  \caption{<caption>}
\end{figure}

If this is your choice, please consider also reading How to influence the position of float environments like figure and table in LaTeX?


Another option would be to use the caption package which provides \captionof{<float type>} (the tiny capt-of package provides a similar interface):

\begin{center}
  \includegraphics[<options>]{<image>}
  \captionof{figure}{<caption>}
\end{center}
2

I'll try to make the point of some things here, also because I don't agree with the approach suggested in the answer posted by @Mattia. There is a bit of confusion here about some heavy matters: positioning figures, floats, captions.

First, the center environment is mainly meant to be used to center text paragraphs rather than images, figure should be used here instead.

Further, to nest center and figure is definitely overmuch. Better to use \centering command, inside a figure environment. Nesting these two environments may result in excessive vertical margins around the figure and - I think - makes the code overabundant.

Ah, figure environment makes the image a float, hence the use of something like [h] or [!h] (or float package, as suggested by Werner) to force positioning in case of necessity. But, on the other hand, figure also allows the use of a dedicated command \caption{ } inside.

Finally (this was not requested, I only write this to complete my code below), if you need to label your figure, then the \caption{ } command must come before \label{ }.

The resulting code:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

Text paragraph.

\begin{figure}[h]
\centering
\includegraphics[width=13.7cm, height=8.2cm]{pictures/c25k.png}
\caption{Here the caption.}
%\label{img1}
\end{figure}

Text paragraph %\ref{img1} and some other text

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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