mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-29 15:57:57 +02:00
Added Stern-Brocot-Tree
This commit is contained in:
parent
e0923f1d09
commit
23569bea72
4 changed files with 94 additions and 0 deletions
48
tikz/stern-brocot-tree/stern-brocot-tree.tex
Normal file
48
tikz/stern-brocot-tree/stern-brocot-tree.tex
Normal file
|
@ -0,0 +1,48 @@
|
|||
\documentclass[tikz]{standalone}
|
||||
\usepackage{forest}
|
||||
\makeatletter
|
||||
\pgfmathdeclarefunction{strrepeat}{2}{%
|
||||
\begingroup
|
||||
\pgfmathint{#2}\pgfmath@count\pgfmathresult
|
||||
\let\pgfmathresult\pgfutil@empty
|
||||
\pgfutil@loop
|
||||
\ifnum\pgfmath@count>0\relax
|
||||
\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult#1}%
|
||||
\advance\pgfmath@count-1\relax
|
||||
\pgfutil@repeat
|
||||
\pgfmath@smuggleone\pgfmathresult
|
||||
\endgroup}
|
||||
\makeatother
|
||||
\tikzset{
|
||||
Stern Brocot at/.style={at/.pgfmath={
|
||||
strcat("([rotate around=180:(!", strrepeat("#1",\SBLevel),")] !",
|
||||
strrepeat("#1",\SBLevel-1),"2)")}},
|
||||
Stern Brocot at*/.style n args={3}{
|
||||
at/.pgfmath={strcat("(!",strrepeat("#1",#2),"-| SB@#3)")},
|
||||
append after command/.expanded={\ifnum#2<\SBLevel(\noexpand\tikzlastnode)
|
||||
edge[densely dotted] (SB@#3@\the\numexpr\SBLLoop+1\relax)\fi}}}
|
||||
\begin{document}
|
||||
\begin{forest}
|
||||
Stern Brocot*/.style n args={2}{
|
||||
content=$\frac{#1}{#2}$,
|
||||
edge=densely dotted,
|
||||
if={level()<\SBLevel}{append={[,Stern Brocot*={#1}{#2}]}}{}},
|
||||
Stern Brocot/.style n args={5}{
|
||||
/utils/exec=\edef\SBLevel{#5},@Stern Brocot={#1}{#2}{#3}{#4}},
|
||||
@Stern Brocot/.style n args={4}{
|
||||
/utils/exec=\edef\SBTop{\number\numexpr#1+#3\relax}%
|
||||
\edef\SBBottom{\number\numexpr#2+#4\relax},
|
||||
content/.expanded=$\frac{\SBTop}{\SBBottom}$,
|
||||
if/.expanded={level()<\SBLevel}{% true
|
||||
append={[,@Stern Brocot={#1}{#2}{\SBTop}{\SBBottom}]},
|
||||
append={[,Stern Brocot*={\SBTop}{\SBBottom}]},
|
||||
append={[,@Stern Brocot={\SBTop}{\SBBottom}{#3}{#4}]}
|
||||
}{}}% false (empty)
|
||||
[,Stern Brocot={0}{1}{1}{0}{4}]
|
||||
%
|
||||
\coordinate[Stern Brocot at=1] (SB@left) coordinate[Stern Brocot at=3] (SB@right);
|
||||
\foreach \SBLLoop in {\SBLevel, ..., 0}
|
||||
\path node[Stern Brocot at*={1}{\SBLLoop}{left}] (SB@left@\SBLLoop) {$\frac01$}
|
||||
node[Stern Brocot at*={3}{\SBLLoop}{right}] (SB@right@\SBLLoop) {$\frac10$};
|
||||
\end{forest}
|
||||
\end{document}
|
Loading…
Add table
Add a link
Reference in a new issue