将节点等距放置在垂直矩形内

将节点等距放置在垂直矩形内

如下图所示,我试图将数字/节点放置在垂直矩形内,彼此之间保持固定距离。

有没有更好的方法来定位这些数字?!或者有没有简单的方法来计算正确的坐标?

在此处输入图片描述

\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}

答案1

这是一个执行垂直适配的宏。第一个参数是上坐标,第二个参数是下坐标,第三个参数是以逗号分隔的条目列表。

\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}

例如,您可以使用矩阵。

\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}

条目自动成为节点,间距易于控制。放置也很容易,如果您要做更高级的事情,我建议positioning也加载库。

\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}

在此处输入图片描述

相关内容