6

When answering this question, I found out that the shading angle key does not do what I expected it to do. The following MWE illustrates the problem. I've defined a shading with a sharp edge to make it easier to see its direction.

\documentclass[tikz,margin=5pt]{standalone}

\begin{document}
    \pgfdeclareverticalshading{myshading}{100bp}{color(0bp)=(red); color(49bp)=(red); color(51bp)=(green); color(100bp)=(green)}

    \begin{tikzpicture}[scale=2]
        \draw[shading=myshading,shading angle=15]
                (0:1)
            arc (0:30:1)
            --  (30:1.2)
            arc (30:0:1.2)
            -- cycle;
        \draw (0,0) -- (15:1.2);
    \end{tikzpicture}
\end{document}

This is the result. Note how the transition in the shading and the line are not parallel, even though the angle of the line and shading angle are both set to 15 degrees.

"wrong" result

This is what I would have expected:

exepected result

The pgfmanual's section about shading a path doesn't really explain how this key works.

5

Here's how I now think the key works from my own experiments, illustrated in five steps:

steps used for explanation

  1. First, the selected shading is applied to a square canvas
  2. Then, the shading angle key is applied.
  3. The canvas is then resized to the size of the bounding box of the path that is to be shaded. If the result is non-square, this changes the angle of the gradient!
  4. The canvas is moved so it's under the path.
  5. Finally, it is clipped to lie entirely within the path.

Of course, none of the intermediate steps are actually drawn. In case anyone cares, here is how I produced the image:

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{calc}

\begin{document}
    \pgfdeclareverticalshading{myshading}{100bp}{color(0bp)=(red); color(49bp)=(red); color(51bp)=(green); color(100bp)=(green)}

    \begin{tikzpicture}[scale=2]
        \newcommand*\outline[1][draw]{
            \path[#1]
                   ($(c) + (0:1)$)    coordinate (p1)
                arc (0:30:1)           coordinate (p2)
                --  ($(c) + (30:1.2)$) coordinate (p3)
                arc (30:0:1.2)         coordinate (p4)
                -- cycle;
            \coordinate (sw) at (p1 -| p4);
            \coordinate (ne) at (p2 |- p3);
            \coordinate (center) at ($(sw)!.5!(ne)$);
            \fill (c) circle (1pt);
            \path[#1] (c) -- ($(c) + (15:1.2)$);
        }

        \coordinate (c) at (0,0);
        \shade[shading=myshading] (c) +(-.5,-.5) rectangle +(.5,.5);
        \outline
        \node[below] at (c) {1};

        \coordinate (c) at (2,0);
        \shade[shading=myshading,shading angle=15] (c) +(-.5,-.5) rectangle +(.5,.5);
        \outline
        \node[below] at (c) {2};

        \coordinate (c) at (4,0);
        \outline[]
        \shade[shading=myshading,shading angle=15] ($(sw) - (center) + (c)$) rectangle ($(ne) - (center) + (c)$);
        \outline
        \node[below] at (c) {3};

        \coordinate (c) at (1,-2);
        \outline[]
        \shade[shading=myshading,shading angle=15] (sw) rectangle (ne);
        \outline
        \node[below] at (c) {4};

        \coordinate (c) at (3,-2);
        \outline[draw,shading=myshading,shading angle=15]
        \node[below] at (c) {5};
    \end{tikzpicture}
\end{document}

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.