5

I need to wrap a tasks environment inside a custom \NewEnviron. Apparently, tasks chokes on code besides its \task or \item due to the way it handles its own environment body, presumably causing it to do so on \BODY. Any way to get around this? Exact error given is "Something's wrong--perhaps a missing \item", as per:

\documentclass[a4paper]{book}
\usepackage{environ}
\usepackage[more]{tasks}

\NewTasks[style=enumerate,counter-format=tsk[1]),label-width=2ex]{mytaskenum}[\item](3)
\NewEnviron{mytaskenv}[1][Exercises]{
  % Misc. stuff to precede the tasks env here
  \begin{mytaskenum}
  \BODY
  \end{mytaskenum}
  % Misc. stuff to follow
}{}

\begin{document}

\begin{mytaskenv}
  \item item 1
  \item item 2
  \item item 3
\end{mytaskenv}

\end{document}
  • @clemens \BODY is provided by environ; it expands to the contents of what's between \begin{mytaskenv} and \end{mytaskenv}. Perhaps I'm misunderstanding your suggestion? – Texman Apr 7 '15 at 7:35
  • 1
    tasks needs to see the expansion of the macro \BODY in order to do its work – clemens Apr 7 '15 at 7:41
  • Is there a simple way to accomplish that? – Texman Apr 7 '15 at 22:07
5
+50

You have to expand \BODY before \begin{mytaskenum} starts its job:

\documentclass[a4paper]{book}
\usepackage{environ}
\usepackage[more]{tasks}

\NewTasks[style=enumerate,counter-format=tsk[1]),label-width=2ex]{mytaskenum}[\item](3)
\NewEnviron{mytaskenv}[1][Exercises]{%
  % Misc. stuff to preceed the tasks env here
  \def\tempbegin{\begin{mytaskenum}}%
  \expandafter\tempbegin\BODY
  \end{mytaskenum}
  % Misc. stuff to follow
}

\begin{document}

\begin{mytaskenv}
  \item item 1
  \item item 2
  \item item 3
\end{mytaskenv}

Check:

\begin{mytaskenum}
  \item item 1
  \item item 2
  \item item 3
\end{mytaskenum}

\end{document}

enter image description here

Cleaner alternative with xparse:

\documentclass[a4paper]{book}
\usepackage{environ}
\usepackage[more]{tasks}
\usepackage{xparse}

\NewTasks[style=enumerate,counter-format=tsk[1]),label-width=2ex]{mytaskenum}[\item](3)

\ExplSyntaxOn
\NewDocumentCommand{\domytaskenum}{}
 {
  \texman_mytaskenum:V \BODY
 }
\cs_new_protected:Npn \texman_mytaskenum:n #1
 {
  \begin{mytaskenum}
  #1
  \end{mytaskenum}
 }
\cs_generate_variant:Nn \texman_mytaskenum:n { V }
\ExplSyntaxOff

\NewEnviron{mytaskenv}[1][Exercises]{%
  % Misc. stuff to preceed the tasks env here
  \domytaskenum
  % Misc. stuff to follow
}


\begin{document}

\begin{mytaskenv}
  \item item 1
  \item item 2
  \item item 3
\end{mytaskenv}

Check:

\begin{mytaskenum}
  \item item 1
  \item item 2
  \item item 3
\end{mytaskenum}

\end{document}

The \domytaskenum command simply calls \texman_domytaskenum:V \BODY; in turn \texman_domytaskenum:V is a variant of \texman_domytaskenum:n that does the job of processing the argument in the mytaskenum environment. The V variant uses the value of the token list stored in \BODY as if it was the explicit contents. Basically it does the same trick as before, but in a more abstract way, without doing the temporary definition.

  • Can you get this to work an xparse NewDocumentEnvironment rather than a environ environment? I am trying to get this to work for more than one optional argument. – Pravin Sep 15 '17 at 1:38
  • @Pravin More than one optional arguments make for awkward syntax; please, make a new question with the desired specifications. – egreg Sep 15 '17 at 8:08
  • please see tex.stackexchange.com/questions/391459/… – Pravin Sep 15 '17 at 8:10

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.