\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\newcommand{\Domino}[2]{%
\foreach \X/\Y [count=\Z] in {#1}
{\ifnum\Z=1
\draw[thick,fill=\Y] (0,0) rectangle (\X,1);
\draw[thick,latex-latex] (0,0.5) -- (\X,0.5) node[midway,fill=\Y]{#2};
\xdef\lastX{\X}
\else
\draw[thick,fill=\Y] (\lastX,0) rectangle ({\lastX+\X},1);
\pgfmathsetmacro{\lastX}{\lastX+\X}
\xdef\lastX{\lastX}
\fi
}}
\begin{tikzpicture}[font=\sffamily,fat arrow/.style={->,ultra thick,shorten
>=2pt, shorten <=2pt}]
\begin{scope}[local bounding box=left tiling]
\Domino{5/white}{$n$-tiling}
\end{scope}
\begin{scope}[xshift=6.5cm]
\begin{scope}[yshift=2cm,local bounding box=tiling 1]
\Domino{5/white,1/red,1/red}{$n$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 1.south east){Add two squares};
\begin{scope}[local bounding box=tiling 2]
\Domino{5/white}{$(n-1)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 2.south east){Remove a square};
\begin{scope}[yshift=-2cm,local bounding box=tiling 3]
\Domino{4/white,3/blue}{$(n+2)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 3.south east){Replace the square with a tromino};
\begin{scope}[yshift=-4cm,local bounding box=tiling 4]
\Domino{3/white,1/red,1/red}{$(n-5)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 4.south east)
{Replace with two squares};
**> \begin{scope}[yshift=-4cm,局部边界框=平铺 5]
\Domino{5/white}{$(n-10)$-tiling} \end{scope} \node[anchor=north east] at (tiling 5.south east) {Replace with a square};**
**
\end{scope}
\draw[decorate,very thick,decoration={brace}]
([xshift=-2pt]tiling 4.south west) --
([xshift=-2pt]tiling 3.north west) --
([xshift=-2pt]tiling 2.north west) coordinate[midway,left=3pt] (X);
\draw[fat arrow] (left tiling) -- (tiling 1.west);
\draw[fat arrow] (left tiling) -- (tiling 2.west);
\end{tikzpicture}
\end{document}
为什么我突出显示的代码部分没有生成另一个图像?每当我运行这个新代码时,我的最后一个平铺图像就会覆盖自身。
答案1
因为你忘了调整yshift
。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\newcommand{\Domino}[2]{%
\foreach \X/\Y [count=\Z] in {#1}
{\ifnum\Z=1
\draw[thick,fill=\Y] (0,0) rectangle (\X,1);
\draw[thick,latex-latex] (0,0.5) -- (\X,0.5) node[midway,fill=\Y]{#2};
\xdef\lastX{\X}
\else
\draw[thick,fill=\Y] (\lastX,0) rectangle ({\lastX+\X},1);
\pgfmathsetmacro{\lastX}{\lastX+\X}
\xdef\lastX{\lastX}
\fi
}}
\begin{tikzpicture}[font=\sffamily,fat arrow/.style={->,ultra thick,shorten
>=2pt, shorten <=2pt}]
\begin{scope}[local bounding box=left tiling]
\Domino{5/white}{$n$-tiling}
\end{scope}
\begin{scope}[xshift=6.5cm]
\begin{scope}[yshift=2cm,local bounding box=tiling 1]
\Domino{5/white,1/red,1/red}{$n$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 1.south east){Add two squares};
\begin{scope}[local bounding box=tiling 2]
\Domino{5/white}{$(n-1)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 2.south east){Remove a square};
\begin{scope}[yshift=-2cm,local bounding box=tiling 3]
\Domino{4/white,3/blue}{$(n+2)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 3.south east){Replace the square with a tromino};
\begin{scope}[yshift=-4cm,local bounding box=tiling 4]
\Domino{3/white,1/red,1/red}{$(n-5)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 4.south east)
{Replace with two squares};
\begin{scope}[yshift=-6cm,local bounding box=tiling 5]
\Domino{5/white}{$(n-10)$-tiling}
\end{scope}
\node[anchor=north east] at (tiling 5.south east)
{Replace with a square};
\end{scope}
\draw[decorate,very thick,decoration={brace}]
([xshift=-2pt,yshift=-2pt]tiling 4.south west) --
([xshift=-2pt,yshift=2pt]tiling 2.north west) coordinate[midway,left=3pt] (X);
\draw[fat arrow] (left tiling) to[out=90,in=180] (tiling 1.west);
\draw[fat arrow] (left tiling) to[out=-90,in=180] (X);
\draw[fat arrow] (left tiling) to[out=-90,in=180] (tiling 5.west);
\end{tikzpicture}
\end{document}