
我的努力是
\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
來繪製每個位元組(參見第 18.2 節)蒂克茲手動的)。名為 pic 的圖片byte cube
有兩個參數:{0 or 1, exponent}
。圖中繪製了“位元組立方體”,數字下方的填滿顏色設定為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}