为此,我借用了网上找到的一些代码,并尝试对其进行了一些修改以满足我的需求。这是我编辑的代码版本:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\trianglewidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
\symbol{X}{\pgflsystemdrawforward}
\symbol{Y}{\pgflsystemdrawforward}
\rule{X -> X-Y+X+Y-X}
\rule{Y -> YY}
}%
\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}
\begin{tikzpicture}[scale=2]
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{tikzpicture}
\end{document}
但是,我不知道如何在同一tikzpicture
环境中创建谢尔宾斯基三角形的第二个副本(minipage
对我的目的不起作用)。我尝试在环境中创建代码的副本tikzpicture
,然后修改一些参数,但没有成功。这是该尝试的代码:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\trianglewidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
\symbol{X}{\pgflsystemdrawforward}
\symbol{Y}{\pgflsystemdrawforward}
\rule{X -> X-Y+X+Y-X}
\rule{Y -> YY}
}%
\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}
\begin{tikzpicture}[scale=2]
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
% It does not look like the Sierpinski triangle
\fill [black] (3,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (3,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{tikzpicture}
\end{document}
所以我的问题是:如何修改代码以获取谢尔宾基三角形的两个副本?
笔记:代码取自这里并由创建Jake 在 TeX SE 上。
答案1
使用范围环境
\documentclass[border=5mm]{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\trianglewidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
\symbol{X}{\pgflsystemdrawforward}
\symbol{Y}{\pgflsystemdrawforward}
\rule{X -> X-Y+X+Y-X}
\rule{Y -> YY}
}%
\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}
\begin{tikzpicture}[scale=2]
\begin{scope}
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{scope}
\begin{scope}[shift={(5cm,0)}]
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{scope}
\end{tikzpicture}
\end{document}