2

I tried to use the tasks environment from the tasks package along with the \foreach command from pgffor to automatically create tasks. However, my approach does not work and latex outputs

Package tasks Warning: You've placed (tasks) (tasks) \foreach \i in {0,...,5}{\task \i } (tasks) (tasks) before the first \task on line 98. I will throw it (tasks) away and ignore it for the rest of the list.

This is a MWE of what I tried to do to create the 5 tasks 0, 1,...,4:

\documentclass{article}
\usepackage{pgffor}
\usepackage{tasks}

\begin{document}
\begin{tasks}
\foreach \i in {0,...,5}{
\task\i
}
\end{tasks}
\end{document}

Is there any way to use \foreach in the tasks environment?

2
  • As you may notice, your posted code can't compile. Please add, what's missing. Thank you
    – MS-SPO
    Commented Jun 22 at 7:28
  • 1
    @MS-SPO done, I added the preamble
    – YukiJ
    Commented Jun 22 at 7:37

1 Answer 1

3

The tasks environment doesn't process items one after the other: it first reads the entire contents up to \end{tasks} and uses \task as a placeholde to store items separately, so as to be able to process them in various ways.

Hence the \task commands should be explicit and your \foreach approach cannot work.

You need to build the \task structure beforehand.

\documentclass{article}
\usepackage{tasks}

\ExplSyntaxOn

\NewDocumentCommand{\makedummytasks}{O{1}m}
 { % #1 = starting point
   % #2 = end point
   \tl_clear:N \l_tmpa_tl
   \int_step_inline:nnn { #1 } { #2 } { \tl_put_right:Nn \l_tmpa_tl { \task ##1 } }
   \exp_args:Nno \begin{tasks} \l_tmpa_tl \end{tasks}
 }

\ExplSyntaxOff

\begin{document}

\makedummytasks[0]{5}

\end{document}

enter image description here

A more complex solution where you can state the number of columns like tasks does, but also apply some processing to the dummy items. The trailing optional argument is a template where #1 takes the place of \i in \foreaach.

\documentclass{article}
\usepackage{tasks}

\ExplSyntaxOn

\NewDocumentCommand{\makedummytasks}
 {
  O{1}    % start point (optional, default 1)
  m       % end point
  D(){1}  % number of columns in parentheses (optional, default 1)
  o       % optional template
 }
 {
   \group_begin:
   \IfValueTF { #4 }
    {
     \cs_set:Nn \__yuki_dummytask_text:n { #4 }
    }
    {
     \cs_set:Nn \__yuki_dummytask_text:n { ##1 }
    }
   \tl_clear:N \l_tmpa_tl
   \int_step_inline:nnn { #1 } { #2 }
    {
     \tl_put_right:Nn \l_tmpa_tl { \task \__yuki_dummytask_text:n {##1} }
    }
   \yuki_dummytask_make:nV {#3} \l_tmpa_tl
   \group_end:
 }

\cs_new_protected:Nn \yuki_dummytask_make:nn
 {
  \begin{tasks}(#1) #2 \end{tasks}
 }
\cs_generate_variant:Nn \yuki_dummytask_make:nn {nV}

\ExplSyntaxOff

\begin{document}

\makedummytasks[0]{8}(3)

\bigskip

\makedummytasks[0]{5}(2)[This is task #1]

\bigskip

\makedummytasks{5}[This is task #1]

\end{document}

enter image description here

5
  • Thank you for the explanation! What would I need to change if the actual task isn't just \i but something that dependes on \i, say something like a string as "This is task \i"?
    – YukiJ
    Commented Jun 22 at 8:46
  • 1
    @YukiJ \task This~is~task~##1 instead of \task ##1
    – egreg
    Commented Jun 22 at 8:55
  • Thank you! Is it also possible to make the dummy-environment in a multicolored fashion just like with \begin{tasks}(2) ... \end{tasks} in the original tasks package for tasks in 2 columns instead of 1?
    – YukiJ
    Commented Jun 22 at 9:02
  • 1
    @YukiJ You can do even better, see edit.
    – egreg
    Commented Jun 22 at 9:24
  • Wow, this is perfect! Thank you!
    – YukiJ
    Commented Jun 22 at 10:07

You must log in to answer this question.

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