15

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} 

2 Answers 2

15

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} 
6
  • 48 sec too slow :(
    – Skillmon
    Jul 23, 2018 at 9:19
  • @Skillmon: :-). Identical answer? Am sure there are other ways to do this. Jul 23, 2018 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
    Jul 23, 2018 at 9:23
  • @Skillmon: You could probably also do this in text mode with a raisebox with a negative value of half the height. Jul 23, 2018 at 9:24
  • You should probably roll \vcenter{\hbox ... }} into the macro? Then the document would still be the same.
    – joojaa
    Jul 23, 2018 at 9:27
5

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

Update Asymptote code for fun!

enter image description here

unitsize(1cm);
size(8cm);
usepackage("amsmath");
import math;
import patterns;
pen p=blue,q=red;

add("hatch",hatch(1mm,q));
picture Lpic,Cpic,Rpic;

fill(Lpic,box((0,1),(3,2)),pattern("hatch"));
fill(Cpic,box((0,1),(2,2)),pattern("hatch"));
fill(Rpic,box((0,1),(1,2)),pattern("hatch"));

add(Lpic,grid(3,2));
add(Cpic,grid(3,2));
add(Rpic,grid(3,2));

pair D=(0,-1);
label(Lpic,"\boldmath $\dfrac12$",point(Lpic,S)+D,q);
label(Cpic,"\boldmath $\dfrac13$",point(Cpic,S)+D,q);
label(Rpic,"\boldmath $\dfrac16$",point(Rpic,S)+D,q);

real s=.8;
add(shift(-3-s,0)*Lpic);
add(Cpic);
add(shift(3+s,0)*Rpic);

label("\boldmath $-$",(-s/2,0)+D,p); 
label("\boldmath $-$",(-s/2,0)-D,p);
label("\boldmath $=$",(3+s/2,0)+D,p);
label("\boldmath $=$",(3+s/2,0)-D,p);

shipout(bbox(5mm,invisible));
0

You must log in to answer this question.

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