13

We know that 1/2 - 1/3 = 1/6. Now I use this picture to show. enter image description here

How to add minus and equal signs between two rectangles?

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

\begin{document}
\Blocks[3]{2}{(1,2),(2,2),(3,2)}
\Blocks[3]{2}{(1,2),(2,2)}
\Blocks[3]{2}{(1,2)}
\end{document} 
14

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} 
  • 48 sec too slow :( – Skillmon likes topanswers.xyz Jul 23 '18 at 9:19
  • @Skillmon: :-). Identical answer? Am sure there are other ways to do this. – Peter Grill Jul 23 '18 at 9:21
  • Yep, \vcenter. At first I thought about defining a \vertcenter macro for text mode, that's why I was to slow when I finally realized I could put the \Blocks in math mode :) – Skillmon likes topanswers.xyz Jul 23 '18 at 9:23
  • @Skillmon: You could probably also do this in text mode with a raisebox with a negative value of half the height. – Peter Grill Jul 23 '18 at 9:24
  • You should probably roll \vcenter{\hbox ... }} into the macro? Then the document would still be the same. – joojaa Jul 23 '18 at 9:27
4

The simple figure should be drawn by simple code.

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

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

\begin{scope}[shift={(-3-\a,0)}]
\fill[pattern=north east lines] (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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.