I want to generate the following figure in LaTeX.

I know TikZ a little, so I need ideas to reproduce the same.


  • 5
    Hi. Can you add an MWE of what you have done, so that we can work from there. Dec 21, 2016 at 5:17
  • 1
    If you don't know how to create a minimal working example (MWE), see this link.
    – CarLaTeX
    Dec 21, 2016 at 7:36
  • Why not use Inkscape? Construct the graph and export in .tex. See at the link tex.stackexchange.com/questions/61274/…. It is very easy. Or you can use IPE.
    – Sebastiano
    Dec 21, 2016 at 8:48
  • I did this using latex tables, but the code is to messy to make changes, Moreover the width of empty cells is varying after compilation, although I am fixing it. I am wondering if this can be done using Tikz in an easy way.
    – Astro
    Dec 21, 2016 at 9:59
  • 1
    @VinayakAbrol Could you please change the question title to make it informative for future readers in view of the answers?
    – Diaa
    Dec 24, 2016 at 8:53

3 Answers 3


You can start with some matrix of nodes:

\usetikzlibrary{positioning, matrix}


\matrix (data) [matrix of nodes, 
nodes={draw, anchor=center, inner sep=1pt, 
        minimum height=2cm, minimum width=6mm},
        column sep=-\pgflinewidth, row sep=-\pgflinewidth]
$y_1$ & $y_2$ & $y_3$ & $\dots$ & $y_n$ \\

\matrix (Coef) [right=of data, matrix of nodes, 
     nodes in empty cells, nodes={draw, anchor=center, inner sep=1pt, 
                 minimum size=6mm},
     column sep=-\pgflinewidth, row sep=-\pgflinewidth]
|[nz]| & & & & $a_{1n}$ \\
& & & |[nz]| & \\

\draw[shorten >=1mm, shorten <=1mm] (Coef-1-1.north west)--++(90:4mm) -| (Coef-1-5.north east) node [fill=white, pos=.25] {A};


enter image description here


I don't know if OP had enough time to learn a little bit of TiKZ, but it's Christmas and I've had some time to procrastinate ;-)

