How to add minus and equal sign in this picture?

I would put the Blocks in math mode and use \vcenter:

enter image description here

Note:

  • I added the missing % following \end{tikzpicture}.

Code:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns}
\newcommand\Blocks[3][\relax]{% usage \Blocks[cols]{rows}{list of shaded cells}
   \begin{tikzpicture}[line join=miter,xshift=1.5cm, baseline]
     \def\colsForBlocks{#2}
     \ifx#1\relax\relax\def\rowsForBlocks{\colsForBlocks}
     \else\def\rowsForBlocks{#1}
     \fi
     % draw the grid
     \foreach \row in {0,...,\rowsForBlocks} {
        \draw[thick](\row,0)-- ++(0,\colsForBlocks);
     }
     \foreach \col in {0,...,\colsForBlocks} {
        \draw[thick](0,\col)-- ++(\rowsForBlocks,0);
     }
     % shade the specified boxes
     \foreach \cell in {#3} {
         \draw[thick,pattern = north east lines,line join=miter] \cell rectangle ++ (-1,-1);
     }
   \end{tikzpicture}% <-- was missing
}

\begin{document}
$
  \vcenter{\hbox{\Blocks[3]{2}{(1,2),(2,2),(3,2)}}}
- \vcenter{\hbox{\Blocks[3]{2}{(1,2),(2,2)}}}
= \vcenter{\hbox{\Blocks[3]{2}{(1,2)}}}
$
\end{document} 

The simple figure should be drawn by simple code.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[line join=round,pattern=north east lines]
\def\a{.6} % distance between blocks
\begin{scope}
\fill (0,1) rectangle (2,2);
\draw (0,0) grid (3,2);
\end{scope}

\begin{scope}[shift={(3+\a,0)}]
\fill (0,1) rectangle (1,2);
\draw (0,0) grid (3,2);
\end{scope}

\begin{scope}[shift={(-3-\a,0)}]
\fill (0,1) rectangle (3,2);
\draw (0,0) grid (3,2);
\end{scope}

\path (3+.5*\a,1) node{$=$} (-.5*\a,1) node{$-$};
\end{tikzpicture}
\end{document}

enter image description here

Tags:

Tikz Styles