0

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{tasks}
\usepackage{xparse}

\NewDocumentCommand{\macone}{s O{} +g +g}{%
  \begin{enumerate}
  %\begin{tasks}%  %% <-- USE THIS INSTEAD OF LINE ABOVE
}

\NewDocumentCommand{\mactwo}{s O{} +g +g}{%
  \item #3
  %\task #3%  %% <-- USE THIS INSTEAD OF LINE ABOVE
}

\NewDocumentCommand{\macthree}{s O{} +g +g}{%
  \end{enumerate}
  %\end{tasks}%  %% <-- USE THIS INSTEAD OF LINE ABOVE
}

\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
1

The solution seems much simpler:

\documentclass[letterpaper, 12pt]{article}

\usepackage{tasks}

\NewTask{subquestions}[\subquestion]

\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}

enter image description here

  • 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
0

If anybody has a better way of doing this, please post your answer!!!


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{tasks}
\usepackage{xparse}

\newoutputstream{exfile}

\NewDocumentCommand{\startsubquestions}{s o +g +g}{%
  \addtostream{exfile}{\protect\begin{tasks}\IfNoValueF{#2}{(#2)}}%
}

\NewDocumentCommand{\subquestion}{s O{} +g +g}{%
  \addtostream{exfile}{\protect\task\space #3}%
}

\NewDocumentCommand{\endsubquestions}{s O{} +g +g}{%
  \addtostream{exfile}{\protect\end{tasks}}%
}

\begin{document}

\openoutputfile{\jobname.ex}{exfile}%
\addtostream{exfile}{\protect\begin{enumerate}}
\addtostream{exfile}{\protect\item\space First question}
        \startsubquestions
        \subquestion{First subquestion}
        \subquestion{Second subquestion}
        \endsubquestions
\addtostream{exfile}{\protect\item\space Second question}
\addtostream{exfile}{\protect\item\space Third question}
        \startsubquestions[2]
        \subquestion{First subquestion}
        \subquestion{Second subquestion}
        \subquestion{Third subquestion}
        \endsubquestions
\addtostream{exfile}{\protect\end{enumerate}}
\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
\begin{tasks}
\task First subquestion
\task Second subquestion
\end{tasks}
\item Second question
\item Third question
\begin{tasks}(2)
\task First subquestion
\task Second subquestion
\task Third subquestion
\end{tasks}
\end{enumerate}


The output is:

Output associated to the first code block in this answer

  • If you can have the user interface be an environment, you can do it without all of that apparatus. – cfr Nov 30 '15 at 1:41
  • @cfr I don't understand your proposition; feel free to elaborate! My entire overly complicated homemade "package" is working, but if you have a better way of doing this, it could be useful for others. – feculededentier Dec 1 '15 at 2:50
  • What I mean is: it is because you are using commands not environments that you can't handle the environ stuff straightforwardly. If you could use environments, it would be straightforward. But if you have something which works and you're happy with it, let it be. – cfr Dec 1 '15 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.