Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

migrated from stackoverflow.com Dec 15 '11 at 19:08

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 8 down vote accepted

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

share|improve this answer
    
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. –  Antal S-Z Dec 15 '11 at 22:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.