我正在设计原始数据类型的内存布局,例如int
显示 8 位。以下是代码片段-
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, calc}
\begin{document}
\begin{tikzpicture}[node distance=0mm,every node/.style={inner sep=1mm, font=\tiny}]
\node [draw, matrix] (a)
{
\foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
};
\node [draw, matrix,right=of a](b)
{
\foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
};
\node [draw, matrix,right=of b](c)
{
\foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
};
\node [draw, matrix,right=of c](d)
{
\foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
};
\node[below=of $(a)!0.5!(d)$, yshift=-1mm] (plus) {label goes here};
\end{tikzpicture}
\end{document}
是否可以缩短代码?例如,只需封装几个节点,我认为就可以做到。有什么线索吗?
答案1
在这种情况下,所有带有点的块都相等,您可以重复定义为的块\pic
:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 1mm and 0mm,
dot/.style = {circle, fill, inner sep=1pt, right=1mm},
memory/.pic = {\foreach \x in {1,...,8} {\node (n\x) [dot] at (\x/5,0) {};}
\node (f) [draw,
inner xsep=3mm, inner ysep=2mm,
outer sep=0mm, fit=(n1) (n8)] {};
\coordinate (-e) at (f.east);
\coordinate (-s) at (f.south);
}
]
\pic (a) {memory};
\pic[right=of a-e] (b) {memory};
\pic[right=of b-e] (c) {memory};
\pic[right=of c-e] (d) {memory};
%
\node[below=of b-s -| b-e] (plus) {label goes here};
\end{tikzpicture}
\end{document}
答案2
只是为了好玩,手册(text effects along path
)中的一个示例在这里稍作改动:
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\path [
decoration={
text effects along path,
text={~~~~~~~~ ~~~~~~~~ ~~~~~~~~},
text effects/.cd,
path from text,
every letter/.style={shape=rectangle, fill=blue!20, draw=blue!40,
minimum size=7mm, label={center:$\bullet$}}
},
decorate,
local bounding box=memory
] (0,0);
\node[below,scale=2] at (memory.south) {label goes here};
\end{tikzpicture}
\end{document}
答案3
这个只使用一个节点及其相应的标签。
节点是一个rectangle split
节点(参见shapes.multipart
库)。每个部分内容都是用命令构建的\mydots
。此命令有一个可选参数,默认值为 8。此参数固定项目符号的数量。由于\bullet
是二元运算符,因此在项目符号数量为偶数时会引起间距问题,因此可以使用 将其转换为普通运算符\mathop
。最后,使用选项绘制节点的标签label
。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.multipart}
\newcommand{\mydots}[1][8]{$\foreach\i in {1,...,#1}{\mathop{\bullet}}$}
\begin{document}
\begin{tikzpicture}
\node[rectangle split,
rectangle split horizontal, draw, font=\small,
inner xsep=2mm,
label=below:label goes here]{%
\mydots\nodepart{two}\mydots
\nodepart{three}\mydots\nodepart{four}\mydots};
\end{tikzpicture}
\end{document}