[Tex/LaTex] Keep item and includegraphics or figure on same page

listspage-breakingsamepage

I'm creating a software guide where I use enumerated list for the steps. Many of the steps have a screenshot I display under the item. I'd like to keep the item and the following figure or graphic together. I've tried Samepage, which does not work. I've also tried minipages, which works but complicates the enumerated list. I'd have thousands of minipages if I did use it. My solution thus far was to add manual pagebreaks or clearpage. I'd prefer code so I don't have to continually edit the manual breaks.

MWE

    \section{Section Name}
    \begin{enumerate}
    \item Directions go here
    \FloatBarrier \begin{figure[!h]
    \centering\includegraphics[scale=0.65]{ImageA.png}
    \end{figure} \FloatBarrier
    \item Next step in directions.
    \FloatBarrier \begin{figure[!h]
    \centering\includegraphics[scale=0.65]{ImageB.png}
    \end{figure} \FloatBarrier
    \item Step three.
    \FloatBarrier \begin{figure[!h]
    \centering\includegraphics[scale=0.65]{ImageC.png}
    \end{figure} \FloatBarrier
\end{enumerate}

Best Answer

You need not have \includegraphics within a figure environment. In particular, if you want to place images inline within the text, you might as well use the center environment:

\documentclass{article}

\usepackage{mwe}
\usepackage{graphicx}

\begin{document}
\section{Section Name}
\begin{enumerate}
\item Directions go here
  \begin{center}
    \includegraphics[height=4cm]{example-image-a}
  \end{center}
\item Next step in directions.
  \begin{center}
    \includegraphics[height=4cm]{example-image-b}
  \end{center}
\item Step three.
  \begin{center}
    \includegraphics[height=4cm]{example-image-c}
  \end{center}
\end{enumerate}
\end{document}

output

Note that this, on its own, simply makes it easier to place the images but it does not prevent a page breaks between the text and the center environment. If you really want to force them being together, there are two options: either use the samepage environment, or define a new \par that prevents a page break (taken from this answer). Both are illustrated below:

\documentclass{article}

\usepackage{mwe}
\usepackage{graphicx}
\makeatletter 
\newcommand\nobreakpar{\par\nobreak\@afterheading} 
\makeatother

\begin{document}
\section{Section Name}
\begin{enumerate}
\item Directions go here   % This allows a page break
  \begin{center}
    \includegraphics[height=6cm]{example-image-a}
  \end{center}
\item Next step in directions. \nobreakpar % No page break here
  \begin{center}
    \includegraphics[height=6cm]{example-image-b}
  \end{center}
\item
  \begin{samepage}  % No page break anywhere inside this environment
    Step three.
    \begin{center}
      \includegraphics[height=6cm]{example-image-c}
    \end{center}
  \end{samepage}
\end{enumerate}
\end{document}