4

Below is an example using the fit library to put a highlight around nodes A, B, and C. What if I wanted to exclude D? (obviously it would have to be a non-rectangular shape)

I guess I'm looking for a way to have union/intersection/difference of shapes.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows,positioning,fit,backgrounds,shapes,patterns}
\begin{document}
\begin{tikzpicture}[node distance=5mm, auto,
    block/.style={
        % The shape:
        rectangle, minimum size=6mm, minimum height=6mm, minimum width=30mm,
        rounded corners=1mm,
        node distance=8mm,
        % The rest
        thick,draw=black,
        top color=white,
        bottom color=myblue!15,
        font=\sffamily\footnotesize,
        drop shadow
    },
    area/.style={
        rectangle,
        fill=green!80!black, opacity=0.2,
        draw=black
    },
    >=latex
    ]
        \node (a) {A};
        \node [right=of a] (b) {B};
        \node [below=of a] (c) {C};
        \node [right =of c] (d) {D};
        \node [area, fit=(a) (b) (c)] {};   
\end{tikzpicture}
\end{document}

This is similar to How to perform not rectangular fit with tikz? but not the same.

1

1 Answer 1

3

See, if the following solution is appropriate to you:

enter image description here

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,fit,patterns,positioning,shadows,shapes}
\begin{document}
    \begin{tikzpicture}[
    node distance=5mm, 
    auto,
every node/.style={inner sep=2mm, minimum size=7mm}
                        ]
\node (a)               {A};
\node (b) [right=of a]  {B};
\node (c) [below=of a]  {C};
\node (d) [right=of c]  {D};
%
\draw[draw=gray, rounded corners, thick, fill=green!80!black, opacity=0.2]
    (a.north) -| (b.south east) -- (c.south east) -| 
    (a.west) |- (a.north);
\end{tikzpicture}
\end{document}
4
  • that works in this sample case, but it defeats the point of using the "fit" library. My real problem involves a much more complicated set of shapes.
    – Jason S
    Commented Mar 10, 2016 at 19:21
  • I'm afraid, that there not exist a shape, which be appropriate for your needs. So, one solution can be just draw line around selected nodes. In more complex situation, the curve will become more complicated ... some case you can use ellipse, however its positioning is not so simple. In a many cases it can't avoid some nodes.
    – Zarko
    Commented Mar 10, 2016 at 19:29
  • 1
    +1 Esp. |- and -| is useful, would like to know more about this. Commented Mar 26, 2021 at 5:39
  • 2
    @Ilonpilaaja, see TikZ & PGF manual, subsection 13.3.1 Intersections of Perpendicular Lines, page 144 (version 3.1.8b).
    – Zarko
    Commented Mar 26, 2021 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .