[Tex/LaTex] How to center text between two rectangles in TiKZ

horizontal alignmenttikz-pgf

I have the following TiKZ code:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}

  \newcommand{\delim}[1]{\draw(#1,-0.2) -- (#1,-0.5);}

  \draw (0,0) rectangle (1,1);

  \draw (1,0) rectangle (2,1);

  \delim{0}
  \delim{2}

  \node[below] at(0,0){\small Unused};

\end{tikzpicture}

\end{document}

This results in this image:

enter image description here

But I'd like to center the text between the two boxes, sort of like this image:

enter image description here

How do I do this?

Best Answer

The text is centered under (0,0) which is the coordinate you provided. I think you want to use (1,0):

\node[below] at(1,0) {\small Unused};

If you want thicker lines, you use [thick], [ultra thick] options, or specify an line width as [line width=2pt] :

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \newcommand{\delim}[1]{\draw [thick] (#1,-0.2) -- (#1,-0.5);}

  \draw [ultra thick] (0,0) rectangle (1,1);
  \draw [ultra thick] (1,0) rectangle (2,1);

  \delim{0}
  \delim{2}

  \node[below] at(1,0){\small Unused};
\end{tikzpicture}
\end{document}

Another method is to modify the \delim macro to take a second parameter and use that to label the center of the line segments, A and B in the example below. Then you can compute the midpoint of the two points using ($(A)!0.5!(B)$) and place the text at that position. This solution has the added benefit that if you change the coordinates in the \delim macro, the text will be centered between the two points.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
  \newcommand{\delim}[2]{\draw [thick] (#1,-0.2) -- (#1,-0.5); \node (#2) at (#1,-0.35) {};}

  \draw [ultra thick] (0,0) rectangle (1,1);
  \draw [ultra thick] (1,0) rectangle (2,1);

  \delim{0}{A}% Label this as (A)
  \delim{2}{B}% Label this as (B)

  \node at ($(A)!0.5!(B)$) {\small Unused};% Place the node midway between (A) and (B)
\end{tikzpicture}
\end{document}

Note that this requires \usetikzlibrary{calc} in the preamble.