15

I'm trying to add fadings on arrows in a TikZ picture, but when I add a fading on a vertical arrow, the arrow tip partly disappears.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
    \draw [->] (0,0) -- (0,1);
    \draw [->, path fading=south] (1,0) -- (1,1);
\end{tikzpicture}
\end{document}

Why does this happen? Is there another method to add fadings on arrows?

Vincent
  • 20,157

2 Answers2

17

I do not know why it happens for vertical/near vertical arrows. A workaround would be to draw the arrowhead before anything else like this:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings, decorations.markings}
\begin{document}
\begin{tikzpicture}
\draw [->] (0,0) -- (0,1);
\draw[
  decoration={markings,mark=at position 1 with {\arrow{>}}},
  preaction={decorate},
  shorten >=0.4pt, path fading=south,
  ] (1,0) -- (1,1);
\end{tikzpicture}
\end{document}

Normal arrow and fading arrow

Edit:

I found that by inserting an extra point at the beginning of the path, the bounding box for the fading is expanded, without drawing anything. The extra point can be a repeat of the start point with an offset both in x and y, so that it works both for (near) vertical and (near) horizontal arrows:

\documentclass[tikz, border=1cm]{standalone}\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
    \draw [->] (0,0) -- (0,1);
    \draw [->, path fading=south] ([xshift=5pt, yshift=5pt]1,0) (1,0) -- (1,1);
\end{tikzpicture}
\end{document}

Arrow and faded arrow

5

This is a bug analysis, I don't have a solution. I attempt to answer the following two problems:

  • Why is there a clipping at all?
  • Why is there a clipping when the line is vertical/horizontal, but not when the line is less boring?

Why clipping

It is, in fact, not a clipping. It's the natural boundary of the fading/shading. See the explanation after \pgfshadepath. Or let me summarize as follows: a fading/shading is essentially a window and a curtain. Normally you would buy a curtain large enough to cover the entire window. Otherwise, people passing by your window see inside your room.

But there is a caveat when computing the necessary size of the curtain. For example, if you have a 50bp x 50bp window, you would expect that a 50.1bp x 50.1bp curtain is definitely large enough. But that's not right. Because some people want the curtain to be installed with a 45° rotation. So the curtain ends up covering an octagonal region but leaving out four small corners.

relation between window and "fit" curtain

So the package authors outsmart people by ordering oversize curtains. The curtain is translated and rescaled so that the window will locate at the center, covering 25% of the area.

enlarged curtain

Sometimes, we see fat and short windows. Then the curtain is also scaled along the proper axis

unproportionally rescaled curtain

Why is horizontal and vertical arrows problematic

Actually, all arrows are problematic because the arrow tips do not count toward the bounding box. More precisely,

  • The default arrow tip of TikZ does not have a bounding box (convex hull).
  • That said, the default arrow tip of \usetikzlibrary{arrows.meta} does have a bounding box (convex hull).
  • That said, the bounding box of an arrow tip is not available until the very end of the output routine.

You see, the arrows are added by the command \pgfusepath and this command is executed only after you have done everything with nodes, decorations, double, and perhaps shading and fading. In fact, the magic node current path bounding box has never included arrow tips. (see the definition of \pgf@arrow@update@bb.)

So when you use path fading with arrow tips, the tips do not necessarily lie inside the window, despite that usually they are still covered by the curtain because there were some safety margin.

shrinking windows and arrow tips bleed

Symbol 1
  • 36,855