4

I want to use latex to generate similar figure as following plot enter image description here using tikzpicture.

For instance using the example given here

\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}

I can generate grids but how can I add labels and put the grids all together? Is it possible to do it via latex?

Dalek
  • 509
  • @marmot I want to generate a similar figure as what I posted but with different variables and stuff with latex if it is possible. – Dalek Jun 08 '18 at 20:39

1 Answers1

3

Here is a proposal: define an appropriate pic and use it. This allows you to put stuff around it with the positioning library.

enter image description here

\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}