# How to place a textnode at the center of a drawn rectangle

A rectangle label does not appear inside the rectangle but on the corner of it.

  \tikzstyle{every node} = [align=center]
\begin{tikzpicture}
\draw (0, 0) rectangle (2, 2) node [center] {label};
%center is unknown but was my best guess
\end{tikzpicture}


How do I move the labelling node to have a common center with the rectangle?

The result should look like this

  \tikzstyle{every node} = [align=center]
\begin{tikzpicture}
\draw (0, 0) rectangle (2, 2);
\node at (1,1) {label};
\end{tikzpicture}


but without having to specify the center manually.

• Why not \node[draw,rectangle, minimum size=2cm] at (1,1){label};? – Claudio Fiandrino Feb 1 '13 at 15:10
• I had that approach before and i am now refactoring. I have trouble realigning my boxe´s if i dont explicitly state start and endpoints. – Johannes Feb 1 '13 at 15:14
• @Johannes: give a name to the node (e.g. \node[draw,rectangle, minimum size=2cm] (name) at (1,1){label};) then you will have name.south west for the lower left corner and name.north east for the upper right corner. – Claudio Fiandrino Feb 1 '13 at 15:35

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,2) node[pos=.5] {Test};
\end{tikzpicture}
\end{document}


By using node in a path without option, it will position the node to the last point.

• You can also use [midway] option instead of [pos=.5]. It is equivalent and the syntax sounds perhaps a bit more natural. – JLDiaz Feb 1 '13 at 16:45

You can write the Text, that should be centered, after the connection (rectangle in this case). It also works for lines etc.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle node{Test} (2,2);
\end{tikzpicture}
\end{document}

• What is the difference between your code and \draw (0,0) rectangle (2,2) node[pos=.5] {Test}; from Lionel's? – Sigur Jan 16 '17 at 13:03
• The result is the same, but the pos option is set implicitly. – dexteritas Jan 16 '17 at 14:33

It seems that OP wants to draw a rectangle on fixed coordinates and later on insert the label in its center.

By default a node is a rectangle with a text in its center, so we could use it as an alternative.

Unfortunately node's solution needs some minimum width|height|size (which is not always respected) and it seems that OP doesn't want to compute them.

As an alternative node which does the work for us is a fit node. In this case we can enter both corner coordinates and insert the text with a center label. This solution has the advantage that we have created a correct size node which be used later on in our diagram.

\node[draw, fit={(0,0) (2,2)}, inner sep=0pt, label=center:Test] (A) {};


Rectangle corners can be absolute or relative coordinates. In this case we can use a shift option to place it where we want.

Following code shows both options, in blue Lionel or dexteritas solution and in black a fit node.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

\begin{scope}
\node[draw, fit={(0,0) (2,2)}, inner sep=0pt, label=center:Test] (A) {};
\node[draw, fit={(0,0) (2,2)}, xshift=3cm, inner sep=0pt, label=center:Test] (B) {};
\draw (A)--(B);
\end{scope}

\begin{scope}[draw=blue, yshift=2.5cm]
\draw (0,0) rectangle node[draw] (A) {Test} ++(2,2);
\draw (3,0) rectangle node[draw] (B) {Test} ++(2,2);
\draw (A)--(B);
\end{scope}
\end{tikzpicture}
\end{document}