# include figure, auto-fit image and add caption at same time

I have to include some images to illustrate a problem. So I used the following code to auto-scale them to text-witdh, which works amazing. But how can I add some caption? (I have to label and number all figures)?

I found some solutions with google (caption etc.) but AFAIK they need a figure-object. So I also tried to use it instead of this box, but then I was not able to auto-fit the image.

\begin{center}
\makebox[\textwidth]{\includegraphics[width=\textwidth]{pstack}}
\end{center}


\usepackage{capt-of}
...

\noindent
\begin{minipage}{\textwidth}
\includegraphics[width=\textwidth]{pstack}
\captionof{figure}{foo}\label{bar}
\end{minipage}


or

\begin{figure}[!htb]
\includegraphics[width=\textwidth]{pstack}
\caption{foobar}\label{barbaz}
\end{figure}


You do not need the \makebox if your images are of the same width.

• wow, works like a charm! – NaN May 24 '14 at 7:58