0

I want to let forest draw trees similar to following one automatically in a way, that changing the root of the tree will lead to the correct tree for this root. Example Tree

The root is any natural number. The rule is child1=floor((parent-1)/2) and child2=floor(parent/2).

For now I wrote a Fortran-95 programm that produces some Forest code, that makes me able to produce such trees. WideTree I would now like to do in a more elaborated way.

I found this code in another question and I think it is going in the right direction:

\begin{forest}
 Stern Brocot/.style n args={5}{%
  content=$\frac{\number\numexpr#1+#3\relax}
  {\number\numexpr#2+#4\relax}$,
  if={#5>0}{% true
    append={[,Stern Brocot={#1}{#2}{#1+#3}{#2+#4}{#5-1}]},
    append={[,Stern Brocot={#1+#3}{#2+#4}{#3}{#4}{#5-1}]}
       }{}}% false (empty)
   [,Stern Brocot={0}{1}{1}{0}{3}]
\end{forest}

But I am not able to understand how these #1,#2,... are working.

  • Welcome! What do you have so far? Right now, this is just another do-it-for-me without any ducks. – cfr Jul 21 '17 at 17:21
  • I read the first answer of tex.stackexchange.com/questions/106895/…. I also read the tutorial from the forest documentation. But I have problems understanding how these features of Forest are working. What i have right now is a small program that writes the tree in Forestsyntax into a file. – Imperaton Jul 21 '17 at 17:28
  • I actually just want to find out how I can do something like this, without browsing and reading around for more hours, An example of something similar with an explanation would also be sufficient. But I didn't find any. – Imperaton Jul 21 '17 at 17:36
  • Please always post complete code people can compile and include links in your question. Comments are liable to deletion on a regular basis. – cfr Jul 21 '17 at 19:30
  • The hashes are just the place holders for the arguments, as in regular macro definitions. The style shown takes 5 arguments, so the place holders are #1, #2, #3, #4 and #5. In the example, these gets the values 0, 1, 1, 0 and 3 respectively. – cfr Jul 21 '17 at 19:42
5

Note that I think I ought not answer do-it-for-mes, but sometimes do so anyway. When I do so, I do so for me. If the result happens to be of use, so be it. If not, not. Having started from scratch, I am typically less sympathetic to requests for fine-tuning, adjustments, explanations and other follow-up. Fine-tuning and adjustments are typically left as an exercise for the under-exercised reader. Explanations are for specific questions about particular problems, obstacles or code, where a user asks a question which lets me explain this bit, this change or this step. Since do-it-for-mes don't post code, there is no starting point from which to begin an explanation. In any case, I answer these solely for me. Any explanation is for me.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
  parhau/.style={
    delay={
      tempcounta/.process={O+nw+P {content}{int(floor((##1-1)/2))}},
      tempcountb/.process={O+nw+P {content}{int(floor(##1/2))}},
      if={
        >Rn>{tempcounta}{0}
      }{
        prepend/.process={ RSn=?_ lw2  {tempcounta}{1}{}{parhau}{[##2, ##1]} },
      }{},
      append/.process={ RSn=?_ lw2  {tempcountb}{1}{}{parhau}{[##2, ##1]} },
    },
  },
}
\begin{document}
\begin{forest}
  parhau
  [20]
\end{forest}
\end{document}

<code>parhau</code> recursion

  • Togheter with the Forest Documentation I was able to learn alot from this. What still confuses me: What is the job of the first '>' in this line: >Rn>{tempcounta}{0}. – Imperaton Jul 21 '17 at 21:56
  • @Imperaton See page 64. It is not using the .process handler, but the generic <forestmath>. Hence the switch. – cfr Jul 21 '17 at 23:45
  • Do you always use ##1,##2,... (and not #1,#2) for the wrap process, or does it have a special reason here? – Imperaton Jul 22 '17 at 6:40
  • @Imperaton No. It is in a style, so one hash would be an argument passed to the style. – cfr Jul 22 '17 at 13:06

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.