[Tex/LaTex] How to use shading in TikZ when rotating the picture

pdftextikz-pgf

I have the following problem: I want to draw a rectangle and around it, there should be some smooth fading to the background (white). I found the shading technique of tikz quite interesting. The problem is, that it does not behave as I suspect. See the following example:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{scopes,fadings}
\begin{document}
\begin{tikzpicture}
  \shade [top color=white,bottom color=red,shading angle=90] (-1,-1) rectangle (1,1);
  \filldraw [color=red,xshift=2cm] (-1,-1) rectangle (1,1);
  \shade [top color=red,bottom color=white,shading angle=90,xshift=4cm] (-1,-1) rectangle (1,1);
  { [ xshift=10cm,rotate=-30]
    \shade [top color=white,bottom color=red,shading angle=60] (-1,-1) rectangle (1,5);
    \filldraw [color=red,xshift=2cm] (-1,-1) rectangle (1,5);
    \shade [top color=red,bottom color=white,shading angle=60,xshift=4cm] (-1,-1) rectangle (1,5);
  }
\end{tikzpicture}
\end{document}

I for me get using pdflatex two blocks. The left is drawn correctly but (clearly) not rotated. In the right picture the colors at the ends of the fadings are not set correctly: I can see a difference between the white background and the nearly white upper left end. Also in the middle at the conjunction with the red square the color is not correct.

Can you tell me what I am doing wrong?

Best Answer

The problem is that the figures get rotated first and then the shading is applied; to obtain the desided effect, you can apply a canvas transformation (See Section 22.4 Canvas Transformations of the pgf manual):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes,fadings}

\begin{document}
\begin{tikzpicture}[transform canvas={rotate=-30}]
    \shade [top color=white,bottom color=red,shading angle=60] (-1,-1) rectangle (1,5);
    \filldraw [color=red,xshift=2cm] (-1,-1) rectangle (1,5);
    \shade [top color=red,bottom color=white,shading angle=60,xshift=4cm] (-1,-1) rectangle (1,5);
\end{tikzpicture}

\end{document}

enter image description here