# Positioning nodes equally distanced inside a vertical rectangle

As in the following picture I am trying to put the numbers/nodes inside that vertical rectangle in fixed distance between one another.

Is there a better way to position those number?! or an easy way to calculate the proper coordinates?

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

\begin{figure}[h]
\centering

\begin{tikzpicture}
%
% A Rectangle
\draw (0, 0) rectangle (0.5, 3);

% adding numbers :( so boring
\node at (0.25, 3 - 0.25) {1}; % the top number;
\node at (0.25, 3 - 0.69) {0};
\node at (0.25, 1.3 + 0.6) {0};
\node at (0.25, 1.25) {\vdots};
\node at (0.25, 0.25) {0};

\end{tikzpicture}%
\end{figure}
\end{document}


Here is a macro that does the vertical fit. The first argument is the upper coordinate, the second is the lower coordinate, and the third is a comma-separated list of entries.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,calc}
\newcommand{\FitIntoRectangle}[4][]{
\path let \p1 = #2, \p2 = #3, \n1 = {abs(\y1-\y2-4pt)} in \pgfextra{\xdef\Dist{\n1}};
\typeout{\the\baselineskip}
\foreach \X[count=\Y] in {#4} {\xdef\Num{\Y}}
\foreach \X[count=\Y] in {#4} {
\pgfmathsetmacro{\Pos}{(2pt+\the\baselineskip/2)/\Dist
+((\Y-1)/(\Num-1))*(\Dist-4pt-\the\baselineskip)/\Dist}\typeout{\Y,\X,\Pos}
\path #2 -- #3 node[pos=\Pos] (num-\Y){\X} ;
}
\node[fit=(num-1) (num-\Num),draw,inner sep=2pt] (Box){};
}
\begin{document}
\begin{tikzpicture}
\FitIntoRectangle{(0,0)}{(0,-4)}{1,0,0,0,0,1}
% just to check that the thing works
\draw (0,0) circle (1pt); \draw (0,-4) circle (1pt);
\end{tikzpicture}%
\end{document}


You could, for instance, use a matrix.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
mymat/.style={draw,
matrix of math nodes,
align=center,
},
}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\matrix[mymat]  (mat1)
{  1\\
0\\
0\\
$\vdots$\\
0\\
};%
\end{tikzpicture}%
\end{figure}
\end{document}


The entries are automatically nodes, and the spacing is easy to control. Placing it is also easy, if you do more advanced things, I'd recommend to load the positioning library as well.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
mymat/.style={draw,
row sep=0.1cm,
matrix of math nodes,
align=center,
},
}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\draw (0,0) -- (0,-2);
\matrix[mymat,at={(2,0)},anchor=north]  (mat1)
{  1\\
0\\
0\\
$\vdots$\\
0\\
};%
\draw[latex-latex] (mat1-1-1) to[out=0,in=0] (mat1-3-1);
\end{tikzpicture}%
\end{figure}
\end{document}


• At first thank you, but how I didn't figure out how to change the matrix position. – Taha Magdy May 9 '18 at 1:32
• Also, I have a constraint; that vertical rectangle is a part of a bigger complex figure, what I need is to distribute the numbers equally spaced inside the rectangle, I cannot change the rectangle size, because something else depends on it. the rectangle length is fine. – Taha Magdy May 9 '18 at 1:36
• But your answer does not solve the problem :( It does not work with my case, because in your solution the length of the rectangle depends on how many nodes inside it. I may make a change in the code to use your way, I it worked, I'll accept your answer immediately. Thank You. – Taha Magdy May 9 '18 at 2:00
• I am sorry If the question is coined in a confusing way, I will make it clearer. – Taha Magdy May 9 '18 at 2:19