\usetikzlibrary{positioning, matrix, arrows.meta}

    zero/.style={draw, minimum size=6mm, 
        inner sep=1pt, anchor=center},
    data/.style={draw, minimum width=6mm,
        minimum height=2cm, inner sep=1pt,
    mymatrix/.style={matrix of math nodes,
        column sep=-\pgflinewidth,
        row sep=-\pgflinewidth,
        inner sep=0pt},
    vector/.style={mymatrix, nodes=data},
    coeff/.style={mymatrix, nodes=zero, nodes in empty cells},

\matrix (data) [vector,
    label={[name=Y, label=data]Y},
    label={[name=n, label=below:samples]below:n},
y_1 & y_2 & y_3 & \dots & y_n \\
\draw[shorten <=1mm] (data.north west) |- (Y);
\draw[shorten <=1mm] (data.north east) |- (Y);
\draw[->] (n)--(n-|data.east);
\draw[->] (n)--(n-|data.west);
\draw[->] (m)--(m|-data.north);
\draw[->] (m)--(m|-data.south);

\matrix (dict) [vector,
    right=8mm of data,
    label={[name=D, label=dictionary]D},
    label={[name=p, label=below:factors (atoms)]below:p},
d_1 & d_2 & d_3 & d_4 & d_5 & \dots & d_p \\
\draw[shorten <=1mm] (dict.north west) |- (D);
\draw[shorten <=1mm] (dict.north east) |- (D);
\draw[->] (p)--(p-|dict.east);
\draw[->] (p)--(p-|dict.west);

\matrix (Coef) [coeff,
    above right= 0 and 8mm of dict.south east,
    label={[name=n, label=below:coefficients]below:n},
|[nz]| & & & & a_{1n} \\
& & & |[nz]| & \\
& |[nz]| & & & \\
& |[nz]| & & & |[nz]| \\
|[nz]| & & & & |[nz]| \\
& & |[nz]| & |[nz]| & \\
a_{p1} & & |[nz]| & & a_{pn}\\
\draw[shorten <=1mm] (Coef.north west) |- (A);
\draw[shorten <=1mm] (Coef.north east) |- (A);
\draw[->] (p1)--(p1|-Coef.north);
\draw[->] (p1)--(p1|-Coef.south);
\draw[->] (n)--(n-|Coef.east);
\draw[->] (n)--(n-|Coef.west);

\path (data.east)-- node {$=$} (dict.west);
\path (dict.east)-- node {$\times$} (dict-|Coef.west);

\node[zero, above left=8mm and 0 of data.north east, label=right:zero] (z1) {};
\node[zero, nz, above=-\pgflinewidth of z1.north, label=right:nonzero] (nz1) {};

enter image description here


A PSTricks solution:


\usepackage{geometry} % to avoid `overfull \hbox' warning
\psset{dimen = m}

% simplifies code

  \multido{\r = \fpeval{0.4+0.7*(#1-1)+(#2)*\width}+\width}{#3}{%
  \multido{\r = \fpeval{0.4+0.7*(#1-1)+(#2+0.5)*\width}+\width, \i = 1+1}{%

  \pcline{<->}(\fpeval{0.4+0.7*(#1-1)+(#2)   *\width},0.5)%


  \pnode(\fpeval{0.4+(#1-1)*0.7+(#2)   *\width},\fpeval{0.75+(#4)*\width}){#5}
  \ncbar[angle = 90]{#5}{#6}

    fillstyle = solid,
    fillcolor = #3




% color

% parameters


  \labelH{2}[\blocksA](\blocksB){p}{factors (atoms)}
  \multido{\rA = \fpeval{1.8+(\blocksA+\blocksB+1)*\width}+\width}{\blocksA}{%
    \multido{\rB = \fpeval{0.75+\width}+\width}{\blocksB}{%



All you have to do is change the values of the parameters, and the drawing will be adjusted accordingly.

  • Nice @Svend ! Right now its a bit complicated, but I will consider this in future.
    – Astro
    Dec 22, 2016 at 6:00
  • Tveskæg my best compliments for the job. You are very good.
    – Sebastiano
    Dec 23, 2016 at 23:01
  • I know pstricks but I not understand because just about only TikZ. I have upvoted, immediately. Was 5 and now 6. Merry Christmas for you and your family.
    – Sebastiano
    Dec 23, 2016 at 23:22

Not adding much to the previous answers, except a different way of drawing the cells in the grid.

  minimum height=8em, minimum width=2em,
  inner sep=0pt,      outer sep=0pt,
every block/.style={
  inner sep=0, minimum size=2em, text depth=0
block 0/.style={every block/.try},
block 1/.style={every block/.try, fill=red!50!black,
grid lines/.style={draw=black, thick},
offset to/.style args={#1 and #2}{to path={
  ([shift={(#1,#2)}]\tikztostart) -- ([shift={(#1,#2)}]\tikztotarget)
offset y/.style={offset to=0 and #1}, offset x/.style={offset to=#1 and 0}
\begin{tikzpicture}[start chain=going {at=(\tikzchainprevious.south east)},
  anchor=south west,align=center, line cap=round, line join=round,
  >=Triangle, every node/.style={align=center}, x=2em, y=2em]
\begin{scope}[local bounding box=data]
\foreach \y in {1,2,3,-,n}
  \node [on chain, cell] (y-\y) {$\expandafter\if\y-\ldots\else y_{\y}\fi$};
\node [on chain, cell, draw=none] (equals) {$=$};
\begin{scope}[local bounding box=dictionary]
\foreach \d in {1,2,3,4,5,-,p}
  \node [on chain, cell] (d-\d) {$\expandafter\if\d-\ldots\else d_{\d}\fi$};
\node [on chain, cell, draw=none] (times) {$\times$};
\coordinate [on chain] (grid origin);
\begin{scope}[shift=(grid origin),  local bounding box=grid]
\foreach \k [count=\y from 0] in {4,6,17,9,8,2,16}
   \foreach \x [evaluate={\c=int(mod(int(\k / (2^\x)), 2));}] in {0,...,4}
     \node [block \c/.try] (a-\y-\x) at (4-\x, \y) {};
\draw [grid lines/.try, step=2em] grid ++(5,7);
\node [every block] at (4,6) {$a_{1n}$};
\node [every block] at (0,0) {$a_{p1}$};
\node [every block] at (4,0) {$a_{pn}$};

\draw [{Bar[left]}-{Bar[right]}] (data.north west) to [offset y=1/4]
  node [midway, above] {Data \\ $Y$} (data.north east);
\draw [<->] (data.south east) to [offset y=-1/4] 
  node [midway, below] {$n$ \\ samples} (data.south west);
\draw [<->] (data.south west) to [offset x=-1/4] 
  node [midway, left] {$m$}  (data.north west);

\draw [{Bar[left]}-{Bar[right]}] (dictionary.north west) to [offset y=1/4]
  node [midway, above] {Dictionary \\ $D$} (dictionary.north east);
\draw [<->] (dictionary.south west) to [offset y=-1/4]
  node [midway, below] {$p$ \\ factors (atoms)} (dictionary.south east);

\draw [{Bar[left]}-{Bar[right]}] (grid.north west) to [offset y=1/4]
  node [midway, above] {$A$} (grid.north east);
\draw [Triangle-Triangle] (grid.south west) to [offset y=-1/4] 
  node [midway, below] {$n$ \\ coefficients} (grid.south east);
\draw [<->] (grid.south east) to [offset x=1/4] 
  node [midway, right] {$p$} (grid.north east);

\node [block 1, grid lines, label=0:non-zero] at ([shift={(0,5)}]y-n) {};
\node [block 0, grid lines, label=0:zero]     at ([shift={(0,4)}]y-n){};

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .