# How to make xshift and yshift compatible with redefined units in TikZ when using nested scopes

I am writing some code with nested scopes that have various transformations applied within them. In particular, I have nested scopes of the form \begin{scope}[x={(a,b)},y={(c,d)}] redefining the units of the coordinate system, as well as transformations of the form \begin{scope}[xshift=a,yshift=b].

Now, either of these things seem to commute with themselves nicely; if I try

\begin{scope}[x={(2,0)},y={(0.5,2)}]
%code A
\begin{scope}[x={(2,0)},y={(0.5,2)}]
%code B
\end{scope}
\end{scope}


then code A will be drawn with an x-vector of (2,0) and a y-vector of (1,2). (I think this is because the y={(0.5,2)} comes after the redefinition of x, and so the 0.5 coefficient is applied to the x vector.) Meanwhile, code B will be drawn with an x-vector of (4,0) and a y-vector of (4,4), which is what I would expect from composing these two transformation matrices.

Likewise, composing shift transformations acts how I would expect.

Where I run into trouble, though, is combining the two.

\newcommand{\exampleshape}{
\draw[fill=red!20](0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
\draw[fill=green!20](0,0) -- (1,0) -- (1,0.5) -- cycle;
}
\begin{tikzpicture}

\draw (0,0) grid (3,3);
\exampleshape

\begin{scope}[y={(0,2)}]
\begin{scope}[yshift=1cm]
\exampleshape
\end{scope}
\end{scope}


I would expect that the second scope here "believes" y to be the vector (0,2), and so applying a yshift of 1 should shift the picture up by 2 units in the underlying coordinate system. Instead, it seems that the yshift is performed relative to absolute coordinates.

Why doesn't this happen, and what should I do to get the desired behavior? (Obviously in this case I can manually adjust things, but in practice I am working with much more complicated nested scopes and this is not so feasible.)

In TikZ there are two types of operations: some are affected by transformations, some are not. The former are for the general drawing, the latter are for adjustments, labels etc. xshift and yshift belong to the second group, while shift belongs the first one. So, instead of yshift=1cm, use shift={(0,1)}.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\exampleshape}{
\draw[fill=red!20](0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
\draw[fill=green!20](0,0) -- (1,0) -- (1,0.5) -- cycle;
}
\begin{tikzpicture}
\draw (0,0) grid (3,3);
\exampleshape
\begin{scope}[y={(0,2)},shift={(0,1)}]
\exampleshape
\end{scope}
\end{tikzpicture}
\end{document}