Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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. –  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
    
@Qrrbrbirlbel would you consider writing this up as an answer? –  Paul Gessler Feb 22 at 21:13
    
@Paul Gessler I can write the answer, in fact it is very simple. –  Elena Feb 26 at 14:18

1 Answer 1

\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

share|improve this answer

Your Answer

 
discard

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.