# tikz \foreach dynamic list with environment arguments

I'm making an axis environment that recieves 5 arguments (the first is optional and it's 1 by default). To make the x marks on the x axis I use

\foreach \x in {#2,...,#4}
{\ifthenelse{\x = 0}{}{\draw [shift={(\x,0)}] (0,1pt)--(0,-1pt);\draw (\x,0.5mm) node[anchor=north,font=\tiny] {$\x$};}}


and works perfect, but it's not flexible: it labels all integers between #2 and #4; so i write this in an atempt to make it not label all the numbers:

\foreach \x in {#2,#2+#1,...,#4}%#1 is the optional argument and by default is 1.
{\ifthenelse{\x = 0}{}{\draw [shift={(\x,0)}] (0,1pt)--(0,-1pt);\draw (\x,0.5mm) node[anchor=north,font=\tiny] {$\x$};}}


but I get:

Missing = inserted for \ifnum."


I also tryed:

\foreach \x [evaluate=\x as \xeval using \evaluar{\x}] in {#2,#2+#1,...,#4}
{\ifthenelse{\x = 0}{}{\draw [shift={(\x,0)}] (0,1pt)--(0,-1pt);\draw (\x,0.5mm) node[anchor=north,font=\tiny] {$\x$};}}


where

\newcommand{\evaluar}[1]{\pgfmathparse{int(#1)} \pgfmathresult}


and getting:

Incomplete \iffalse; all text was ignored after line 121.


I tryed a lot of combinations but LaTeX always complains about something. Any idea of what can I do?

Thank you.

PD: I'm from Argentina, so sorry about my writing if I have some mistakes.

-
Please add to your question a complete minimal document illustrating the definition of your environment. –  Gonzalo Medina Sep 18 '12 at 2:29

If only integer numbers are involved, you can use \numexpr (in my example I used an \axis command with just three arguments: the mandatory second and third arguments control the range and the optional first argument gives the "step"):

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}

\newcommand\axis[3][1]{%
\foreach \x in {#2,\the\numexpr#2+#1\relax,...,#3}
{\ifthenelse{\x = 0}{}{%
\draw [shift={(\x,0)}] (0,1pt)--(0,-1pt);
\draw (\x,0.5mm) node[anchor=north,font=\tiny] {$\x$};}}}

\begin{document}

\begin{tikzpicture}
\axis{2}{13}
\end{tikzpicture}

\begin{tikzpicture}
\axis[2]{2}{13}
\end{tikzpicture}

\begin{tikzpicture}
\axis[3]{2}{13}
\end{tikzpicture}

\begin{tikzpicture}
\axis[5]{2}{13}
\end{tikzpicture}

\end{document}


-
Given that \ifthenelse is expecting an integer, the argument has to be an integer. An alternative would be to do the computation beforehand with \pgfmathtruncatemacro\temp{#1+#2} and then use \temp as the second argument in the list. –  Loop Space Sep 18 '12 at 8:56

I don't see the need for ifthen package in the MWE you gave. Also, a look at tikZ's internal parsing shows that the \relax after \the\numexpr#2+#1 isn't necessary.

\newcommand\axis[3][1]{%
\foreach \x in {#2,\the\numexpr#2+#1,...,#3}{%
\ifnum\x=0\relax\else
\draw [shift={(\x,0)},color=red]
(-1.5pt,0) -- (1.5pt,0) (0,-1.5pt) -- (0,1.5pt);
\draw (\x,0.5mm) node[anchor=north,font=\normalfont] {$\mathbf{\x}$};
\fi
}%
}


I think there is a problem with Medina's and my results. Since the last point is always 13, why is 13 not shown in some of the lines in the output?

Because of this observation, I have recomputed the result with my \newforeach and obtained the following output.

\newcommand\axis[3][3]{%
\newforeach \x in {#2,\the\numexpr#2+#1,...,#3}{%
\ifnum\x=0\relax\else
\draw [shift={(\x,0)},color=red]
(-1.5pt,0) -- (1.5pt,0) (0,-1.5pt) -- (0,1.5pt);
\draw (\x,0.5mm) node[anchor=north,font=\normalfont] {$\mathbf{\x}$};
\fi
}%
}


Here is another test:

\newcommand\axis[3][5]{%
\newforeach [
count in = \ci all \x satisfying \ifnum\x>4\fi initially 0,
remember = \x as \lastx initially 1
]
\x in {#2,\the\numexpr#2+#1,...,#3}{%
\ifnum\x=0\relax\else
\draw [shift={(\x*.9,0)},color=red]
(-1.5pt,0) -- (1.5pt,0) (0,-1.5pt) -- (0,1.5pt);
\draw (\x*.9,0.4mm) node[anchor=north,font=\small] {$\mathbf{\lastx,\x}$};
\ifforeachlastitem
\draw (\x,0.4mm) node[anchor=north,font=\small,blue] {$>4:\ci$};
\fi
\fi
}%
}


-