6

I cannot manage to compute and to draw the box (that is, the lower left corner and the upper right corner) that would exactly encompass the green rectangle node and the red point (whose exact coordinates and dimensions can vary).

enter image description here

I tried this code, which does not work:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%

\begin{document}
\begin{tikzpicture}
  \clip (-2,-2) rectangle (10,8);

  \node[inner sep=2cm, fill=green] (rect) at (3,4) {Bla Bla};
  \node[inner sep=1pt, outer sep=0, circle, fill=red] (point) at (-1,1) {};

  \ExtractCoordinate{point.center}
  \edef\xone{\XCoord}
  \edef\yone{\YCoord}

  \ExtractCoordinate{rect.south west}
  \edef\xtwo{\XCoord}
  \edef\ytwo{\YCoord}
  \pgfmathparse{min(\xone,\xtwo)}
  \coordinate (x) at (\pgfmathresult,0);
  \pgfmathparse{min(\yone,\ytwo)}
  \coordinate (y) at (0,\pgfmathresult);

  \ExtractCoordinate{rect.north east}
  \edef\xtwo{\XCoord}
  \edef\ytwo{\YCoord}
  \pgfmathparse{max(\xone,\xtwo)}
  \coordinate (xx) at (\pgfmathresult,0);
  \pgfmathparse{max(\yone,\ytwo)}
  \coordinate (yy) at (0,\pgfmathresult);

  \draw ($(x) + (y)$) rectangle (0, 0);
  \draw ($(xx) + (yy)$) rectangle (1, 0);      
  \draw ($(x) + (y)$) rectangle ($(xx) + (yy)$);

  %% The expected result should be equivalent to drawing
  % \draw[orange, dashed] (\xone,\yone) rectangle (\xtwo, \ytwo);      
\end{tikzpicture}
\end{document}

I first extract the X and Y coordinates of the point and of the south west and north east anchors of the rectangle, then call the min and max functions. However, I guess at this stage: \coordinate (x) at (\pgfmathresult,0); something goes wrong, so I get incorrect values.

The expected result can be seen after uncommenting the last line in tikzpicture.

3
  • 2
    With the fit library you can just do \node[fit=(rect)(point), inner sep=+0pt,draw] {}; to achieve a rectangle that encompasses (rect) and (point). From your code, you are missing at least a few pt after the \pgfmathresults in the coordinate specification of x, y and so on. Dec 15, 2013 at 18:11
  • Thanks, it is exactly what I need, and way more elegant.
    – Elena
    Dec 15, 2013 at 18:57
  • @Paul Gessler I can write the answer, in fact it is very simple.
    – Elena
    Feb 26, 2014 at 14:18

1 Answer 1

4
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
  \clip (-2,-2) rectangle (10,8);

  \node[inner sep=2cm, fill=red] (rect) at (3,4) {};
  \node[inner sep=1pt, outer sep=0, circle, fill=red] (point) at (-1,1) {};

  \node[fit=(rect)(point), draw, thick, inner sep=0] {The tight
    encompassing rectangle}; %
  \node[fit=(rect)(point), draw, thick, inner sep=0.5cm, blue] {The
    encompassing rectangle with inner separation};
\end{tikzpicture}
\end{document}

The result is

encompassing rectangle

You must log in to answer this question.

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