I am trying to collect into a command called \drawtasks
a series of \drawtask
commands that call TikZ. Every time I call \wptask
, the command should increment a counter and append to \drawtasks
a call to \drawtask
with the current value counter. Unfortunately, when I finally call \drawtasks
, I always get the last counter value. I've tried out solutions proposed in similar questions on here (e.g., here, but it just doesn't work: it seems that my problem is different in that I want to append a call to another command which should not be expanded. Here is an example:
\documentclass{article}
\usepackage{tikz}
\newcommand{\drawtask}[4]{
\node[anchor=west] at (-2,-0.5 * #1 - 0.25) {Task #2} ;
\filldraw[fill=green!20,draw=green!50!black] (#3 / 3,-0.5 * #1 - 0.15) rectangle (#4 / 3,-0.5 * #1 - 0.35) ;
}
\newcounter{wptaskcounter}
\setcounter{wptaskcounter}{0}
\newcommand{\drawtasks}{}
\makeatletter
\newcommand{\appendwptask}[4]{%
\protected@edef\@tempa{#1}%
\expandafter\g@addto@macro\expandafter\drawtasks\expandafter{%
\drawtask{\@tempa}{#2}{#3}{#4}%
}%
}
\makeatother
\newcommand{\wptask}[3]{%
\appendwptask{\thewptaskcounter}{#1}{#2}{#3}%
\stepcounter{wptaskcounter}%
}
\wptask{1.1}{0}{3}
\wptask{1.2}{3}{6}
\wptask{1.3}{6}{9}
\begin{document}
\begin{tikzpicture}
\drawtasks
\end{tikzpicture}
\end{document}
I'm really confused with the expansion rules of LaTeX and would really appreciate some help! Thanks in advance.
\drawtask
before the{}
whereas you want to expand\@tempa
and have\drawtask
not be expanded, don't you?\expandafter
just skips the next thing, which is the{
, and expands the thing after, which is\drawtask
. At least, that's what egreg says. I asked in a comment on the answer you linked to.