4

I am using the caption package, now I have one problem: In one case an image is placed at the bottom of one page, while its caption is placed on the top of the following page. In this case I would prefer the image to be also moved to the next page.

Actually my LaTeX code looks like this:

\begin{flushleft}
    \includegraphics{sample.png}
    \captionof{figure}{the caption}
    \label{figure:sample}
\end{flushleft}

Is there some way to tell LaTeX that the graphic and the caption shall be seen as one unit and shall never be split up over 2 pages?

1
  • 2
    Why don't you use the figure environment? Nov 19, 2011 at 22:25

2 Answers 2

6

You could use a minipage environment enclosing figure and caption. Within minipage environments no pagebreak can occur.

Further there's the samepage environment for that purpose. Unlike minipage, samepage doesn't require a width argument.

6
  • Though further support of \samepage is doubtful, you could read about that in source2e.
    – Stefan Kottwitz
    Dec 4, 2010 at 23:48
  • thank you very much! minipage seems to work the best. is it maybe somehow possible to define in the preamble, that \includegraphics followed by \caption shall be automatically put in a minipage or do I have to add the minipage always manually? Dec 4, 2010 at 23:54
  • It should be added in the document body. Shortcuts might be used. But you could define a new environment or command taking filename, caption and label as argument, that produces a minipage together with \includegraphics, \caption and \label if you desire to compress your code.
    – Stefan Kottwitz
    Dec 5, 2010 at 0:06
  • No, he should use the figure environment. Nov 19, 2011 at 22:26
  • 1
    @MartinSchröder If he wants it to be able to float. If he knows the caption package and \captionof{figure}{...} it seems that he would like to avoid figures or floating, respectively, while keeping the caption below the image though. However, I agree with you recommending figure and preferring using and improving floating instead of avoiding it.
    – Stefan Kottwitz
    Nov 19, 2011 at 22:54
1

I would suggest to improve Stefan's answer by putting minipage inside figure environment. So image + caption can still be a floating object:

\begin{figure}
\begin{minipage}{\textwidth}
    \includegraphics{sample.png}
    \caption{the caption}
    \label{figure:sample}
\end{minipage}
\end{figure}
3
  • 3
    You don't need a minipage inside the figure environment. They all become a single entity.
    – percusse
    Aug 11, 2013 at 12:57
  • I guess, there is a reason for him not using a floating environment.
    – basseur
    Feb 21, 2018 at 11:08
  • Like if you are using multicols. Floats are not supported. Oct 29, 2021 at 8:20

You must log in to answer this question.

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