I'm finding some weird behavior with TikZ's \foreach within an \begin{axis} environment.
This works:
\foreach \x in {-2,-1,...,2} {
\addplot[thick, domain=0.05+(\x*2-1)*pi/2:+(\x*2+1)*pi/2-0.05] (x, {tan(deg(x))});
}
but this doesn't:
\foreach \x in {-2,-1,...,2} {
\draw (\x,-5) -- (\x,5);
}
It gives the error
! Undefined control sequence.
<argument> \x
,-5
l.771 \end{axis}
Here's the really weird part: it only fails when the axis has defined bounds. For example, here's a MWE that works (but doesn't draw anything):
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} % [ ymin=-3, ymax=3, xmin=-5, xmax=5]
\foreach \q in {-2,-1,...,2} {
\draw (axis cs: \q,-5) -- (axis cs: \q,5);
}
\end{axis}
\end{tikzpicture}
\end{document}
but removing the comment causes it to fail.
Why is this and how can I prevent it?
(My end goal is to draw five segments of the tangent function and their verical symptotes.)

n*pi/2because of floating point errors. In any case, I got the tangent function to plot fine with a for loop; it's the lines that are causing trouble. – wchargin Mar 16 '14 at 20:30