2

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!
  \begin{tasks}
    \foreach \ax/\ay/\bx/\by in \pontlist {
      \felezopont{\ax}{\ay}{\bx}{\by}
    }
  \end{tasks}
\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
1

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:
\newcommand*\taskslist{}
\foreach \ax/\ay/\bx/\by in \pontlist {
  \begingroup
  \edef\x{\endgroup
    \expandafter\noexpand\csname g@addto@macro\endcsname
    \noexpand\taskslist{\noexpand\task\noexpand\felezopont{\ax}{\ay}{\bx}{\by}}%
  }\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
{\begin{tasks}}
\end{tasks}

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{%
  \expandsecond\taskslist
  {\begin{tasks}}
  \end{tasks}%
}

and then

\begin{question}
  Text
  \printtaskslist
\end{question}

enter image description here

Complete code:

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

\usepackage{exsheets,tasks}
\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:
\newcommand*\taskslist{}
\foreach \ax/\ay/\bx/\by in \pontlist {
  \begingroup
  \edef\x{\endgroup
    \expandafter\noexpand\csname g@addto@macro\endcsname
    \noexpand\taskslist{\noexpand\task\noexpand\felezopont{\ax}{\ay}{\bx}{\by}}%
  }\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}%
}

\newcommand*\printtaskslist{%
  \expandsecond\taskslist
  {\begin{tasks}}
  \end{tasks}%
}

\begin{document}

\begin{question}
  Text
  \printtaskslist
\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
1

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
\cs_new_protected:Nn \uzsolt_pointtask:n
 {
  \begin{tasks}
  #1
  \end{tasks}
 }
% the variant that accepts a token list variable as its argument
\cs_generate_variant:Nn \uzsolt_pointtask:n { V }

% a variable for later usage
\tl_new:N \l_uzsolt_pointtask_tl

% the main macro that accepts an implicit list of points as argument
\NewDocumentCommand{\pointtask}{m}
 {
  \tl_clear:N \l_uzsolt_pointtask_tl
  \clist_map_inline:Nn #1
   {
    \tl_put_right:Nn \l_uzsolt_pointtask_tl { \task \uzsolt_felezopont:w ##1 }
   }
  \uzsolt_pointtask:V \l_uzsolt_pointtask_tl
 }
\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!
\pointtask\pontlist
\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!
\pointtask{
  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.

enter image description here

  • 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

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.