Graphics, Figures & TablesHow to create two copies of the Sierpinski triangle?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
moitetajni
Posts: 2
Joined: Tue Aug 25, 2015 11:03 pm

How to create two copies of the Sierpinski triangle?

Post by moitetajni »

Hello. I am trying to create two copies of the Sierpinski triangle like this:
Image

For this I borrowed some code I found online and tried to modify it a little bit to meet my needs. Here's my edited version of the code:

Code: Select all

\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}
However, I don't know how to create the second copy of the Sierpinski triangle in the same tikzpicture enviroment (minipage does not work for my purpose). I tried to create a copy of the code inside the tikzpicture environment and then modify some parameters but it didn't work. This is the code of that attempt:

Code: Select all

\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}
So my question would be: how can I modify the code to obtain the two copies of the Sierpinki triangles?

Note: The code was taken from https://www.overleaf.com/latex/examples ... zOB36GVvvx and was created by Jake on TeX SE.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

How to create two copies of the Sierpinski triangle?

Post by mas »

You can repeat the code inside a scope environment with a shift in the x-direction.

Code: Select all

\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];

  \begin{scope}[xshift=3cm]
  \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}
x.png
x.png (8.92 KiB) Viewed 4448 times
You can also use the pic method of repeating an object.

Code: Select all

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

\tikzset{
  sierp/.pic={
    \fill [black] (0,0) -- ++(0:\trianglewidth) -- 
      ++(120:\trianglewidth) -- cycle;

    \draw [draw=none] (0,0) l-system 
      [l-system={Sierpinski triangle, axiom=X},fill=white];
}}


\begin{tikzpicture}[scale=2]

  \pic at (0,0) {sierp} ;
  \pic at (1,1) {sierp} ;

\end{tikzpicture}
\end{document}
x2.png
x2.png (16.43 KiB) Viewed 4446 times

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to create two copies of the Sierpinski triangle?

Post by Johannes_B »

The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply