4

It is my understanding that the shading in the following code is applied after the scope transformation.

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\pgfdeclareverticalshading{myshade}{2cm}{
    color(0cm)=(red);
    color(.3cm)=(orange);
    color(.6cm)=(yellow);
    color(.9cm)=(green);
    color(1.2cm)=(blue);
    color(1.5cm)=(purple);
    color(1.8cm)=(brown)
}

\begin{tikzpicture}
\begin{scope}[rotate=26.73]
    \draw [shading=myshade] (0, 0) circle (1);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}
    \draw [shading=myshade] (0, 0) circle (1);
\end{scope}
\end{tikzpicture}

\end{document}

Given that a circle is symmetrical by rotation, the result of both tikzpictures should be the same. Yet the shading is different. Why is this the case?

enter image description here

2
  • The shading is done on the basis of the bounding box. A circle is constructed from Bezier curves, and this means that its bounding box changes under rotations. As you can see, the circles are not vertically aligned either, for the same reason. – user194703 Apr 17 '20 at 16:25
  • Related? – user108724 Apr 17 '20 at 16:30
3

The shading is done on the basis of the bounding box, see e.g. here. A circle is constructed from Bezier curves, and this means that its bounding box changes under rotations. As you can see, the circles are not vertically aligned either, for the same reason. Once you fix the bounding box, the effect is gone.

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{bbox}
\begin{document}
\pgfdeclareverticalshading{myshade}{2cm}{
    color(0cm)=(red);
    color(.3cm)=(orange);
    color(.6cm)=(yellow);
    color(.9cm)=(green);
    color(1.2cm)=(blue);
    color(1.5cm)=(purple);
    color(1.8cm)=(brown)
}

\begin{tikzpicture}[bezier bounding box]
\begin{scope}[rotate=26.73]
    \draw [shading=myshade] (0, 0) circle (1);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[bezier bounding box]
\begin{scope}
    \draw [shading=myshade] (0, 0) circle (1);
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

If you want to rotate the shading, change the shading angle. (You can also use transform canvas but this is sometimes a bit hard to tame.)

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{bbox}
\begin{document}
\pgfdeclareverticalshading{myshade}{2cm}{
    color(0cm)=(red);
    color(.3cm)=(orange);
    color(.6cm)=(yellow);
    color(.9cm)=(green);
    color(1.2cm)=(blue);
    color(1.5cm)=(purple);
    color(1.8cm)=(brown)
}

\begin{tikzpicture}[bezier bounding box]
\path (0, 0) circle[radius=1];
\begin{scope}[transform canvas={rotate=26.73}]
    \draw [shading=myshade] (0, 0) circle[radius=1];
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[bezier bounding box]
\begin{scope}
    \draw [shading=myshade,shading angle=26.73] (0, 0) circle[radius=1];
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[bezier bounding box]
\begin{scope}
    \draw [shading=myshade] (0, 0) circle[radius=1];
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

5
  • I think the OP's question is about rotating the inside color and not about bounding box! Isn't? – user108724 Apr 17 '20 at 16:34
  • 1
    @C.F.G The question reads: "Given that a circle is symmetrical by rotation, the result of both tikzpictures should be the same. Yet the shading is different. Why is this the case?". – user194703 Apr 17 '20 at 16:35
  • Can bezier bounding box be used with a scope, or does it have to apply to the entire tikzpicture? – usernumber Apr 17 '20 at 19:58
  • @usernumber It can (and should) be applied in a scope. The library is not perfect at this point. – user194703 Apr 17 '20 at 20:12
  • Note that the bbox library was removed from TikZ with version 3.1.6 due to licensing issues: github.com/pgf-tikz/pgf/releases/tag/3.1.6 – Torbjørn T. Sep 28 '20 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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