How make sure two elements stay on the same page? [duplicate]

Possible Duplicate:
Unbreakable block

I am writing a very simple how-to document with several screenshots. The document flow goes something like this:

A few sentences explaining action 1

\begin{center}
\includegraphics{action_1_screenshot}
\end{center}

A few sentences explaining action 2

\begin{center}
\includegraphics{action_2_screenshot}
\end{center}


However since the screenshots are often large, I sometimes get page breaks between the paragraph and the corresponding screenshot. I want the paragraph to always be on the same page as a screenshot.

I have used \clearpage after each screenshot, but that doesn't quite get what I want. \clearpage would make sure that there is a page break after each screenshot, but I still want multiple screenshots on the same page if they fit. Ideally, I would have something like this to guarantee that two items were on the same page, though wouldn't necessarily always cause a page break:

\begin{samepage}

A few sentences explaining action 2

\begin{center}
\includegraphics{action_2_screenshot}
\end{center}

\end{samepage}


Is something like this possible? I'm not really looking for figures, because those don't tend to work well with longer descriptive paragraphs. Figures seem to be more optimized for a short sentence or sidenote.

-

marked as duplicate by lockstep, Gonzalo Medina, Andrey Vihrov, Martin Scharrer♦Oct 6 '11 at 19:53

Did you try what you have posted? Looks fine to me. –  topskip Oct 6 '11 at 19:45
@Patrick Holy cow, I never tried samepage, I just thought such an environment was wishful thinking on my part. Turns out that it actually exists... –  Phil Oct 6 '11 at 19:52

If you put everything together into a {minipage}{\textwidth} environment you ensure that everything is placed on one page. However, this doesn't allow for normal footnotes, which require more work. Also you will get ugly page breaks.

I would rather use figure environments for the screenshots together with \caption and \label so you can \ref-erence them in the text. You can place two \includegraphics into one figure with separate \captions to ensure they are always placed together.

You could also define a samepage environment which uses two zref page labels in the \begin and \end code. Then after one compiler run the \begin code can check if both are on separate pages and cause an \clearpage if not. The code in my answer to Test if a paragraph has a page break in it? could be used for this.

-

The minipage environment is what you're after. That is

\begin{minipage}{\textwidth}

A few sentences explaining action 2

\begin{center}
\includegraphics{action_2_screenshot}
\end{center}

\end{minipage}

-