TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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

  \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{rect.south west}
  \coordinate (x) at (\pgfmathresult,0);
  \coordinate (y) at (0,\pgfmathresult);

  \ExtractCoordinate{rect.north east}
  \coordinate (xx) at (\pgfmathresult,0);
  \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);      

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.

share|improve this question
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. – Qrrbrbirlbel Dec 15 '13 at 18:11
Thanks, it is exactly what I need, and way more elegant. – Elena Dec 15 '13 at 18:57
@Paul Gessler I can write the answer, in fact it is very simple. – Elena Feb 26 '14 at 14:18

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

The result is

encompassing rectangle

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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