6

In

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \coordinate (bottom left) at (10,10);
    \coordinate (top right) at (20,20);
    \draw[rounded corners,fill=black] (bottom left) rectangle (top right);
  \end{tikzpicture}
\end{document}

the line

    \draw[rounded corners,fill=black] (bottom left) rectangle (top right);

draws a black rectangle with rounded corners between the coordinates (bottom left) and (top right).

What's the most concise way to define a node with exactly the same shape, position and size as a rectangle defined with '\draw' and 'rectangle' (as above)?

The solution should ...

  • use the provided coordinates with manual calculations,
  • work with rounded corners,
  • place the node's coordinates (n.north, n.west, ...) were expected, and
  • be as concise (and elegant) as possible.
3
  • Apologies for a previous comment. The best way to do this is by defining a use- defined to path.
    – user10274
    Jun 14, 2015 at 11:40
  • 1
    Related: Q87989 Jun 14, 2015 at 13:18
  • What should happen, if you apply a rotation to the scope, say \begin{tikzpicture}[rotate=30]. Should this transformation also apply to the rectangular node? Jun 14, 2015 at 13:19

2 Answers 2

6

If I understood you correctly, you like to determine rectangle with size determined with two coordinates (bottom left, top right). If this is a case, you can obtain this as follows:

\documentclass[tikz,border=5mm]{standalone}
    \usetikzlibrary{fit}
\begin{document}
    \begin{tikzpicture}[
mynode/.style 2 args = {
    draw, rounded corners, fill=black, 
    inner sep=0pt, outer sep=0pt,
    fit=(#1) (#2)}
                        ]
\coordinate (bottom left) at (10,10);
\coordinate (top right) at (20,20);
    \node[mynode={bottom left}{top right}] {};
    \end{tikzpicture}
\end{document}

enter image description here

Of course, you can define rectangle size on different way, for example width minimum width, minimum height, text width and text height etc.

For fiting node on given coordinates I use tikzlibrary{fit} and set inner node separation to 0pt. The node aslo have all anchors as defined for rectangular shape.

Comment: Declare rectangle nodes on such a way may lead to problem with placing of node's contents. They can be displaced from center of node.

1

You can also define a node placed at the middle of the coordinates, and big half the distance of the two coordinates.

\node[rounded corners, draw=red, minimum size=10cm] at (15,15) {};

Here are the corners to show it fits:

enter image description here enter image description here

Ouput

figure 1

Code

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \coordinate (bottom left) at (10,10);
    \coordinate (top right) at (20,20);
    \draw[rounded corners,fill=black] (bottom left) rectangle (top right);
    \node[rounded corners, draw=red, minimum size=10cm] at (15,15) {};
  \end{tikzpicture}
\end{document}

You must log in to answer this question.

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