中间带有文本的位数组

中间带有文本的位数组

我对 Latex 还很陌生,我想绘制 Bitarrays,虽然我在 beamer 中让它工作了(以一种非常不优雅的方式),但我在文章中很难让它正常工作。我从这个网站上的一些代码中复制了一个示例,只是修改了它来获得我的结果,我不确定我是否真的知道自己在做什么。

我想要的是:

  1. 绘制位数组,在其左边有方程/运算
  2. 在 Bitarrays 之间放置一些文本(以解释我刚刚执行的操作)
  3. 正确对齐所有位数组
  4. 在右侧画一个手镯,并在上面写上数字(如图2所示)
  5. 在我的文档中,我使用了\footnotesize(见图2)有没有更好的方法自动地调整大小并使其中的所有框/文本/方程式具有相同的大小tikzpicture
  6. 我想给每个 1 的位着色,有没有一种自动的方法来做到这一点,而不是给每个节点着色?

我迄今所做的:

对于 beamer,我通过将每个位设为一个节点来解决这个问题。这非常不优雅,导致代码太多,现在我很难在中间插入文本。

例如下面的代码:

\documentclass[11pt]{scrartcl}

 \usepackage{tikz}
\usetikzlibrary{positioning,matrix, arrows.meta,calc,positioning,chains,decorations.pathreplacing,decorations.pathmorphing,shapes}

\oddsidemargin=0.in
\topmargin=-1.5cm
\textheight=23cm
\textwidth=16cm

\begin{document}

\begin{tikzpicture}[
  %-{Stealth[length = 2.5pt]},
       start chain = going right,
     node distance = 0pt,
MyStyle/.style={draw, minimum width=1mm, minimum height=2mm,
                outer sep=0pt, on chain},
  ]
 \node [MyStyle] (L1) {$ x_{1}$};       %
\node [MyStyle, right=1.7cm of L1] (R1) {$0$};
\node [MyStyle] (2) {$0$};
\node [MyStyle] (3) {$0$};
\node [MyStyle] (4) {$0$};
\node [MyStyle] (5) {$0$};
\node [MyStyle] (6) {$0$};
\node [MyStyle] (7) {$0$};
\node [MyStyle] (8) {$0$};
\node [MyStyle] (9) {$0$};
\node [MyStyle] (11) {$0$};
\node [MyStyle] (12) {$0$};
\node [MyStyle] (13) {$0$};
\node [MyStyle] (14) {$0$};
\node [MyStyle] (15) {$0$};
\node [MyStyle] (16) {$0$};
\node [MyStyle] (17) {$0$};
\node [MyStyle] (18) {$0$};
\node [MyStyle] (19) {$0$};
\node [MyStyle] (20) {$0$};
\node [MyStyle] (21) {$0$};
\node [MyStyle, fill=green!50] (22) {$1$};
\node [MyStyle] (23) {$0$};
\node [MyStyle] (24) {$0$};
\node [MyStyle, fill=green!50] (25) {$1$};
\node [MyStyle, fill=green!50] (26) {$1$};
\node [MyStyle] (27) {$0$};
\node [MyStyle, fill=green!50] (28) {$1$};
\node [MyStyle] (29) {$0$};
\node [MyStyle] (30) {$0$};
\node [MyStyle, fill=green!50] (31) {$1$};
\node [MyStyle] (RE1) {$0$}; 
\node[on chain]            {  };


