# Generating a list with macros using tasks package

I cannot figure out how to generate a list with the tasks package using macros.

If you swap the three identified lines in the MWE below, you should get the following error message: \begin{tasks} on input line 25 ended by \end{document}

Notice that the use of the enumitem and the xparse packages (along with the fancy macro definitions) don't seem to have any effect on the issue (I've tried without both packages, using only LaTeX2e's \newcommand).

Any ideas how to fix this issue (i.e. make a macro-generated tasks)? Any insight as to why this is happening would also be appreciated!

\documentclass[letterpaper, 12pt]{article}

\usepackage{enumitem}
\usepackage{xparse}

\NewDocumentCommand{\macone}{s O{} +g +g}{%
\begin{enumerate}
}

\NewDocumentCommand{\mactwo}{s O{} +g +g}{%
\item #3
}

\NewDocumentCommand{\macthree}{s O{} +g +g}{%
\end{enumerate}
}

\begin{document}

\macone
\mactwo{Testing 123}
\macthree

\end{document}

• You don't mention anything about the requirement for \macroone, \macrotwo or \macrothree to keep/use its arguments. Are they really necessary? If so, could you please provide a use-case that incorporates those? – Werner Jun 1 '15 at 5:35
• One problem with your macro-approach is that the tasks environment is defined under environ and therefore needs an explicit \end{tasks}; the tasks environment captures its contents before processing it. – Werner Jun 1 '15 at 5:59
• The macros seem unnecessarily complicated in the MWE, but I am working on a very customized "exercise-answer" type package for my colleagues, and the lists are built "on-the-fly" by one environment (exercices) and two macros (\question and \sousquestion). The definitions in the MWE above are as I need them in the actual code, but I don't believe they affect the MWE in any way. – feculededentier Jun 1 '15 at 13:07
• There are at least two other questions asking the same thing about tasks on this site... – clemens Sep 1 '15 at 7:39

The solution seems much simpler:

\documentclass[letterpaper, 12pt]{article}

\begin{document}

\begin{enumerate}
\item First question
\begin{subquestions}
\subquestion First subquestion
\subquestion Second subquestion
\end{subquestions}
\item Second question
\item Third question
\begin{subquestions}[2]
\subquestion First subquestion
\subquestion Second subquestion
\subquestion Third subquestion
\end{subquestions}
\end{enumerate}

\end{document}


• The question is: will it work if the code is generated by macros? I know it's a weird requirement, but for various reasons, I need this to be the case. For example, the various questions and subquestions must be able to accept a list of options (pgfkeys). I have managed to write my personalized package using my workaround posted above. – feculededentier Jan 30 '16 at 1:44
• @feculededentier No detail about what you want to do with those optional arguments, no way to help. – egreg Jan 30 '16 at 10:54

Based on @Werner's comment about the tasks package using environ to define the tasks environment (I've verified this and it does so using LaTeX3 syntax), I have come up with the following workaround. Although this is less than optimal, it seems to work:

• first build the list by writing the code to an external file (within the macros or environments);
• once the entire list structure is complete, input the file back into the main document.

MWE:

\documentclass[letterpaper, 12pt]{article}

\usepackage{enumitem}
\usepackage{newfile}
\usepackage{xparse}

\newoutputstream{exfile}

\NewDocumentCommand{\startsubquestions}{s o +g +g}{%
}

\NewDocumentCommand{\subquestion}{s O{} +g +g}{%
}

\NewDocumentCommand{\endsubquestions}{s O{} +g +g}{%
}

\begin{document}

\openoutputfile{\jobname.ex}{exfile}%
\startsubquestions
\subquestion{First subquestion}
\subquestion{Second subquestion}
\endsubquestions
\startsubquestions[2]
\subquestion{First subquestion}
\subquestion{Second subquestion}
\subquestion{Third subquestion}
\endsubquestions
\closeoutputstream{exfile}%

\InputIfFileExists{\jobname.ex}{}{RECOMPILE (or file simply does not exist)''}%

\end{document}


The code generated within the \jobname.ex file is:

\begin{enumerate}
\item First question
\item Second question
\item Third question