如何为所有 TikZ 图片设置默认背景?

如何为所有 TikZ 图片设置默认背景?

为每个 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}

在此处输入图片描述

相关内容