[Tex/LaTex] Placing a rectangular node in tikz by specifying two opposing corners

nodespositioningtikz-pgf

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.

Best Answer

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.