3

I am trying to do a simple calculation with the tikz foreach, but it does something very suprising:

\documentclass{article} 
\usepackage{tikz} 
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\begin{document} 
\begin{preview}
\def\radius{10}
\def\step{6}

\begin{tikzpicture}
\def\stepsize{\number\numexpr180/\step\relax}

\foreach \Angle in {0.5\stepsize,1.5\stepsize,...,180} {\node [right] at (\Angle:10) {\Angle};}

\end{tikzpicture}
\end{preview}
\end{document}

From this I would expect on ouput going from 15,45,...,180, but the output is the following. enter image description here

This is a very,very small stepsize. If I write 0.5*\stepsize it tells me it is an illegal unit. Of course I could solve it by defining a half and a 1.5 step-size. Any ideas why this is happening?

4

The problem comes from the concatenation of .5 and \stepsize, that make .530 then 1.530.

enter image description here

\documentclass{article} 
\usepackage{tikz} 
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\begin{document} 
\begin{preview}
\def\radius{10}
\def\step{6}


\def\stepsize{\number\numexpr180/\step\relax}

\pgfmathtruncatemacro{\first}{0.5*\stepsize}
\pgfmathtruncatemacro{\step}{1.5*\stepsize}

\stepsize

\begin{tikzpicture}

\foreach \Angle in {\first,\step,...,180} {\node [right] at (\Angle:10) {\Angle};}

\end{tikzpicture}
\end{preview}
\end{document}

With evaluate

enter image description here

\documentclass{article} 
\usepackage{tikz} 
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\begin{document} 
\begin{preview}
\def\radius{10}
\def\step{30}

\def\First{15}
\pgfmathtruncatemacro{\NbStep}{(180-\First)/\step}


\begin{tikzpicture}

\foreach \i [evaluate=\i as \Angle using int(\First+\step*\i)]
    in {0,1,...,\NbStep} {%
    \node [right] at (\Angle:\radius) {\Angle} ;
}

\end{tikzpicture}
\end{preview}
\end{document}
  • Wow, I thought that it would make a calculation there. Is there a way to force an evaluation inside the foreach or I should \def everything I want to use in there? – fbence Aug 7 '15 at 8:31
  • Yes there is an evaluate possibility, I'll post it in a minute. – Tarass Aug 7 '15 at 8:33
  • You can't evaluate foreach parameter inside the foreach loop. It has to done before. – Tarass Aug 7 '15 at 8:45

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.