如何在此图片中添加减号和等号?

如何在此图片中添加减号和等号?

我们知道1/2 - 1/3 = 1/6。现在我用这幅图来展示。在此处输入图片描述

如何在两个矩形之间添加减号和等号?

我的代码

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

答案1

我会将其置于Blocks数学模式并使用\vcenter

在此处输入图片描述

笔记:

  • 我添加了下列缺失的%内容\end{tikzpicture}

代码:

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

答案2

简单的图形应该用简单的代码来绘制。

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

在此处输入图片描述

更新有趣的渐近线代码!

在此处输入图片描述

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));

相关内容