Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I apologise if I'm using and searching with incorrect terminology. This seems an obvious question (to me) so I'm surprised I've not found anything (and so assume I'm missing something).

In metapost, I can use the following to define a loop:

for i=some-value step 0.05 until some-other-value:
    fill fullcircle scaled so-and-so withcolor i*white;
    so-and-so := so-and-so - something;
    something-else := i;
endfor;

This means that I can define how large the increments should be without needing to know the initial and final value of i.

Is something similar possible in tikz?

I can achieve the same effect in a more roundabout fashion by calculating the second value for the loop from the first value plus the value of the step. However, I wonder if there is not some simpler method.

That is, the following code works:

\documentclass[border=5pt,tikz]{standalone}

\begin{document}
  \tikzset{
    /mytree/key1/.initial={-10},
    /mytree/key2/.initial={10},
    /mytree/step1/.initial={2},
  }
  \begin{tikzpicture}
    \pgfmathsetmacro{\myfirststep}{\pgfkeysvalueof{/mytree/key1} + \pgfkeysvalueof{/mytree/step1}}%
    \foreach \i in {\pgfkeysvalueof{/mytree/key1},\myfirststep,...,\pgfkeysvalueof{/mytree/key2}}
      \node at (\i,0) {\i};
  \end{tikzpicture}
\end{document}

but I wonder is it possible to replace the lines

    \pgfmathsetmacro{\myfirststep}{\pgfkeysvalueof{/mytree/key1} + \pgfkeysvalueof{/mytree/step1}}%
    \foreach \i in {\pgfkeysvalueof{/mytree/key1},\myfirststep,...,\pgfkeysvalueof{/mytree/key2}}

with something more elegant such as

    \foreach \i [step=\pgfkeysvalueof{/mytree/step1}] in {\pgfkeysvalueof{/mytree/key1},...,\pgfkeysvalueof{/mytree/key2}}

which would be analogous to the usage of step in metapost?

share|improve this question
    
You can embed your customization into a style and use it repeatedly, would that be elegant enough? :P –  percusse Jul 17 at 8:30
    
@percusse I was hoping for just a step= type option. But if I haven't missed something, then, yes, that would be elegant enough ;). Do you mean literally using .../.style... rather than .../.code...? I'm still somewhat at sea with keys in pgf/tikz! –  cfr Jul 17 at 9:16
3  
Put \pgfkeys{/pgf/foreach/custom step/.code={your first line here doing the computation}} and then use it with \foreach \i[custom step] in {\pgfstuff,\myfirststep,...,\pgfmorestuff}{...} that's what I meant. –  percusse Jul 17 at 9:18
    
@percusse Aha! I see. I will definitely try this shortly. That does, indeed, look very neat ;). Thanks! –  cfr Jul 17 at 9:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.