\node [MyStyle, below=0.4cm of L1] (L2) {$x^{(1)}_{1}= x_{1} \gg (32-2)$};   %
\node [MyStyle, below=0.5cm of R1] (R2) {$0$};
\node [MyStyle] (35) {$0$};
\node [MyStyle] (36) {$0$};
\node [MyStyle] (37) {$0$};
\node [MyStyle] (38) {$0$};
\node [MyStyle] (39) {$0$};
\node [MyStyle] (40) {$0$};
\node [MyStyle] (41) {$0$};
\node [MyStyle] (42) {$0$};
\node [MyStyle] (43) {$0$};
\node [MyStyle] (44) {$0$};
\node [MyStyle] (45) {$0$};
\node [MyStyle] (46) {$0$};
\node [MyStyle] (47) {$0$};
\node [MyStyle] (48) {$0$};
\node [MyStyle] (49) {$0$};
\node [MyStyle] (50) {$0$};
\node [MyStyle] (51) {$0$};
\node [MyStyle] (52) {$0$};
\node [MyStyle] (53) {$0$};
\node [MyStyle] (54) {$0$};
\node [MyStyle] (55) {$0$};
\node [MyStyle] (56) {$0$};
\node [MyStyle] (57) {$0$};
\node [MyStyle] (58) {$0$};
\node [MyStyle] (59) {$0$};
\node [MyStyle] (60) {$0$};
\node [MyStyle] (61) {$0$};
\node [MyStyle] (62) {$0$};
\node [MyStyle] (63) {$0$};
\node [MyStyle] (RE2) {$0$};
\end{tikzpicture}
\end{document}

结果如图 1 所示:

图片1

图片 2:正如您所见,它没有对齐,我想避免为每个起始节点手动对齐它。

图片2

答案1

欢迎来到 TeX.SE!!!

也许这可以作为一个起点。我\pic为位数组创建了一个,并调整了的比例以tikzpicture适应文章几何形状(您可能需要更改比例)。括号是在decorations库的帮助下绘制的,对齐是手工完成的,但这并不难。

我的代码:

\documentclass {article}
\usepackage    {amssymb} % \gg \veebar
\usepackage    {lipsum}  % dummy text
\usepackage    {tikz} 
\usetikzlibrary{decorations.pathreplacing} % decoration, brace

\tikzset
{%
    my label/.style={draw,inner sep=1pt},
    my arrow/.style={red,->},
    my brace/.style={red,decorate,decoration={brace,raise=1mm}},
    pics/bitarray/.style={% #1 = list of bits
      code={%
        \foreach[count=\j from 0]\i in {#1}
        {
          \ifnum\i>0
            \fill[green] (0.5*\j,0) rectangle ++ (0.5,0.5);
          \fi
          \draw (0.5*\j,0) rectangle ++ (0.5,0.5);
          \node at (0.5*\j+0.25,0.25) {$\i$};
        }
        \coordinate (-west)       at (0,0.25);
        \coordinate (-north east) at (16,0.5);
        \coordinate (-south east) at (16,0);
    }},
}

\begin{document}
\lipsum[1]
\begin{figure}[ht]\centering
\begin{tikzpicture}[transform shape,scale=0.59] % <-- change the scale if you have other paper geometry
%\draw[help lines] (-3,0) grid[step=0.5] (16,7);% <-- only to see positions and alignment
% bit arrays
\pic (1) at (0,6) {bitarray={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,0,0,1,0}};
\pic (2) at (0,4) {bitarray={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
\pic (3) at (0,2) {bitarray={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
\pic (4) at (0,0) {bitarray={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,0,0,1,1}};
% labels
\node[my label] (A) at (-2,6.25) {\strut $y = x_1$};
\node[my label] (B) at (-2,5.25) {\strut $y^{(1)} = y \gg 11$};
\node[my label] (C) at (-2,3.25) {\strut $y^{(2)} = y^{(1)} \& d$};
\node[my label] (D) at (-2,1.25) {\strut $y^{(3)} = y \veebar y^{(2)}$};
% bracket
\draw[my brace] (1-north east) -- (4-south east) node[black,midway,xshift=8mm]{$(1.1)$};
% optional arrows
\draw[my arrow] (A) -- (1-west);
\draw[my arrow] ([yshift=-1mm]1-west) to[bend right] (B.east) to[bend right] ([yshift=1mm]2-west); 
\draw[my arrow] ([yshift=-1mm]2-west) to[bend right] (C.east) to[bend right] ([yshift=1mm]3-west); 
\draw[my arrow] ([yshift=-1mm]3-west) to[bend right] (D.east) to[bend right] ([yshift=1mm]4-west); 
\end{tikzpicture}
\caption{Some bit arrays.}
\end{figure}

\lipsum[2]
\end{document}

输出结果如下: 在此处输入图片描述

相关内容