# Checking if current item is last item inside enumerated list

I have a personalized class for exams and I need to do something different before the end of my custom enumerated list (questions).

After each question I add a new blank page with some text at the top (such as "(question X, cont'd)", and then start the next question on a new page -- so question 1 is on the first page, page 2 is blank except for some text on the top, and question 2 is on the third page. But for the last question I don't want to add the extra page for the next question (there is no next question).

Is there a way of doing this without manually adding the \newpage? That is, can I code this inside the custom enumerated list, so that if I change the order of the questions I won't have to keep changing the information?

I am already using the enumitem package, but can't find an option to check if current item is the last item (even if compiled more than once).

What I have right now (simplified):

\documentclass{article}
\begin{document}

Questions:

\begin{enumerate}
\item \label{qa} This is a question.
\newpage
(Question \ref{qa}, cont'd)
\newpage
\item \label{qb} Another question.
\newpage
(Question \ref{qb}, cont'd)
\newpage
\item \label{qc} Final question.  Note that there is only one "newpage" after it.
\newpage
(Question \ref{qc}, cont'd)
\end{enumerate}
\end{document}


I'm trying to create an environment which has this behavior but I'm not seeing a solution.

I've also thought that perhaps I could change the first item (such that it does not get a \newpage before the item is defined), as it might be easier to check if the counter is zero.

Also, as a bonus, is it possible to create two different types of \items depending on the behavior expected (for example, if for a question I didn't want to add any extra pages)?

Here's an approach that allows for more manipulations, if needed.

I split the entire environment's contents at \question, remove the first item in the sequence because it will be blank, and then deliver the sequence adding \newpage\item between items.

\documentclass{article}

\usepackage{environ,xparse}

\ExplSyntaxOn
\NewEnviron{questions}
{
\seq_set_split:NnV \l_malu_questions_seq { \question } \BODY
\seq_pop_left:NN \l_malu_questions_seq \l_tmpa_tl % remove the initial blank
\begin{enumerate}
\item \seq_use:Nn \l_malu_questions_seq { \newpage\item }
\end{enumerate}
}
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\seq_new:N \l_malu_questions_seq
\ExplSyntaxOff

\begin{document}

Questions:

\begin{questions}
\question \label{qa} This is a question.
\newpage
(Question \ref{qa}, cont'd)
\question \label{qb} Another question.
\newpage
(Question \ref{qb}, cont'd)
\question \label{qc} Final question.  Note that there is only one "newpage" after it.
\newpage
(Question \ref{qc}, cont'd)
\end{questions}

\end{document}


You can automate the process of adding a \newpage before every \item but delay the first \newpage use a cunning trick:

\documentclass{article}

\newenvironment{questions}
{\let\olditem\item
% Delay \newpage by one \question (https://tex.stackexchange.com/a/89187/5764)
\def\itemnewpage{\def\itemnewpage{\newpage}}%
\def\question{\itemnewpage\olditem}%
\begin{enumerate}
\let\item\question}
{\end{enumerate}}

\begin{document}

Questions:

\begin{questions}
\question \label{qa} This is a question.
\newpage
(Question \ref{qa}, cont'd)

\question \label{qb} Another question.
\newpage
(Question \ref{qb}, cont'd)

\question \label{qc} Final question.
\newpage
(Question \ref{qc}, cont'd)
\end{questions}

\end{document}


I defined some semantic language for your environment and questions. This way it makes sense what they mean.

If the structure of your \questions are always the same (question, followed by a blank page, then the wording Question X (cont'd) and a blank page), one can automate that process as well.

• What's the purpose of \let\olditem\item, as you're using \question in the environment? Oct 6, 2017 at 13:36

With the usage of a few Boolean flags and checks we could achieve what you asked for:

1. After each question, add a new blank page with some text at the top (such as (question X, cont'd).
2. Then start the next question on a new page.
3. Create two different types of \items depending on the behavior expected, for example, for a question we don't want to add any extra page.

\documentclass{article}

\usepackage{xparse}
% First, optional argument, put [n] for no extra page needed
% Second, question text
\NewDocumentCommand\qitem{O{}m}{\iftoggle{qstarted}%
% Already a non first question, needs a newpage
{\newpage}
% First question, set flag, does not need a newpage
{\toggletrue{qstarted}}
\item\label{ql\arabic{enumi}}#2
% Check if the user wanted an extra page for this question
\ifstrequal{#1}{n}
% Did not want an extra page
{\relax}
% Wanted an extra page
{\newpage
(Question \ref{ql\arabic{enumi}}, cont'd)}}

\usepackage{etoolbox}
\newtoggle{qstarted}
\togglefalse{qstarted}

\begin{document}

Questions:

\begin{enumerate}
\qitem{This is a question.}

\qitem{Another question.}

\qitem[n]{This question will not have an extra page}

\qitem{Final question.  Note that there is only one "newpage" after
it.}
\end{enumerate}
\end{document}


The Output

The code achieves exactly what you asked for.

How It Works?

Flag qstarted keeps track of whether we are past the first question. This flag is set to false as soon as we start the first question.

We added an optional argument [n] to \qitem. If this argument if found to be set, no extra page is generated.