4

I'm trying to draw the following diagram using Tikz: Complexity Diagram

I wrote the following to recreate a similar version of it

\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}

But, of course, when I create the fit node, with the fill option, it covers the fitted nodes over, yielding something like this: result

How can I make the filling stay "behind" the other nodes, so as to not overwrite the fitted nodes (P, and NP)?

5

PGF allows you to create and switch layers. For example this command:

\pgfdeclarelayer{background}

will create a layer named background.

Then, you can configure the order of these layers with:

\pgfsetlayers{background,main}

The main layer is, you guessed right, the main layer :P

This will set the main on top of background.

Then you can draw on specific layers with:

\begin{pgfonlayer}{<layer_name>}
    <stuff>
\end{pgfonlayer}

So your code becomes:

\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}

enter image description here

Edit:

As requested by Lord @marmot. It took a while to get the colors right :)

\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}

enter image description here

  • 1
    Very nice! (+1) Perhaps load \usetikzlibrary{shapes.geometric} to get ellipses and add [font=\sf] after \begin{tikzpicture} to reproduce the Wikipedia figure even more closely. – marmot Mar 13 '18 at 22:59
  • @marmot Better? – Phelype Oleinik Mar 13 '18 at 23:18
  • Looks great! (And in order to honor the \pi day, you rotated the node orientation by \pi. That's really subtle ;-) – marmot Mar 13 '18 at 23:19
  • 1
    @marmot \pies aren't problems U_U \pineapple \pizzas are! – Phelype Oleinik Mar 13 '18 at 23:21
  • Well, that's why I prefer \pineapple \pie (or was it cake?) – marmot Mar 13 '18 at 23:23
6

backgrounds library does part of the job for you. It declares foreground, main and background layers. They can be used inside a scope with on background layer option:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.