13

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?

1 Answer 1

12

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

2
  • Note that if you do this, you almost certainly want to still have shading angle=90 in the rotated block; as it stands, the code tries to rotate the shading 30° twice, and that gives the shading the odd "twisted" look it has in the picture. Commented Dec 15, 2011 at 22:09
  • I think canvas transformations is section 25.4 of the pgf manual.
    – usernumber
    Commented Apr 17, 2020 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .