为每个 TikZ 图片单独设置背景:
\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}
将背景规则移入\tikzset{}
,背景不再起作用:
\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}
答案1
我认为你正在寻找
every picture/.append style={background rectangle/.style={fill=black!30,rounded corners}, show background rectangle}
如同
\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}