使用 tikz 制作一些网格的卡通图

使用 tikz 制作一些网格的卡通图

我想使用 latex 生成类似下图的图形在此处输入图片描述 使用tikzpicture

例如使用给出的例子这里

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \def\BITARRAY{
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
    {1,0,0,1,0},
    {1,1,1,0,0},
    {1,1,1,1,1},
    {0,0,1,1,1},
    {0,0,1,1,1}%
  }
  \fill[red]
    \foreach \row [count=\y] in \BITARRAY {
      \foreach \cell [count=\x] in \row {
        \ifnum\cell=1 %
          (\x-1, -\y+1) rectangle ++(1, -1)
        \fi
        \pgfextra{%
          \global\let\maxx\x
          \global\let\maxy\y
        }%
      }
    }
  ;
  \draw[thin] (0, 0) grid[step=1] (\maxx, -\maxy);
\end{tikzpicture}
\end{document}

我可以生成网格,但如何添加标签并将网格放在一起?是否可以通过 latex 完成?

答案1

这里有一个建议:定义一个合适的pic并使用它。这允许您使用定位库在其周围放置东西。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
% based on https://tex.stackexchange.com/questions/315313/dynamically-filling-a-grid-with-tikz-from-a-data-array?noredirect=1&lq=1 and https://tex.stackexchange.com/a/311441/121799
\tikzset{
  pics/BitArray/.style n args={2}{
  code={\begin{scope}[#2]
  \fill
    \foreach \row [count=\y] in {#1} {
      \foreach \cell [count=\x] in \row {
        \ifnum\cell=1 %
          (\x-1, -\y+1) rectangle ++(1, -1)
        \fi
        \pgfextra{%
          \global\let\maxx\x
          \global\let\maxy\y
        }%
      }
    }
  ;
  \draw[thin] (0, 0) grid[step=1] (\maxx, -\maxy);
  \end{scope}
  }
 }
}

\begin{document}
You can either work with \texttt{local bounding boxes} in a
\texttt{tikzpicture}\\
\begin{tikzpicture}
\begin{scope}[local bounding box=BA1]
 \pic {BitArray={%
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
    {1,0,0,1,0},
    {1,1,1,0,0},
    {1,1,1,1,1},
    {0,0,1,1,1},
    {0,0,1,1,1}%
  }{fill=red,scale=0.5}};
\end{scope}  
\node[above=1pt of BA1] {$k$};
\node[left=1pt of BA1.north west] {$X$};
\node[left=1pt of BA1.west] {$N$};

\begin{scope}[local bounding box=BA2,scale=0.5]
 \pic[right=3cm of BA1.north east] {BitArray={%
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
    {1,0,0,1,0},
    {1,1,1,0,0},
    {1,1,1,1,1},
  }{fill=blue,scale=0.5}};
\end{scope}  
\node[above=1pt of BA2] {$\ell$};
\end{tikzpicture}\\
or use equations
\[
\begin{tikzpicture}[baseline=(N.base)]
\begin{scope}[local bounding box=BA1,scale=0.5]
 \pic {BitArray={%
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
    {1,0,0,1,0},
    {1,1,1,0,0},
    {1,1,1,1,1},
    {0,0,1,1,1},
    {0,0,1,1,1}%
  }{fill=red,scale=0.5}};
\end{scope}  
\node[above=1pt of BA1] {$k$};
\node[left=1pt of BA1.north west] {$X$};
\node[left=1pt of BA1.west] (N) {$N$};
\end{tikzpicture}
~\sim~
\begin{tikzpicture}[baseline=(N2.base)]
\begin{scope}[local bounding box=BA2,scale=0.5]
 \pic[right=3cm of BA1.north east] {BitArray={%
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
    {1,0,0,1,0},
    {1,1,1,0,0},
    {1,1,1,1,1},
  }{fill=blue,scale=0.5}};
\end{scope}  
\node[above=1pt of BA2] {$\ell$};
\node[left=1pt of BA2.west] (N2) {$N$};
\end{tikzpicture}
~\dots~
\begin{tikzpicture}[baseline=(dots.base)]
\begin{scope}[local bounding box=BA3,scale=0.5]
 \pic[right=3cm of BA1.north east] {BitArray={%
    {0,0,1,1,1},
    {1,1,1,0,0},
    {0,0,1,1,1},
  }{fill=cyan,scale=0.5}};
\end{scope}  
\node[below=1pt of BA3.south] (dots) {$\vdots$};
\end{tikzpicture}
\]
\end{document}

相关内容