3

I'm writing a lab manual with step by step directions. Some directions include screenshots. To keep the screenshot with the line of direction, I use samepage. I am exploring an environment I can use for efficiency. The issue is that I want the screenshot centered, but not the line of text prior. Is it possible to write such an environment? Here's my MWE.

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{graphicx}

\newenvironment{myItem}{%
   \item 
   \begin{samepage}
   \begin{center}
   }
   {
   \end{center}
   \end{samepage}
   }

\begin{document}

\chapter{Chapter Blah} \label{chap:ChpBlah}
Chapter text here.

\begin{enumerate}
\item Step directions here.
\item Step directions here too.
\begin{myItem}Open Chrome or Firefox and browse to url here. 
\includegraphics[scale=0.55]{AnyImage.png}\end{myItem} %only the image should be centered
\end{enumerate}

\end{document}
1
  • No, not this way. You can simplify by using an (optional) argument to the environment
    – user31729
    May 27, 2016 at 17:07

2 Answers 2

2

Since the environment myItem effectively starts with the center environment, anything written after \begin{myItem} deliberately is centered (unless shifted additionally).

I suggest to use an (optional) argument for the description text, this way the description text will be given to \item #1 first and then the centering occurs.

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{graphicx}

\newenvironment{myItem}[1][]{%
   \item #1
   \begin{samepage}
     \begin{center}
     }
     {%
     \end{center}
   \end{samepage}
 }

\begin{document}

\chapter{Chapter Blah} \label{chap:ChpBlah}
Chapter text here.

\begin{enumerate}
\item Step directions here.
\item Step directions here too.
\begin{myItem}[Open Chrome or Firefox and browse to url here.]
\includegraphics[scale=0.2]{ente}\end{myItem} %only the image should be centered
\end{enumerate}

\end{document}

enter image description here

Checking if #1 is used at all:

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{graphicx}
\usepackage{etoolbox}

\newenvironment{myItem}[1][]{%
  \notblank{#1}{
  \item #1
  }{}
   \begin{samepage}
     \begin{center}
     }
     {%
     \end{center}
   \end{samepage}
 }

\begin{document}

\chapter{Chapter Blah} \label{chap:ChpBlah}
Chapter text here.

\begin{enumerate}
\item Step directions here.
\item Step directions here too.
\begin{myItem}[Open Chrome or Firefox and browse to url here.]
\includegraphics[scale=0.2]{ente}\end{myItem} %only the image should be centered
\end{enumerate}

\clearpage
\begin{enumerate}
\item Step directions here.
\item Step directions here too.
\begin{myItem}
\includegraphics[scale=0.2]{ente}
\end{myItem}
\end{enumerate}


\end{document}
1
  • @PepPaddy: You're welcome. I will add an improvement version later on, that checks for the 1st argument and omits the \item if it is not present.
    – user31729
    May 27, 2016 at 17:19
1

The following myItem environment redefines \includegraphics to place the image centred. As such, you can place content before or after the image and they will be justified as set previously.

enter image description here

\documentclass[twoside,openright,12pt]{report}

\usepackage{graphicx}

\newenvironment{myItem}{%
  \item%
    \let\oldincludegraphics\includegraphics
    \renewcommand{\includegraphics}[2][]{%
      \par\nobreak
      {\centering\oldincludegraphics[##1]{##2}\par}}
  }{}

\begin{document}

\chapter{Chapter Blah}
Chapter text here.

\begin{enumerate}
  \item Step directions here.
  \item Step directions here too.
  \begin{myItem}
    Open Chrome or Firefox and browse to URL here.
    \includegraphics[scale=0.55]{example-image}%only the image should be centered
  \end{myItem}
\end{enumerate}

\end{document}

I've minimized the example slightly as it seems like you're not interested in having text after the image. However, if you do, one could find a way around that as well, making content stay on the same page.

0

You must log in to answer this question.

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