LaTeX TikZ Option execute at end picture

Nov. 09, 2025 in Buffalo, United States • Updated Nov. 15, 2025

There is a LaTeX TikZ option execute at end picture1:

/tikz/execute at end picture=⟨code⟩

This option installs ⟨code⟩ that will be executed at the end of the picture. Using this option multiple times will cause the code to accumulate. This option must also be given in the optional argument of the {tikzpicture} environment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds}

\begin{document}
	
\begin{tikzpicture}[execute at end picture=%
	{
		\begin{pgfonlayer}{background}
			\path[fill=yellow,rounded corners]
			(current bounding box.south west) rectangle
			(current bounding box.north east);
		\end{pgfonlayer}
	}]
	\node at (0,0) {X};
	\node at (2,1) {Y};
\end{tikzpicture}

\end{document}

png-1

By the way, there is another similar option execute at begin picture1:

/tikz/execute at begin picture=⟨code⟩

This option causes ⟨code⟩ to be executed at the beginning of the picture. This option must be given in the argument of the {tikzpicture} environment itself since this option will not have an effect otherwise. After all, the picture has already ā€œstartedā€ later on. The effect of multiply setting this option accumulates.

This option is mainly used in styles like the every picture style to execute certain code at the start of a picture.

but if we use it in the above case, we can’t get a normal output.


References