我的努力是
\documentclass[margin=1in]{standalone}
\usepackage{tikz}
\usetikzlibrary[arrows, decorations.pathmorphing, backgrounds, positioning, fit, petri]
\begin{document}
\begin{tikzpicture}[node distance=0pt,
box/.style={draw, minimum size=1cm, inner sep=0.5cm},
value/.style={yshift=-1cm}]
\node[box] (b7) {1};
\node[box] (b6) [right=of b7] {1};
\node[box] (b5) [right=of b6] {1};
\node[box] (b4) [right=of b5] {1};
\node[box] (b3) [right=of b4] {1};
\node[box] (b2) [right=of b3] {1};
\node[box] (b1) [right=of b2] {1};
\node[box] (b0) [right=of b1] {1};
\node[value] [below of=b0] {$2^0$};
\node[value] [below of=b1] {$2^1$};
\node[value] [below of=b2] {$2^2$};
\node[value] [below of=b3] {$2^3$};
\node[value] [below of=b4] {$2^4$};
\node[value] [below of=b5] {$2^5$};
\node[value] [below of=b6] {$2^6$};
\node[value] [below of=b7] {$2^7$};
\end{tikzpicture}
\end{document}
我怎样才能在 tikz 中绘制这种二进制数字表示,就像 3d 框节点类型一样。谢谢
答案1
对于二进制数来说,这似乎是一张合理的图像1111111
,但是对于二进制数来说,101010111
我认为图像应该是:
下面的代码定义了一个宏\BinaryNumber
,它接受以逗号分隔的二进制数字列表。定义完成后,您可以使用
\BinaryNumber{1,1,1,1,1,1,1,1,1}
\BinaryNumber{1,0,1,0,1,1,1}
\BinaryNumber{1,0,1,1,1,0,1,0,0,1,1}
生产:
这是代码。我在下面稍微解释一下它的工作原理:
\documentclass{article}
\usepackage{tikz}
\tikzset{
pics/byte cube/.style args = {#1,#2}{
code = {
\draw[fill=white] (0,0) rectangle (1,1);
\node at (0.5,0.5){#1};
\draw[cube #1] (0,0)--(-60:2mm)--++(1,0)--++(0,1)--++(120:2mm)--(1,0)--cycle;
\draw(1,0)--++(-60:2mm);
\node at (0.5,-0.5){$2^{#2}$};
}
},
cube 1/.style = {fill=gray!30}, % style for bytes that are "on"
cube 0/.style = {fill=white}, % style for bytes that are "off"
}
\newcommand\BinaryNumber[1]{%
\begin{tikzpicture}
% count the number of bytes and store as \C
\foreach \i [count=\c] in {#1} { \xdef\C{\c} }
\foreach \i [count=\c, evaluate=\c as \ex using {int(\C-\c)}] in {#1} {
\pic at (\c, 1) {byte cube={\i,\ex}};
}
\end{tikzpicture}
}
\begin{document}
\BinaryNumber{1,1,1,1,1,1,1,1,1} \bigskip
\BinaryNumber{1,0,1,0,1,1,1} \bigskip
\BinaryNumber{1,0,1,1,1,0,1,0,0,1,1} \bigskip
\end{document}
主要思想是使用pic
来绘制每个字节(参见蒂克兹手册)。pic 称为 ,byte cube
接受两个参数:{0 or 1, exponent}
。pic 绘制“字节立方体”,数字下方的填充颜色设置为cube 0
或的相应样式cube 1
。更改这些样式将更改数字下的阴影。(因此,根据设计,样式的选择取决于二进制数字。)
第一个定义\BinaryNumber
循环遍历字节以确定二进制数的“长度”,然后再次循环遍历它们以绘制每个“字节立方体”。每个连续的字节立方体都会覆盖前一个立方体中我们“不想要”的部分。因此,即使右侧的阴影是为每个立方体绘制的,也只对最右边的立方体可见。
答案2
第一个简单的解决方案如下:
\documentclass[margin=1in]{standalone}
\usepackage{tikz}
\usetikzlibrary[arrows, decorations.pathmorphing, backgrounds, positioning, fit, petri]
\begin{document}
\begin{tikzpicture}[node distance=0pt,
box/.style={draw, minimum size=1cm, inner sep=0.5cm},
value/.style={yshift=-1cm}]
\node[box] (b7) {1};
\node[box] (b6) [right=of b7] {1};
\node[box] (b5) [right=of b6] {1};
\node[box] (b4) [right=of b5] {1};
\node[box] (b3) [right=of b4] {1};
\node[box] (b2) [right=of b3] {1};
\node[box] (b1) [right=of b2] {1};
\node[box] (b0) [right=of b1] {1};
\def\xasn{1mm}% x direction of the box
\def\yasn{-1.5mm}% y direction of the box
\fill[lightgray] (b0.north east) -- ([shift={(\xasn,\yasn)}]b0.north east) -- ([shift={(\xasn,\yasn)}]b0.south east) -- (b0.south east);
\draw (b0.north east) -- ([shift={(\xasn,\yasn)}]b0.north east) -- ([shift={(\xasn,\yasn)}]b0.south east);
\foreach \boxnr in {0,1,...,7} {
\draw[fill=lightgray] (b\boxnr.south west) -- +(\xasn,\yasn) -- ([shift={(\xasn,\yasn)}]b\boxnr.south east) -- (b\boxnr.south east);
}
\node[value] [below of=b0] {$2^0$};
\node[value] [below of=b1] {$2^1$};
\node[value] [below of=b2] {$2^2$};
\node[value] [below of=b3] {$2^3$};
\node[value] [below of=b4] {$2^4$};
\node[value] [below of=b5] {$2^5$};
\node[value] [below of=b6] {$2^6$};
\node[value] [below of=b7] {$2^7$};
\end{tikzpicture}
\end{document}