5

I am trying to make the Cayley graph of a free group of rank 2 in TikZ. The result I would like to obtain looks like this:

enter image description here

but arrows are not required. I read Cayley Graph of Free Group in TikZ and tried to figure out Lindemayer systemts, as this is what most answers go by. However, this goes way over my head and I also could not figure out how to use the different coloring (which is the most important feature I am after).

As I don't know how to adjust the Lindemayer system answers to get the different coloring, there is little advantage of giving a minimal working example, as I simply copied one of the answers in the linked question and tried to figure out the code and how to add the color. Any help would be appreciated.

EDIT I also tried to do some work manually and than rotate the whole: I defined the nodes on the right manually, than tried using the rotation macro, but this did not work, I can include this:

  \begin{standalone}
  \usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,0);
\coordinate (C) at (3,0);
\coordinate (D) at (3.5, 0);
\coordinate (E) at (2,1);
\coordinate (F) at (2,1.5);
\coordinate (G) at (2,-1);
\coordinate (H) at (2,-1.5);
\coordinate (I) at (1.5, -1);
\coordinate (J) at (2.5, -1);
\coordinate (K) at (1.5, 1);
\coordinate (L) at (2.5, 1);
\coordinate (M) at (3, 0.5);
\coordinate (N) at (3,-0.5);
\foreach \angle \in {0,90,180,-90}
\begin{scope}[rotate=\angle]
\foreach \point in {A,B,..., N}
\fill [black] (\point) circle (2pt);
\end{scope}
\end{tikzpicture}
\end{document}

but this did not work, I only received the nodes I initially defined... (I am very new to TikZ as you can see, I tried looking up the scope environment in the PGF manuel to see what I did wrong, but I think I don't really understand this environment)

2

The lindenmayersystems library seems the way to go here. A variation from Cayley Graph of Free Group in TikZ. The caveat is to change color of the style depending on the quadrant:

\documentclass[tikz,border=5]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{lindenmayersystems,arrows.meta}
\newcount\quadrant
\pgfdeclarelindenmayersystem{cayley}{
  \rule{A -> B [ R [A] [+A] [-A] ]}
  \symbol{R}{ \pgflsystemstep=0.5\pgflsystemstep } 
  \symbol{-}{
    \pgfmathsetcount\quadrant{Mod(\quadrant+1,4)}
    \tikzset{rotate=90}
  }
  \symbol{+}{
    \pgfmathsetcount\quadrant{Mod(\quadrant-1,4)}
    \tikzset{rotate=-90}
  }
  \symbol{B}{
    \ifnumodd{\quadrant}{%true
        \tikzset{dot-cayley/.append style={draw=red}}
    }{%false
        \tikzset{dot-cayley/.append style={draw=blue}}
    }   
    \draw [dot-cayley] (0,0) -- (\pgflsystemstep,0) 
       node [font=\footnotesize, midway, 
         anchor={270-mod(\the\quadrant,2)*90}, inner sep=.5ex] 
           {};
    \tikzset{xshift=\pgflsystemstep}
  }
}
\tikzset{
  dot/.tip={Circle[sep=-1.5pt,length=3pt]}, cayley/.tip={dot[]}
}
\begin{document}
\begin{tikzpicture}
\draw l-system [l-system={cayley, axiom=[A] [+A] [-A] [++A], step=5cm, order=4}];
\end{tikzpicture}
\end{document}

The result:

enter image description here

With black dots at the end of the segment:

\documentclass[tikz,border=5]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{lindenmayersystems,arrows.meta}
\newcount\quadrant
\pgfdeclarelindenmayersystem{cayley}{
  \rule{A -> B [ R [A] [+A] [-A] ]}
  \symbol{R}{ \pgflsystemstep=0.5\pgflsystemstep } 
  \symbol{-}{
    \pgfmathsetcount\quadrant{Mod(\quadrant+1,4)}
    \tikzset{rotate=90}
  }
  \symbol{+}{
    \pgfmathsetcount\quadrant{Mod(\quadrant-1,4)}
    \tikzset{rotate=-90}
  }
  \symbol{B}{
    \ifnumodd{\quadrant}{%true
        \tikzset{dot-cayley-color/.style={draw=red}}
    }{%false
        \tikzset{dot-cayley-color/.style={draw=blue}}
    }   
    \draw [dot-cayley,dot-cayley-color] (0,0) -- (\pgflsystemstep,0) 
       node [font=\footnotesize, midway, 
         anchor={270-mod(\the\quadrant,2)*90}, inner sep=.5ex] 
           {};
    \tikzset{xshift=\pgflsystemstep}
  }
}
\tikzset{
  dot/.tip={Circle[sep=-1.5pt,length=3pt,color=black]}, cayley/.tip={dot[color=black]}
}
\begin{document}
\begin{tikzpicture}
\draw l-system [l-system={cayley, axiom=[A] [+A] [-A] [++A], step=5cm, order=4}];
\end{tikzpicture}
\end{document}

The result:

enter image description here

  • Thank you! Would it be easy to add the black nodes using this code? Or could I somehow use the small part of code I had to add those? – Student May 30 '18 at 12:02
  • Yes, it is quite easy. I'll add a revision of the code for this – Claudio Fiandrino May 30 '18 at 12:03
  • I have increased your score +1. Very good. – Sebastiano May 30 '18 at 19:42
4

Here is a brute force example. The idea is that it is so simple that you can easily adjust it to your needs.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[line width=1pt,scale=0.5]
\pgfmathsetmacro{\Radius}{4}
\foreach \X in {0,...,3}
{\coordinate (X\X) at ({\X*90}:\Radius);
\ifodd\X
\draw[blue](0,0) -- (X\X);
\else
\draw[red](0,0) -- (X\X);
\fi
\foreach \Y in {1,2,3}
{
\pgfmathtruncatemacro{\sumOne}{\X+\Y}
\ifodd\sumOne
\draw[blue] (X\X) -- ++({(\X+\Y-2)*90}:{\Radius/2.5}) coordinate (X\X-\Y);
\else
\draw[red] (X\X) -- ++({(\X+\Y-2)*90}:{\Radius/2.5}) coordinate (X\X-\Y);
\fi
\foreach \Z in {1,2,3}
{
\pgfmathtruncatemacro{\sumTwo}{\X+\Y+\Z}
\ifodd\sumTwo
\draw[blue] (X\X-\Y) -- ++({(\X+\Y+\Z-4)*90}:{\Radius/2.5^2}) coordinate (X\X-\Y-\Z);
\else
\draw[red] (X\X-\Y) -- ++({(\X+\Y+\Z-4)*90}:{\Radius/2.5^2}) coordinate (X\X-\Y-\Z);
\fi
\foreach \V in {1,2,3}
{
\pgfmathtruncatemacro{\sumThree}{\X+\Y+\Z+\V}
\ifodd\sumThree
\draw[blue] (X\X-\Y-\Z) -- ++({(\X+\Y+\Z+\V-2)*90}:{\Radius/2.5^3}) coordinate
(X\X-\Y-\Z-\V);
\else
\draw[red] (X\X-\Y-\Z) -- ++({(\X+\Y+\Z+\V-2)*90}:{\Radius/2.5^3}) coordinate
(X\X-\Y-\Z-\V);
\fi
}
}
}
}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you! I'll have to take a closer look at it, trying to figure out what exactly is going on. One last question: would it be easy to add the black dots at branch points? Could I use any of the 'code' I wrote for this? – Student May 30 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.