# Nesting Tasks within an xparse Environment

I need to use xparse as I am passing multiple optional arguments through a custom environment (not shown here for brevity). When I attempt to pass the following:

\documentclass{article}
\usepackage{xparse}
\newlength{\longlabel}
\settowidth{\longlabel}{(m)}
item-indent=0pt, label-offset=2pt, column-sep={10pt}, before-skip=-8pt, after-item-skip=2pt, after-skip=-4pt]{newTask}[\item]

\NewDocumentEnvironment{dummyEnvironment}{}

\begin{document}

\item Item 1
\item Item 2
\item Item 3
\item Item 4

\begin{dummyEnvironment}
\item Item 1
\item Item 2
\item Item 3
\item Item 4
\end{dummyEnvironment}

\end{document}


An error returns stating that \begin{newTasks} is ended by \end{dummyEnvironment}

Error:

C:\Users... LaTeX Error: \begin{newTask} on input line 22 ended by \end{dummyEnvironment}. [^^I\end{dummyEnvironment}]
C:\Users... LaTeX Error: \begin{dummyEnvironment} on input line 22 ended by \end{document}. [\end{document}]


Now this question has been answered before for the environ' package here, but how would you fix this problem using xparse environments?

You can (in fact, as egreg comments below, you should) use \newtask...\endnewtask directly:

\documentclass{article}
\usepackage{xparse}
\newlength{\longlabel}
\settowidth{\longlabel}{(m)}
item-indent=0pt, label-offset=2pt, column-sep={10pt}, before-skip=-8pt, after-item-skip=2pt, after-skip=-4pt]{newTask}[\item]

\NewDocumentEnvironment{dummyEnvironment}{}

\begin{document}

\item Item 1
\item Item 2
\item Item 3
\item Item 4

\begin{dummyEnvironment}
\item Item 1
\item Item 2
\item Item 3
\item Item 4
\end{dummyEnvironment}

\end{document}


When you open an environment with \begin{newtask}...\end{newtask} what happens is that a new tex-group is opened, using \bgroup or \begingroup (I can't remember which), and \newtask is called. Latex then looks ahead for a matching \end{newtask} whereupon it inserts \endnewtask and closes the group. With the dummyEnvironment in the OP, latex sees a \begin{newtask} and so looks ahead for a matching \end{newtask}. Unfortunately, it can't find this because the \end{newtask} is hidden in \end{dummyEnvironment}, which it sees first. This is why the code in the OP gives an error. By using \newtask....\endnewtask you avoid this issue.

For completeness, here is the output:

• The fact is that newTask is defined in terms of \NewEnviron, so the standard method for dealing with environments defined upon it is to use \newTask...\endnewTask`. The first verb in your answer should be should rather than can. – egreg Sep 15 '17 at 8:18
• Wow, what an excellent answer. Thank you both very much!! – Pravin Sep 15 '17 at 9:12