我写了下面的内容来重新创建类似的版本
\documentclass[11pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\tikzset{set/.style={draw,circle,inner sep=0pt,align=center}}
\node[set,fill=blue!20,text width=3cm](P) at (0, -1) {P Problems};
\node[set,fill=green!20,text width=3cm](NP) at (3,+1) {NP Complete};
\node[fill=purple!20, fit=(P)(NP),draw,rounded corners=0.5cm,inner sep=1cm,label={NP Problems}]{};
\end{tikzpicture}
\end{figure}
\end{document}
但是,当然,当我使用填充选项创建拟合节点时,它会覆盖拟合的节点,产生如下结果:
我怎样才能使填充停留在其他节点“后面”,以免覆盖已拟合的节点(P
,和NP
)?
答案1
PGF 允许您创建和切换图层。例如此命令:
\pgfdeclarelayer{background}
将创建一个名为 的图层background
。
然后,您可以使用以下命令配置这些层的顺序:
\pgfsetlayers{background,main}
你猜对了,这个main
层就是主层 :P
这会将 设置main
在 之上background
。
然后,您可以使用以下命令在特定图层上绘图:
\begin{pgfonlayer}{<layer_name>}
<stuff>
\end{pgfonlayer}
所以你的代码变成:
\documentclass[11pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{fit}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\tikzset{set/.style={draw,circle,inner sep=0pt,align=center}}
\node[set,fill=blue!20,text width=3cm](P) at (0, -1) {P Problems};
\node[set,fill=green!20,text width=3cm](NP) at (3,+1) {NP Complete};
\begin{pgfonlayer}{background}
\node[fill=purple!20, fit=(P)(NP),draw,rounded corners=0.5cm,inner sep=1cm,label={NP Problems}]{};
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}
编辑:
按照 Lord @marmot 的要求。花了一段时间才把颜色弄对 :)
\documentclass[11pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{tikz}
\definecolor{wikigreen}{HTML}{B3DDD5}
\definecolor{wikiblue}{HTML}{D5D6FA}
\definecolor{wikipink}{HTML}{DBBAD2}
\usetikzlibrary{fit}
\usetikzlibrary{shapes.geometric}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}[font=\sffamily]
\tikzset{set/.style={draw,ellipse,minimum height=1.5cm,inner sep=0pt,align=center}}
\node[thick,set,fill=wikigreen,text width=3cm](P) at (0, -1) {P Problems};
\node[thick,set,fill=wikipink ,text width=3cm](NP) at (3,+1) {NP Complete};
\begin{pgfonlayer}{background}
\node[ultra thick,fill=wikiblue, fit=(P)(NP),draw,rounded corners=0.5cm,inner sep=1cm,label={NP Problems}]{};
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}
答案2
backgrounds
库为您完成了部分工作。它声明了前景、主层和背景层。它们可以在带有on background layer
选项的范围内使用:
\documentclass[11pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{positioning, backgrounds, shapes.geometric, fit}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}[font=\sffamily]
\node[ellipse, draw, fill=blue!30] (P) {P Problems};
\node[ellipse, draw, fill=red!30, below right= 5mm and 5mm of P.east, anchor=north] (NP) {NP Complete};
\begin{scope}[on background layer]
\node[fill=purple!20, fit={(P)(NP)([yshift=7mm]P.north)}, draw, rounded corners=0.5cm, label={[anchor=north, inner sep=3mm]\large{NP Problems}}, inner sep=3mm]{};
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}