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

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

How to create two copies of the Sierpinski triangle?

Postby moitetajni » Wed May 11, 2016 9:15 pm

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:

  1. \documentclass[border=5mm]{standalone}
  2. \usepackage{tikz}
  3.  
  4. \usetikzlibrary{lindenmayersystems}
  5.  
  6. \begin{document}%
  7. \def\trianglewidth{2cm}%
  8. \pgfdeclarelindenmayersystem{Sierpinski triangle}{
  9. \symbol{X}{\pgflsystemdrawforward}
  10. \symbol{Y}{\pgflsystemdrawforward}
  11. \rule{X -> X-Y+X+Y-X}
  12. \rule{Y -> YY}
  13. }%
  14.  
  15. \tikzset
  16. {
  17. l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
  18. }
  19.  
  20. \begin{tikzpicture}[scale=2]
  21. \fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
  22. \draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
  23. \end{tikzpicture}
  24. \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:

  1. \documentclass[border=5mm]{standalone}
  2. \usepackage{tikz}
  3.  
  4. \usetikzlibrary{lindenmayersystems}
  5.  
  6. \begin{document}%
  7. \def\trianglewidth{2cm}%
  8. \pgfdeclarelindenmayersystem{Sierpinski triangle}{
  9. \symbol{X}{\pgflsystemdrawforward}
  10. \symbol{Y}{\pgflsystemdrawforward}
  11. \rule{X -> X-Y+X+Y-X}
  12. \rule{Y -> YY}
  13. }%
  14.  
  15. \tikzset
  16. {
  17. l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
  18. }
  19.  
  20. \begin{tikzpicture}[scale=2]
  21. \fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
  22. \draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
  23.  
  24. % It does not look like the Sierpinski triangle
  25. \fill [black] (3,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
  26. \draw [draw=none] (3,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
  27. \end{tikzpicture}
  28. \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/the-first-six-levels-of-the-sierpinski-triangle/gqwmqrhmphxv#.VzOB36GVvvx and was created by Jake on TeX SE.

Tags:

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

Postby mas » Thu May 12, 2016 2:51 am

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

  1. \documentclass[border=5mm]{standalone}
  2. \usepackage{tikz}
  3.  
  4. \usetikzlibrary{lindenmayersystems}
  5.  
  6. \begin{document}%
  7. \def\trianglewidth{2cm}%
  8. \pgfdeclarelindenmayersystem{Sierpinski triangle}{
  9. \symbol{X}{\pgflsystemdrawforward}
  10. \symbol{Y}{\pgflsystemdrawforward}
  11. \rule{X -> X-Y+X+Y-X}
  12. \rule{Y -> YY}
  13. }%
  14.  
  15. \tikzset
  16. {
  17. l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
  18. }
  19.  
  20. \begin{tikzpicture}[scale=2]
  21.  
  22. \fill [black] (0,0) -- ++(0:\trianglewidth) --
  23. ++(120:\trianglewidth) -- cycle;
  24.  
  25. \draw [draw=none] (0,0) l-system
  26. [l-system={Sierpinski triangle, axiom=X},fill=white];
  27.  
  28. \begin{scope}[xshift=3cm]
  29. \fill [black] (0,0) -- ++(0:\trianglewidth) --
  30. ++(120:\trianglewidth) -- cycle;
  31.  
  32. \draw [draw=none] (0,0) l-system
  33. [l-system={Sierpinski triangle, axiom=X},fill=white];
  34.  
  35. \end{scope}
  36. \end{tikzpicture}
  37. \end{document}


x.png
x.png (8.92 KiB) Viewed 2302 times


You can also use the pic method of repeating an object.

  1. \documentclass[border=5mm]{standalone}
  2. \usepackage{tikz}
  3.  
  4. \usetikzlibrary{lindenmayersystems}
  5.  
  6. \begin{document}%
  7. \def\trianglewidth{2cm}%
  8. \pgfdeclarelindenmayersystem{Sierpinski triangle}{
  9. \symbol{X}{\pgflsystemdrawforward}
  10. \symbol{Y}{\pgflsystemdrawforward}
  11. \rule{X -> X-Y+X+Y-X}
  12. \rule{Y -> YY}
  13. }%
  14.  
  15. \tikzset
  16. {
  17. l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
  18. }
  19.  
  20. \tikzset{
  21. sierp/.pic={
  22. \fill [black] (0,0) -- ++(0:\trianglewidth) --
  23. ++(120:\trianglewidth) -- cycle;
  24.  
  25. \draw [draw=none] (0,0) l-system
  26. [l-system={Sierpinski triangle, axiom=X},fill=white];
  27. }}
  28.  
  29.  
  30. \begin{tikzpicture}[scale=2]
  31.  
  32. \pic at (0,0) {sierp} ;
  33. \pic at (1,1) {sierp} ;
  34.  
  35. \end{tikzpicture}
  36. \end{document}


x2.png
x2.png (16.43 KiB) Viewed 2300 times

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim

User avatar
Johannes_B
Site Moderator
Posts: 4118
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu May 12, 2016 9:02 am

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 2 guests