# tasks environment vs. pgffor's foreach

I want to \foreach on a list and create task (via package tasks) inside a question. A MWE:

\documentclass{article}
\usepackage[magyar]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{t1enc}
\usepackage{exsheets}
\usepackage{pgffor}
\usepackage{fp}
\def\pontlist{
2/3/1/4/,
5/1/-4/3/,
2/7/11/10/,
4/-3/5/7/,
-4/4/5/-7/,
-4/-4/4/4/,
3/7/-3/10/,
-1/-2/3/4/,
-1/3/-2/1/
}
\newcommand{\felezopont}[4]{
\FPeval{\resx}{round( ( (#1)+(#3) )/2,1)}
\FPeval{\resy}{round( ( (#2)+(#4) )/2,1)}
\task $A(#1;#2)$, $B(#3;#4)$
{$(\resx;\resy)$}
}
\begin{document}
\begin{question}
Határozd meg a következő pontpárok felezőpontját!
\foreach \ax/\ay/\bx/\by in \pontlist {
\felezopont{\ax}{\ay}{\bx}{\by}
}
\end{question}
\end{document}


Without \begin{tasks} ... \end{tasks} works well but the use of tasks environment causes an error:

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.33 \end{question}


What do I use wrong?

• The \tasks environments requites a series of \task commands. Would you use a list environment without \items? – Bernard Feb 16 '17 at 19:50
• Yes, I think maybe it's the problem. But how can I do it? I tried to place \task before \felezopont (not inside \felezopont) but it didn't work. If you've another idea instead of pgffor it's OK for me. The most important thing: work properly :) – uzsolt Feb 17 '17 at 6:23

You need to ensure that the tasks environment actually sees the \tasks. They can't be hidden in a macro (even if it were expandable). BTW: tasks is a package of its own – exsheets just loads it.

Here is a possible solution:

\newcommand*\pontlist{
2/3/1/4/,
5/1/-4/3/,
2/7/11/10/,
4/-3/5/7/,
-4/4/5/-7/,
-4/-4/4/4/,
3/7/-3/10/,
-1/-2/3/4/,
-1/3/-2/1/
}

% remove \task from the definition:
\newcommand{\felezopont}[4]{
\FPeval{\resx}{round( ( (#1)+(#3) )/2,1)}
\FPeval{\resy}{round( ( (#2)+(#4) )/2,1)}
$A(#1;#2)$, $B(#3;#4)$
{$(\resx;\resy)$}
}

% build list for the tasks environment:
\foreach \ax/\ay/\bx/\by in \pontlist {
\begingroup
\edef\x{\endgroup
}\x
}

% needed helper macros so the tasks environment actually sees \task macros:
\newcommand\switchtwo[2]{#2#1}
\newcommand\expandsecond[2]{%
\expandafter\switchtwo\expandafter{#1}{#2}%
}


and then:

\expandsecond\taskslist


This solution has the disadvantage that it cannot be used in an environment like the question environment of the exsheets package because \begin{tasks} is hidden from it and \end{tasks} is not. But the solution to this problem is simple: hide it in a macro completely:

\newcommand*\printtaskslist{%
}


and then

\begin{question}
Text
\end{question}


Complete code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgffor}
\usepackage{fp}

\newcommand*\pontlist{
2/3/1/4/,
5/1/-4/3/,
2/7/11/10/,
4/-3/5/7/,
-4/4/5/-7/,
-4/-4/4/4/,
3/7/-3/10/,
-1/-2/3/4/,
-1/3/-2/1/
}

% remove \task from the definition:
\newcommand{\felezopont}[4]{
\FPeval{\resx}{round( ( (#1)+(#3) )/2,1)}
\FPeval{\resy}{round( ( (#2)+(#4) )/2,1)}
$A(#1;#2)$, $B(#3;#4)$
{$(\resx;\resy)$}
}

% build list for the tasks environment:
\foreach \ax/\ay/\bx/\by in \pontlist {
\begingroup
\edef\x{\endgroup
}\x
}

% needed helper macros so the tasks environment actually sees \task macros:
\newcommand\switchtwo[2]{#2#1}
\newcommand\expandsecond[2]{%
\expandafter\switchtwo\expandafter{#1}{#2}%
}

}

\begin{document}

\begin{question}
Text
\end{question}

\end{document}

• Without using question environment works well. But how can I place into a question (as I wrote in my question's MWE)? \begin{question}Text of question... \expandsecond\taskslist{\begin{tasks}}\end{tasks}\end{question} doesn't work. – uzsolt Feb 17 '17 at 13:41
• Ah, yes. I know what's going wrong then. I'll update the answer as soon as I'll find the time. – cgnieder Feb 17 '17 at 15:01
• Thank you. I hope you can find a solution - my life would be easier :) – uzsolt Feb 17 '17 at 19:04
• @uzsolt see the updated answer – cgnieder Feb 18 '17 at 12:59
• Oh, great, thank you, it works well! When I have time I'll learn more about LaTeX (your solution isn't too clear for me). Thanks again! – uzsolt Feb 18 '17 at 13:23

You need to prepare the \task list in advance and deliver it expanded in the tasks environment. Here's a way to do it in with the help of xparse and expl3.

Please, avoid the obsolete package t1enc and also utf8x.

\documentclass{article}
\usepackage[T1]{fontenc}    % NOT t1enc
\usepackage[utf8]{inputenc} % NOT utf8x
\usepackage[magyar]{babel}

\usepackage{xparse,exsheets}

\ExplSyntaxOn
% function for print the coordinates from the input x1/y1/x2/y2/
\cs_new:Npn \uzsolt_felezopont:w #1/#2/#3/#4/
{
$A(#1;#2)$,~$B(#3;#4)$~$(\fp_eval:n { (#1+#3)/2 }; \fp_eval:n { (#2+#4)/2 })$
}
% macro for making the tasks environment
{
#1
}
% the variant that accepts a token list variable as its argument

% a variable for later usage

% the main macro that accepts an implicit list of points as argument
{
\clist_map_inline:Nn #1
{
}
}
\ExplSyntaxOff

\newcommand\pontlist{
2/3/1/4/,
5/1/-4/3/,
2/7/11/10/,
4/-3/5/7/,
-4/4/5/-7/,
-4/-4/4/4/,
3/7/-3/10/,
-1/-2/3/4/,
-1/3/-2/1/
}

\begin{document}

\begin{question}
Határozd meg a következő pontpárok felezőpontját!
\end{question}

\end{document}


On the other hand, if you define \pointtask with

  \clist_map_inline:nn {#1}


instead of \clist_map_inline:Nn #1, the input could be

\begin{question}
Határozd meg a következő pontpárok felezőpontját!
2/3/1/4/,
5/1/-4/3/,
2/7/11/10/,
4/-3/5/7/,
-4/4/5/-7/,
-4/-4/4/4/,
3/7/-3/10/,
-1/-2/3/4/,
-1/3/-2/1/
}
\end{question}


(no previous definition of \pontlist is necessary) which, in my opinion, would be preferable.

• The definition of \pointlist is preferable for me: I'm re-using it in the next questions (in the MWE can't see). Thanks for your answer and suggestions (utf8, t1enc) too. – uzsolt Feb 18 '17 at 14:56