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}
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}
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


\tikzcommand). 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:04tikzpicture. So at least some of what you'd hoped to do inside that environment is possible. (Just as you can set, say,ball colorto different values within the picture.) – cfr Aug 05 '23 at 17:12