Custom enumitem list reset conditional variable

I've defined a new list like this:

\usepackage{enumitem}
\usepackage{mdframed}

\newcommand{\questionlabel}{Question }
\newlist{questions}{enumerate}{4}
\setlist[questions]{
align=left,
ref=\questionlabel\arabic*,
label=\textbf{\questionlabel\arabic*.},
wide
}
\newif\ifquestionsincludelabel
\newcommand{\includelabel}{\questionsincludelabeltrue}
\newcommand{\excludelabel}{\questionsincludelabelfalse}
\newcommand{\question}[2][]{
\needspace{10\baselineskip}
\ifquestionsincludelabel \item #1 \else \item[] #1 \fi
\mbox{}\begin{mdframed}#2\end{mdframed}
}


It uses conditionals to determine whether to print the label. Is there a way to reset this conditional, i.e. execute \includelabel, every time a new list is started with \begin{questions}? It'd be easy to do if questions were defined using \newenvironment, but it uses enumitem's newlist instead.

P.S. when the conditional branch with \item[] is executed, the counter is not incremented. I'd like to automatically do it, but putting \addtocounter{???}{1} doesn't work as I don't know the name of the counter. Is there a way to target the current list or the innermost one (questionsi, questionsii, etc.)?

• Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. A MWE should compile and be as small as possible to demonstrate your problem. it's much easier to help you if we have full working code to start from. – user30471 Nov 1 '17 at 7:49

 before=\includelabel


to the definition of questions. In fact, I would just set \ifquestionsincludelabel to be true directly using

\setlist[questions]{
align=left,
ref=\questionlabel\arabic*,
label=\textbf{\questionlabel\arabic*.},
wide,
before=\questionsincludelabeltrue
}


This said, I wonder whether you need to use \ifquestionsincludelabel at all and whether a better syntax is given by defining a starred version of \question so that \question includes the label and \question* does not. The easiest way to define \question this way is using \NewDocumentCommand from the xparse package:

\NewDocumentCommand{\question}{ s O{} m }{
\needspace{10\baselineskip}
\IfBooleanTF{#1}{\item #2}{\item[]#2}
\mbox{}\begin{mdframed}#3\end{mdframed}
}


Although, possibly you want to switch the starred and unstarred behaviour.

The s O{} m says that this command has three arguments:

• An optional '*' as #1. As above, you can test to see if a * has been used with

 \IfBooleanTF{#1}{...true code}{...false code}

• An optional label, #2, that defaults to {}. (This is your previous #1.)

• A mandatory #3, which is put inside the mdfamed environment

For more details see the xparse manual. It's very readable.

Here is a full minimal working example

\documentclass[a4paper,10pt]{article}
\usepackage{xparse}
\usepackage{enumitem}
\usepackage{mdframed}

\newcommand{\questionlabel}{Question }
\newlist{questions}{enumerate}{4}
\setlist[questions]{
align=left,
ref=\questionlabel\arabic*,
label=\textbf{\questionlabel\arabic*.},
wide,
before=\questionsincludelabeltrue
}
\newif\ifquestionsincludelabel
\newcommand{\includelabel}{\questionsincludelabeltrue}
\newcommand{\excludelabel}{\questionsincludelabelfalse}
\NewDocumentCommand{\question}{ s O{} m }{
\needspace{10\baselineskip}
\IfBooleanTF{#1}{\item #2}{\item[]#2}
\mbox{}\begin{mdframed}#3\end{mdframed}
}

\begin{document}

\begin{questions}
\question {Some text}
\question[2]{More text}
\question*{Even more text}
\question*[4]{Last bit of text }
\end{questions}
\end{document}


and here is the output:

Btw, it is not clear to me that you need to wrap this inside an enumerate environment, as it is not doing that much except making it clear that all of the questions belong "together". You could do equally well with something like:

\newcounter{question}
\NewDocumentCommand{\question}{ s O{} m }{%
\needspace{10\baselineskip}%
\refstepcounter{question}%
\IfBooleanTF{#1}%
{\item \textbf{\questionlabel\space\thequestion} #2}%
{\item[]#2}%
\mbox{}\begin{mdframed}#3\end{mdframed}
}