I have a bunch of variables defined with similar names, e.g.:


All variables have a name wsteps + a letter, and I want to iterate over them using tikz's foreach:

\foreach \v in {A, ..., F} {
    \expandafter\let\expandafter\thisSteps\csname wsteps\v\endcsname
    \foreach \s/\e/\d in {\thisSteps} {
        % Do something...

But this gives me an error:

Package PGF Math Error: You've asked me to divide '0.5' by '0', but I cannot divide any number by '0' (in '1/2/0').

But if I do not use a variable thisSteps, this works fine:

\foreach \s/\e/\d in {1/2/0,2/3.5/1} {


Is there a way to make the first snippet working?

  • 2
    You should use \foreach \s/\e/\d in \thisSteps without {} – Kpym Apr 13 '18 at 13:00
  • @Kpym Forget my previous command, this works within a MCVE, so the new error is somewhere else. You can post this as an answer if you want. – Holt Apr 13 '18 at 13:05
  • @Kpym Care for an answer? – percusse Jul 30 '18 at 18:08
  • @percusse I'm on vacation, so if somebody can answer it , I'll be ok with that :) – Kpym Jul 30 '18 at 18:16

When the iterators are given to a foreach macro it treats the contents as a grouped entity. That is to say wrapped around a curly brace. Here because you add extra {} TikZ tries to see where the comma is but can't find it and decides that \thisSteps is the only element and the loop spins only once assigning \s, to 1/2/0 which results in divide by zero.

Hence as Kpym (aka TikZer Woods) mentioned you have to strip off the braces for TikZ to expand it and check its contents.

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.