1

I am using tikz with the tikz library "fadings". I am drawing a tikzpicture that involves looping through a sequence, and performing an action for each term of the sequence. One such action involves setting a tikzfading style. This seems to necessitate setting the tikzfading style from within the tikzpicture, but I noticed that doing so dramatically increases the size of the boundary box of my tikzpicture.


Here I will demonstrate a MWE of sorts.

If I set the tikzfading style from outside the tikzpicture, the bounding box is not impacted:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}

\tikzfading[name=myfade, outer color=transparent!0, inner color=transparent!100]

\begin{tikzpicture}
    \fill[path fading=myfade] (0,0) rectangle (2,2);
    \draw[blue,dashed] (current bounding box.north west) rectangle (current bounding box.south east);
\end{tikzpicture}

\end{document}

enter image description here

However, simply moving the setting of the tikzfading style to within the tikzpicture causes the bounding box to seemingly double in size:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}

\begin{tikzpicture}
    \tikzfading[name=myfade, outer color=transparent!0, inner color=transparent!100]
    \fill[path fading=myfade] (0,0) rectangle (2,2);
    \draw[blue,dashed] (current bounding box.north west) rectangle (current bounding box.south east);
\end{tikzpicture}

\end{document}

enter image description here

Note: The second picture makes it look like the fading square gets smaller. This is just an illusion due to having to screenshot a larger area in the second picture.


Does anyone know what is causing this? I have read around a bit and it seems that the tikzfading library may use some extra control points in order to calculate the fading, and these control points get included within the bounding box. But if this is the case, I am not sure why this effect is dependent on setting the style from within or without the tikzpicture.

In any case, I am also interested in a work around that allows me to set tikzfading styles from within the picture (so that I can dynamically set them) without messing up the boundary box of the picture. Thanks

  • 3
    fadings are basically small tikzpictures (if you look at the code you will find a \tikz command). That means if you use it inside a tikzpicture you are nesting pictures and you shouldn't do that. See https://tex.stackexchange.com/a/46792/2388 – Ulrike Fischer Aug 03 '23 at 12:04
  • 2
    Possibly related: https://tex.stackexchange.com/questions/277784/document-class-standalone-and-tikzfading-tikzfadingfrompicture – John Kormylo Aug 03 '23 at 13:01
  • 1
    Both linked questions contained incredibly useful information. Thank you. – Adam Higgins Aug 03 '23 at 13:05
  • Note that you can parametise the fading definition and set the parameters within a tikzpicture. So at least some of what you'd hoped to do inside that environment is possible. (Just as you can set, say, ball color to different values within the picture.) – cfr Aug 05 '23 at 17:12

0 Answers0