2

Individually setting the background for each TikZ picture works:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,fit,calc,backgrounds,shapes.geometric}

\tikzset{%
node distance=2em, auto,
empty/.style={draw=black, circle, fill=white, minimum size=2.5em, inner sep=0.1ex},
filled/.style={draw=black, circle, fill=black!15, minimum size=2.5em, inner sep=0.1ex},
arrow/.style={->, -{Stealth[length=.6em, inset=0pt]}, line width=0.7pt},
}

\begin{document}
\begin{tikzpicture}[background rectangle/.style={fill=black!30,rounded corners}, show background rectangle]
\node[filled] (x) {$x$};
\node[empty, right=of x] (y) {$y$};
\path (x) edge[arrow] node {} (y);
\end{tikzpicture}
\end{document}

background working

Moving the background rule into the \tikzset{}, the background does not work anymore:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,fit,calc,backgrounds,shapes.geometric}

\tikzset{%
background rectangle/.style={fill=black!30,rounded corners}, show background rectangle,
node distance=2em, auto,
empty/.style={draw=black, circle, fill=white, minimum size=2.5em, inner sep=0.1ex},
filled/.style={draw=black, circle, fill=black!15, minimum size=2.5em, inner sep=0.1ex},
arrow/.style={->, -{Stealth[length=.6em, inset=0pt]}, line width=0.7pt},
}

\begin{document}
\begin{tikzpicture}
\node[filled] (x) {$x$};
\node[empty, right=of x] (y) {$y$};
\path (x) edge[arrow] node {} (y);
\end{tikzpicture}
\end{document}

background not working

1 Answer 1

6

I think you are looking for

every picture/.append style={background rectangle/.style={fill=black!30,rounded corners}, show background rectangle}

as in

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,fit,calc,backgrounds,shapes.geometric}

\tikzset{%
node distance=2em, auto,
empty/.style={draw=black, circle, fill=white, minimum size=2.5em, inner sep=0.1ex},
filled/.style={draw=black, circle, fill=black!15, minimum size=2.5em, inner sep=0.1ex},
arrow/.style={->, -{Stealth[length=.6em, inset=0pt]}, line width=0.7pt},
every picture/.append style={background rectangle/.style={fill=black!30,rounded corners}, show background rectangle}
}

\begin{document}
\begin{tikzpicture}
\node[filled] (x) {$x$};
\node[empty, right=of x] (y) {$y$};
\path (x) edge[arrow] node {} (y);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .