# Can a transformation be made to apply to the rest of the commands in a TikZ picture?

One nice feature of TikZ is that you can apply a coordinate transformation in the middle of a \draw command (or \path, etc.) and it will automatically apply to the rest of whatever is in the command. I'd like to be able to use a command inside a tikzpicture that similarly applies a transformation automatically to all subsequent \draw commands, i.e., that lasts across semicolons.

For instance, consider the following working example:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.5]
\foreach \n in {1,...,5}
{
\draw[thick,->] (-1.5,0) -- (4.5,0) node[right] {$x$};
[yshift=-1cm];
}
\end{tikzpicture}
\end{center}
%
%
\rule{0pt}{2cm}
%
%
\begin{center}
\begin{tikzpicture}[scale=1.5]
\foreach \n in {0cm,1cm,...,4cm}
{
\draw[yshift=\n, thick, ->] (-1.5,0) -- (4.5,0) node[right] {$x$};
}
\end{tikzpicture}
\end{center}
\end{document}


The second picture gives the result I want; the first picture is the style of code I would like to use for it.

Is there a way to do this?

-
Is the only reason for doing this the fact that you don't want to start the \foreach at 0, and have to include the cm in the list for \foreach? Would be helpful to know why the second code style won't work for you. –  Peter Grill Feb 3 '12 at 20:38
To me, the natural way to think about this instruction is "do some stuff, move down (or move right, or rotate a certain number of degrees, or...), and then repeat x number of times." The first coding style is a much more natural expression of this than the second, at least in my view. –  Charles Staats Feb 3 '12 at 21:51
The sort of thing I would really use this for is more like stacking several different pictures on top of, or beside, one another in the same tikzpicture environment. I've been using nested occurrences of things like \begin{scope}[yshift=-1cm]...\end{scope} to achieve this effect, but I found this an unaesthetic way of coding it. The use of \foreach seemed like an effective way to demonstrate why the "nested scope" approach is less than optimal. –  Charles Staats Feb 3 '12 at 21:57
@Peter Grill Another thing is that if one of the transforms down should be increased (in the case of the for loop, probably by using an "if" statement of some sort to pick out a particular value of \n), all the subsequent ones should fall into line automatically. It seems to me that the first coding style better incorporates this sort of flexibility. –  Charles Staats Feb 3 '12 at 22:04

One way of applying a transformation to all subsequent objects in a tikzpicture is to use \every path/.append style={<transformation command>}, which accumulates the options you pass to it. To reset it (it is initially empty), you can call \tikzset{every path/.style={}}.

However, in your case there's an additional problem: \foreach loops group the commands, so changes to the every path style are limited to each iteration of the loop. If you're still set on using your approach, you will need to ungroup the loop. The pgfplots package provides a \pgfplotsforeachungrouped command, that works like the normal \foreach in every way except for the fact that it doesn't introduce groups.

\documentclass{article}
\usepackage{tikz, pgfplots}

\begin{document}
\begin{center}

\begin{tikzpicture}[scale=1.5]
{
\pgfplotsforeachungrouped \n in {1,...,5}
{
\draw[thick,->] (-1.5,0) -- (4.5,0) node[right] {$x$};
\tikzset{every path/.append style={yshift=-1cm}}
}
}
\end{tikzpicture}
\end{center}
\end{document}

-
Great answer! (You probably put more effort into this than the question deserved, but I feel like I understand tikz better for reading it.) –  Charles Staats Feb 4 '12 at 4:20