3

I am trying to remap the \task to a new command that can take multiple arguments (noting that \NewTasksEnvironment can be used to change \task to \item, but this is not what I am trying to fix here).

I appreciate that the solution must be more nuanced than the below to maintain compatibility with the various tasks features (e.g. \task* or \task!), but nevertheless, trying to make the below work (if possible):

\documentclass{article}
\usepackage{tasks}
\usepackage{xparse}

% Define \itemJ using \NewDocumentCommand
\NewDocumentCommand{\itemJ}{m O{}}{%
    \task #1 \\
    \textit{Solution}:
    #2 
}

\begin{document}

\title{Tasks Package Example with \texttt{NewDocumentCommand}}
\author{Your Name}
\date{\today}
\maketitle

\section*{Example Task Lists}

\subsection*{Simple Task List with itemJ}
\begin{tasks}
\itemJ{First item}
\itemJ{Second item}
\itemJ{Third item}
\end{tasks}

\subsection*{Task List with Multiple Columns}
\begin{tasks}(3)
\itemJ{First item}
\itemJ{Second item}
\itemJ{Third item}
\itemJ{Fourth item}
\itemJ{Fifth item}
\itemJ{Sixth item}
\end{tasks}

\subsection*{Task List with Custom Labels}
\begin{tasks}[label=\alph*., label-format={\bfseries}](2)
\itemJ{First item}
\itemJ{Second item}
\itemJ{Third item}
\itemJ{Fourth item}
\end{tasks}

\end{document}

Current no tasks print

enter image description here

2
  • It seems that \task just acts as a separator, try \begin{tasks}(3) \task {\meaning\task} \end{tasks}, yield a) undefined.
    – ljguo
    Commented Jul 31 at 2:52
  • The problem here is that the main tasks environment looks for explicit use of the control sequence \task and use that as a separating delimiter to identify (and manipulate) each task. So, without it, the environment assumes there's no tasks and nothing to do.
    – Werner
    Commented Jul 31 at 2:54

1 Answer 1

5

The idea is simple: define \itemJ to put its argument into a token list variable. Then use V signature to expand this token list variable first so that the tasks environment could see \task as its item separator.

\documentclass{article}
\usepackage{tasks}

\ExplSyntaxOn
\NewDocumentCommand{\itemJ}{m O{}}{%
  \tl_put_right:Nn \l_tmpa_tl { \task #1 \\ \textit{Solution:} ~ #2 }
}
\NewDocumentEnvironment{harrytasks}
  {
    O{} % optional options
    D(){1} % number of columns
  }
  {
    \tl_clear:N \l_tmpa_tl
  }
  {
    \harry_task_make:nnV {#1} {#2} \l_tmpa_tl
  }
\cs_new_protected:Npn \harry_task_make:nnn #1#2#3
  {
    \begin{tasks}[#1](#2) #3 \end{tasks}
  }
\cs_generate_variant:Nn \harry_task_make:nnn {nnV}
\ExplSyntaxOff


\begin{document}

\title{Tasks Package Example with \texttt{NewDocumentCommand}}
\author{Your Name}
\date{\today}
\maketitle

\section*{Example Task Lists}

\begin{harrytasks}
  \itemJ{First item}[This is the solution]
  \itemJ{Second item}
  \itemJ{Third item}
\end{harrytasks}

\medskip

\begin{harrytasks}(2)
  \itemJ{First item}
  \itemJ{Second item}
  \itemJ{Third item}
  \itemJ{Fourth item}
\end{harrytasks}

\medskip

\begin{harrytasks}[label=\alph*., label-format={\bfseries}](2)
  \itemJ{First item}
  \itemJ{Second item}
  \itemJ{Third item}
  \itemJ{Fourth item}
\end{harrytasks}

\end{document}

enter image description here

7
  • Stephen, yyds !!!
    – ljguo
    Commented Jul 31 at 4:31
  • I believe that the optional argument to the environment should be O{} rather than o
    – egreg
    Commented Jul 31 at 9:09
  • Modified.@egreg
    – Stephen
    Commented Jul 31 at 14:08
  • Unreal; lovely solution
    – Harry
    Commented Aug 2 at 8:38
  • This works a charm. Any way we can make \task (or better \item) work within the list for backwards compatibility sake, it doesnt at the moment, e.g. \begin{harrytasks}(2) \item abcd \itemJ{First item} \itemJ{Second item} \itemJ{Third item} \itemJ{Fourth item} \end{harrytasks} and Ideally \item just is \itemP{#1}
    – Harry
    Commented Aug 6 at 7:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .