在 TikZ 中使用 \underbrace 创建不同颜色的数组

在 TikZ 中使用 \underbrace 创建不同颜色的数组

我想创建类似 TikZ 的东西:

-------------------------------  ...   ----------------
| 0000 | 0001 | 0010 | 0100 |           | 0111 | 1111 |
------------------------------  ...   -----------------
\     / \                  /                    \     /
 \M_0/   \------ M_1------/                      \M_d/

我可以为第一个框指定一种颜色,为后面的 3 个框指定不同的颜色,为最后一个框指定第三种颜色。

我尝试\underbrace创建M_0M_1并且M_d

有人知道我可以在哪里找到类似这样的简短例子吗?

编辑

感谢您提供的出色解决方案。在我上面的例子中,很难看到,也很难为其创建示例,因此下面是我想要的连接盒的示例。我希望这也是一种相对简单的方法。

在此处输入图片描述

答案1

该解决方案建立在 Altermundus 答案的基础上;我只使用该shapes库添加了两个“磁带”来获得中断频带所需的方面:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,shapes}

\begin{document}
\begin{tikzpicture}
 \foreach \c/\i [count=\n] in  
        {blue!20/0001,red!20/0001,red!20/0010,red!20/0100} 
           \node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm,font=\ttfamily](N\n){\i} ;

  \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east); 
  \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east);

  \node [tape, draw,minimum size=1cm,tape bend top=none,
tape bend height=0.4cm,rotate=90] at (9.3,0) (t) {};
  \node [tape, draw,minimum size=1cm,tape bend top=none,
tape bend height=0.4cm,rotate=270] at (11.3,0) (t) {};

\node at (10.3,0.5) {\dots};
\node at (10.3,-0.5) {\dots};

\foreach \c/\i [count=\m] in  
        {olive!20/0111,green!20/1111} 
           \node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=10.6cm+\m*2cm](N\m){\i} ;
  \draw [decoration={brace,mirror,raise=5pt},decorate] (N2.south west) --  node[below=10pt]{$M_d$}(N2.south east); 

 \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
 \foreach \c/\i [count=\n] in  
        {blue!20/0001,red!20/0001,red!20/0010,red!20/0100,white/\dots,yellow!20/0100} 
           \node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;

 \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east); 
 \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east);

\end{tikzpicture}
\end{document}

在此处输入图片描述

更新 :Gonzalo 比我快!下面的代码不完整,当我看到 Gonzalo 的好答案时,我停止了。我提出我的想法是因为我发现了一个很大的困难。在我的想法中,连接框包含数字。结果如下:

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,shapes}
\begin{document}
\begin{tikzpicture}
   \node[tape, draw,fill=red!20, tape bend top=none,rotate=90,minimum width=1cm,minimum height=2cm] at (7.92, 0) {};  
  \node[tape, draw,fill=yellow!20, tape bend bottom=none,rotate=90,minimum width=1cm,minimum height=2cm] at (12, 0) {};   
 \foreach \c/\i/\b [count=\n] in  
        {blue!20/0001/1,red!20/0001/1,red!20/0010/1,/0100/0,//0,/0100/0} 
{\ifnum \b=1
  \node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;
  \else  
    \node[minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;  
  \fi }

 \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east); 
  \draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east); 

\end{tikzpicture}
\end{document} 

困难是确实存在的!因为我无法旋转带有数字的节点。

在此处输入图片描述

答案3

另一种选择,无需使用tikz/pgf

在此处输入图片描述

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newsavebox{\tempbox}
\newcommand{\cbox}[2]{%
  \fcolorbox{black}{#1}{\texttt{#2\strut}}\kern-\fboxrule}% Coloured box
\begin{document}
\[
  \savebox{\tempbox}{\cbox{white}{~~~~~~}}
  \underbrace{\cbox{red!30}{0000}}_{M_0}\!
  \underbrace{\cbox{green!20}{0001}
    \cbox{green!20}{0010}
    \cbox{green!20}{0100}}_{M_1}\!
  \rlap{\usebox{\tempbox}}%
  \makebox[\wd\tempbox][c]{%
    \fcolorbox{white}{white}{$\!\strut\cdot\!$}~%
    \fcolorbox{white}{white}{$\!\strut\cdot\!$}~%
    \fcolorbox{white}{white}{$\!\strut\cdot\!$}}\!
  \cbox{white}{0111}\!
  \underbrace{\cbox{blue!40}{1111}}_{M_d}
\]
\end{document}​

宏以适当的颜色(框架)\cbox{<color>}{<stuff>}排版。将框架颜色修改为并将其多次叠加在放大的矩形上(略有间隔),可提供仿虚线样式来“打破”序列。<stuff>\fcolorbox<color>white

答案4

我经常寻找创建“字节数组”或“正方形数组”(或者更确切地说是“矩形数组”)作为矢量图形的方法 - 在 Tikz 中实现此目的的一种方法是使用rectangle split形状。(也可以使用包D中的符号tikz-timing,但我将在此跳过)。

rectangle split下面是使用来实现该效果的代码:

测试31.png

\documentclass{article}
\usepackage{calc} %\widthof
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}

\newlength\tmpl
\setlength{\tmpl}{\widthof{0000}} % \settowidth{\tmpl}{0000} gives 0.0
\typeout{\tmpl \the\tmpl} % \tmpl 20.00006pt
\tikzstyle{nn} = [inner sep=0pt,outer sep=0pt, text width=\tmpl, align=center]
\tikzstyle{db} = [decoration={brace,mirror,raise=5pt},decorate]

\coordinate[] (origin) at (0,0);

\node[
  rectangle split,
  rectangle split horizontal=true,
  rectangle split empty part width=\tmpl,
  rectangle split parts=7,
  %rectangle split use custom fill,%=true, % documented, but nowork tikz 1.5.1
  rectangle split part fill={blue!20,red!20,red!20,red!20,none,olive!20,green!20},
  line width=1pt,
  draw,
] (barr1) [above=5pt of origin]
{%
\nodepart[nn,fill=red!50]{one}0001% fill is ignored here!
\nodepart[nn]{two}0001%
\nodepart[nn]{three}0010%
\nodepart[nn]{four}0100%
\nodepart[nn]{five}...%
\nodepart[nn]{six}0100%
\nodepart[nn]{seven}1111%
};

\draw [db] (barr1.south west) -- node[below=10pt]{$M_0$} (barr1.one split south);
\draw [db] (barr1.one split south) -- node[below=10pt]{$M_1$} (barr1.four split south);
\draw [db] (barr1.six split south) -- node[below=10pt]{$M_d$} (barr1.south east);

\end{tikzpicture}
\end{document}

相关内容