11

percusse may know if I can do the following with \foreach:

\begin{document}
\foreach [
  evaluate=\x as \z using \x*10,
  evaluate=\x as \p using \x mm*3
] \x in {1,...,5}{
  \endgraf
  \hskip\p pt\relax
  \textcolor{red!\z!blue}{\x}%
}
\end{document}

Aside

\foreach should help newbie with an intelligent message in a case like the following:

\foreach [
  remember=\x % as \lastx (initially A) % n00b forgot the rest.
] \x in {B,C,D,E,F,G,H}{%
  $\overrightarrow{\lastx\x}$,
}
1
  • Just came here searching how to use multiple evaluate !
    – ygorg
    Mar 7 at 12:13

1 Answer 1

11

The evaluate keyword invokes the PGF math engine, which always saves its results in units of pt, but without the pt string. So if you use

\hskip\p pt\relax

(adding the pt after the \p), you get the desired result:

\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\foreach [
  evaluate=\x as \z using \x*20,
  evaluate=\x as \p using \x mm*3
] \x in {1,...,5}{
  \endgraf
  \hskip\p pt\relax
  \textcolor{red!\z!blue}{\x}%
}
\end{document}
5
  • 2
    Thanks. Then I just do evaluate=\x as \p using \x*3 with \hskip\p mm\relax.
    – Ahmed Musa
    Sep 17, 2012 at 13:36
  • @AhmedMusa Thanks for the direct reference. We have a very knowledgable bunch here including yourself so I'm kinda embarrassed :0).
    – percusse
    Sep 17, 2012 at 14:05
  • @AhmedMusa You want "x times 3mm", so use \x*3mm and \hskip\p pt\relax. Later, you can change 3mm to 1em or \baselineskip without any change in your \hskip... command. Sep 17, 2012 at 16:34
  • 2
    @percusse: Every tikZ expert is called percusse, like every TeX user was called Ben User by Knuth. I chose percusse over Andrew Stacey, Jake, Tom Bombadil, etc., because percusse sounds like a word.
    – Ahmed Musa
    Sep 17, 2012 at 17:45
  • @PaulGaborit: That makes some sense. Thanks.
    – Ahmed Musa
    Sep 17, 2012 at 17:47

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.