8

In the following tikz MWE I am using the fit library to draw a red box:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

      \begin{tikzpicture}
        \node (a) {A};
        \node [above right=of a] (b) {B};
        \node [right=of a] (c) {C};
        \node [draw=red, fit=(a) (b)] {};
      \end{tikzpicture}

\end{document}

Unfortunately this fit box includes the C node. Is it possible with fit to draw a box around A and B with including C using either non vertical/horizontal lines or using several vertical/horizontal lines in a "stair" fashion to exclude C ?

Ignasi
  • 136,588
Manuel Selva
  • 1,839
  • You can actually draw it. What is the real use case? –  Feb 25 '15 at 13:33
  • My use case is very close to this MWE. It's a simple graph drawn using the positioning tikz library where I want to highlight a path between two nodes. – Manuel Selva Feb 25 '15 at 13:36
  • Can we draw it then? –  Feb 25 '15 at 13:37
  • This is an interesting question (+1), but I think it will take a lot of work to make it happen in the general case. Even if you defined a new node shape, the "text area" (or for fit, the area in which the fitted nodes are placed) is always rectangular. – Paul Gessler Feb 25 '15 at 13:37
  • @HarishKumar yes I can draw it of course. I was just wondering whether or not a generic solution already existed for that scenario. – Manuel Selva Feb 25 '15 at 13:51

2 Answers2

8

One option is to use rotate fit like this:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fit,positioning,calc}

\begin{document}

      \begin{tikzpicture}
        \node (a) {A};
        \node [above right=of a] (b) {B};
        \node [right=of a] (c) {C};
        \node [draw=red, rotate fit=45, fit=(a) (b)] {};
      \end{tikzpicture}

\end{document}

enter image description here

5

If we can draw it, here you have a not so nice (my first time using it) example with hobby library:

enter image description here

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fit,positioning,hobby}

\begin{document}

      \begin{tikzpicture}
        \node (a) {A};
        \node [above right=of a] (b) {B};
        \node [right=of a] (c) {C};
        \draw[thick, red] ([shift={(-3mm,-3mm)}]a.south west) to[curve through={(a.south east) .. (b.south east) .. ([shift={(+3mm,+3mm)}]b.north east)..(b.north west)..(a.north west)}]  ([shift={(-3mm,-3mm)}]a.south west);
      \end{tikzpicture}

\end{document}

Update: and a nicer one after reading hobby doc:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fit,positioning,hobby}

\begin{document}

      \begin{tikzpicture}
        \node (a) {A};
        \node [above right=of a] (b) {B};
        \node [right=of a] (c) {C};
        \draw[thick, red] ([shift={(-3mm,-3mm)}]a.south west) to[closed, curve through={(a.south east) .. (b.south east) .. ([shift={(+3mm,+3mm)}]b.north east)..(b.north west)..(a.north west)}]  cycle;
      \end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588