0

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{tasks}
\usepackage{xparse}
\newlength{\longlabel}
\settowidth{\longlabel}{(m)}
\NewTasks[label-format=\color{blue}, counter-format={(tsk[a])}, label-width=\longlabel,
    item-indent=0pt, label-offset=2pt, column-sep={10pt}, before-skip=-8pt, after-item-skip=2pt, after-skip=-4pt]{newTask}[\item]

\NewDocumentEnvironment{dummyEnvironment}{}
  {\begin{newTask}(2)}
  {\end{newTask}}

\begin{document}

\begin{newTask}(2)
    \item Item 1
    \item Item 2
    \item Item 3
    \item Item 4
    \end{newTask}

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

2

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

\documentclass{article}
\usepackage{tasks}
\usepackage{xparse}
\newlength{\longlabel}
\settowidth{\longlabel}{(m)}
\NewTasks[label-format=\color{blue}, counter-format={(tsk[a])}, label-width=\longlabel,
    item-indent=0pt, label-offset=2pt, column-sep={10pt}, before-skip=-8pt, after-item-skip=2pt, after-skip=-4pt]{newTask}[\item]

\NewDocumentEnvironment{dummyEnvironment}{}
  {\newTask(2)}
  {\endnewTask}

\begin{document}

\begin{newTask}(2)
    \item Item 1
    \item Item 2
    \item Item 3
    \item Item 4
    \end{newTask}

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

enter image description here

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

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.