# Fitting Shapes Around Irregular Nodes In TikZ

I have the following Hasse diagram

which uses the following code:

\documentclass[a4paper, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\node (abc) at (0,4) {$\{a,b,c\}$};
\node (ab) at (-2,2) {$\{a,b\}$};
\node (ac) at (0,2) {$\{a,c\}$};
\node (bc) at (2,2) {$\{b,c\}$};
\node (a) at (-2,0) {$\{a\}$};
\node (b) at (0,0) {$\{b\}$};
\node (c) at (2,0) {$\{c\}$};
\node (empty) at (0,-2) {$\{\}$};
\draw (abc) -- (ab) -- (a) -- (empty) -- (c) -- (bc) -- (abc) -- (ac) -- (a)
(ac) -- (c)
(b) -- (empty);
\draw[preaction={draw=white, -,line width=6pt}] (ab) -- (b) -- (bc);
\end{tikzpicture}
\label{fig:hasse}
\end{figure}

\end{document}


I am attempting to draw a box around some of the nodes like so:

\documentclass[a4paper, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\node (abc) at (0,4) {$\{a,b,c\}$};
\node (ab) at (-2,2) {$\{a,b\}$};
\node (ac) at (0,2) {$\{a,c\}$};
\node (bc) at (2,2) {$\{b,c\}$};
\node (a) at (-2,0) {$\{a\}$};
\node (b) at (0,0) {$\{b\}$};
\node (c) at (2,0) {$\{c\}$};
\node (empty) at (0,-2) {$\{\}$};
\draw (abc) -- (ab) -- (a) -- (empty) -- (c) -- (bc) -- (abc) -- (ac) -- (a)
(ac) -- (c)
(b) -- (empty);
\draw[preaction={draw=white, -,line width=6pt}] (ab) -- (b) -- (bc);
\node[draw,dotted,fit=(ab) (a) (b)] {};
\end{tikzpicture}
\label{fig:hasse}
\end{figure}

\end{document}


which (unsurprisingly) ends up looking like this:

What I would like (ideally) is to make some round corner triangle that covers the nodes (ab), (a) and (b) such that (ac) remains uncovered. I'm quite new to tikz (though I use tikz-cd a fair amount) so I'm unsure which library would help without overly convoluting the code (several answers I've read I wouldn't be able to adapt for my purposes without explanation).

Any help appreciated, thanks!

• You need to tell TikZ that you want a triangle. The default node shape is a rectangle, which is precisely what you get. Also you can rotate a fit with rotate fit. However, instead of doing all this, samcarter's nice answer is the way to go. – user121799 Jan 7 at 15:43

You can do it also with tikz-cd.

With execute at end picture={...} you can add any TikZ command you need to execute after drawing you tikzcd.

The crossing over idea is by Sebastiano's deleted answer.

If you don't want to name the matrix you can use \tikzcdmatrixname directly (see this answer by marmot, for example).

To set some options for all the tikzcd of your document, you could use \tikzcdset{...}, see Section 2 Controlling the appearance of diagrams of the package documentation.

The matrix syntax is analogous to any other LaTeX table: & to separate the cells and \\ to separate the rows.

The command \ar is an abbreviation for \arrow. Its options u, d, l and r mean up, down, left and right (the direction of the arrow).

The option execute at end picture={...} allows you to add some TikZ commands to be executed after the drawing of the diagram.

mymatr-n-m is the element at row n and column m of matrix mymatr.

\coordinate (comodo) at (mymatr-2-1.north west |- mymatr-3-1.south west); create the point comodo with coordinate x = the one of mymatr-2-1.north west and coordinate y = the mymatr-3-1.south west.

\draw (A) -- (B); draws a line from A to B.

dashed and rounded corners are options with obviuos meaning.

yshift=... shifts the coordinate vertically, xshift=... horizontally.

cycle at the end tells that the path has to go from here to the starting point.

\documentclass[a4paper,12pt]{article}
\usepackage{tikz-cd}
\tikzcdset{
every arrow/.append style={dash},
every diagram/.style={
row sep=50pt,
column sep=30pt
},
}

\begin{document}
\begin{tikzcd}[
every matrix/.style={name=mymatr},
execute at end picture={
\coordinate (comodo) at (mymatr-2-1.north west |- mymatr-3-1.south west);
\draw[dashed, rounded corners] ([yshift=3pt]mymatr-2-1.north west) --  ([yshift=-3pt]comodo) -- ([yshift=-3pt, xshift=2pt]mymatr-3-2.south east) -- ([yshift=4pt, xshift=2pt]mymatr-3-2.north east) -- ([yshift=3pt]mymatr-2-1.north east) -- cycle;
}
]
& \{a,b,c\} \ar[ld] \ar[d] \ar[rd]\\
\{a,b\} \ar[d] & \{a,c\} \ar[ld] \ar[rd] & \{b,c\} \ar[d, crossing over] \ar[ld, crossing over] \\
\{a\} \ar[rd] & \{b\} \ar[lu, crossing over] \ar[d, crossing over] & \{c\} \ar[ld] \\
& \{\}
\end{tikzcd}
\end{document}


• Surely is better you answer that mine. :-) Today is not a very beautiful day for me with LaTeX. There is cold here and I'm not concentrate with my mind (without translator). Is it exact in English? Very good. The fear of doing something wrong is so much that I'm keeping myself "hidden". I hope very much that Hupfer invites me to his forest :-) – Sebastiano Jan 7 at 22:41
• @Sebastiano You gave me the idea. Your English is understandable :) – CarLaTeX Jan 7 at 22:44
• Boh. Are you sure? :-( I don't like the reproaches since I was a little boy :-(:-(:-(. Good night and gold dreams. – Sebastiano Jan 7 at 22:46
• @Sebastiano It was not a reproach, it was a compliment! – CarLaTeX Jan 8 at 3:40
• @Ben Some more info added, let me know if you need more. – CarLaTeX Jan 8 at 11:30

To be more flexible you can manually draw the path around your desired nodes:

\documentclass[a4paper, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\node (abc) at (0,4) {$\{a,b,c\}$};
\node (ab) at (-2,2) {$\{a,b\}$};
\node (ac) at (0,2) {$\{a,c\}$};
\node (bc) at (2,2) {$\{b,c\}$};
\node (a) at (-2,0) {$\{a\}$};
\node (b) at (0,0) {$\{b\}$};
\node (c) at (2,0) {$\{c\}$};
\node (empty) at (0,-2) {$\{\}$};
\draw (abc) -- (ab) -- (a) -- (empty) -- (c) -- (bc) -- (abc) -- (ac) -- (a)
(ac) -- (c)
(b) -- (empty);
\draw[preaction={draw=white, -,line width=6pt}] (ab) -- (b) -- (bc);
\draw[dotted,rounded corners=20pt] ([yshift=0.9cm]ab.north west) -- ([xshift=-0.15cm,yshift=-0.1cm]a.south west) -- ([xshift=0.7cm,yshift=-0.1cm]b.south east) -- cycle;
\end{tikzpicture}
\label{fig:hasse}
\end{figure}

\end{document}


• +1, That's very close to what I'm after. I've played around with the numbers to try and get a shape that I'm happier with, though I'm not familiar with the syntax (for example I don't know if it's possible to get something like b.south east east) which is making it hard to get it to just the right shape (eg not cutting off the right bracket of (ab) while remaining symmetric). I will leave the question open to see if anyone has an improvement for the time being. – Ben Jan 7 at 13:35
• @Ben No south east south is not possible, you can just use shifts to move the corners to the positions you want. I adjusted the positions a bit. If the } from ab should be inside the path shift the b corner right and the ab corner upwards, – user36296 Jan 7 at 13:40