# Forest Tree Euclidean Division

I would like to draw trees like this:

In the first branch a child is calculated by Euclidean Division with the divisor 2 from its parent. In the second branch a child is calculated by Euclidean Division with divisor 3 from its parent. And so on up to the divisor floor(root/2)

This code is able to draw the first branch for arbitrary roots.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
div2/.style={
append/.process={ Ow+PS _>? l_w2 {content}{int(floor(##1/2))}{1}{div2}{}{[##1,##2]}},
},
[10, div2]
\end{forest}
\end{document}


To automatically draw the other branches I tried this:

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
divn/.style args={1} ={
append/.process={ Ow+PS _>? l_w2 {content}
{int(floor(##1/#1))}{1}{divn}{}{[##1,##2]}},
},
%
tempcounta = {1},
branches/.style={
until{2*tempcounta > content}{
divn={content},
tempcounta/.pgfmath={tempcounta+1},
},
},
%
%
[3, branches]
\end{forest}
\end{document}


EDIT: Based on the comments of cfr, I now have this:

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
divn/.style args={1} ={
append/.process={ Ow+PS _>? l_w2 {content}{int(floor(##1/#1))}{1}{divn={tempcounta}}{}{[##1,##2]}},
},
%
tempcounta = 1,
branches/.style={
until={<Rw+P O+P> {tempcounta}{##1*2}{content}}{
branches/.append style={divn={tempcounta}},
tempcounta'+=1,
},
},
%
%
[5, branches]
\end{forest}
\end{document}

• Related (I guess): tex.stackexchange.com/questions/381664/… – cfr Jul 23 '17 at 10:03
• Your first example doesn't work for me. – cfr Jul 23 '17 at 10:06
• For the first one, I guess you want append/.process={ O+nw+P Sn=?_ lw2 {content}{int(floor(##1/2))}{1}{}{div2}{[##2, ##1]} }, though I'm not sure about this. – cfr Jul 23 '17 at 10:20
• until needs an =. divn={content} passes the text content to the style divn which does nothing at all with whatever argument is passed to it. The incremented count tempcounta will not be an integer since .pgfmath will not yield an integer. tempcounta'+=1 would be better here. – cfr Jul 23 '17 at 10:25
• 2*tempcounta > content: neither tempcounta nor content is a number. These are just text strings. So > makes no sense and nor does 2*. 2*tempcounta is like 2*abcdefg i.e. undefined. – cfr Jul 23 '17 at 10:26

## 1 Answer

If you want to use Forest in this way, you will need to spend some more time with the manual and reading examples here and elsewhere. Some of the code in your question makes no sense at all: it doesn't even try to access the value, for example, but just feeds Forest the name of the key as regular text.

If you want to use a regular counter, I think you will need to use recursive delays. (I did this in my answer to your earlier question, although for different reasons.) Otherwise, the counter will not have the value you expect.

If, however, you can make do with the PGF maths function, forestloopcount, then it seems you can avoid the explicit introduction of delays. (I'm quite surprised this isn't needed even for the first iteration, since it uses the content of the root. But dynamic trees build in some delay automatically, if I'm remembering correctly (which I may well not be).

I think you want something like this:

This uses a different condition than the one you gave in order to get the right result, because the loop count gets incremented only once into the loop, I think. Simply increasing the count is too much. However, incrementing the doubled value should give the expected result - I think.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
div n/.style={% #1 is the value passed to the style; ##1/##2 is the child content; ##1 is another iteration, if applicable
append/.process={ Ow+PS n>? _lw2 {content}{int(floor(##1/#1))}{#1-1}{div n=#1}{}{[##2,##1]}},
},
div branches/.style={
until={% loop count is incremented only in the loop, so add 1 to compensate
> P+nO+n> {int((2*(forestloopcount))+1)}{content}
}{% we don't want a branch for 1 - the first loop should use 2 and so on
tempcounta/.pgfmath=forestloopcount,
tempcounta'+=1,
div n/.register=tempcounta,
},
}
}
\begin{document}
\begin{forest}
[3, div branches, baseline]
\end{forest}
\begin{forest}
[10, div branches, baseline]
\end{forest}
\begin{forest}
[20, div branches, baseline]
\end{forest}
\begin{forest}
[17, div branches, baseline]
\end{forest}
\end{document}

• where can I find documentation about \number\numexpr that is used in the process instruction n? What is the difference between tempcounta and tempcounta'? – Imperaton Jul 23 '17 at 15:52
• @Imperaton tempcounta'is faster, but less flexible. So it makes sense to use it when it will work. Basically, when setting a simple assignment or simple addition etc. (no PGF maths). PGF maths is s-l-l-o-o-o-w-w, so best avoided when possible. n does TeX maths, which is (relatively) fast. There's a bit in Forest's manual about it. See table 1 on page 65, which links to all the different argument processor options. Sometimes, though n is just to say 'this is a number' and not, say, text or a dimension. If you get complaints about comparisons of not making sense, specify their types. – cfr Jul 23 '17 at 16:17
• @Imperaton So, for example, I try to limit use of P, .pgfmath etc. because these are all slow. So is the default processing of something like if={condition} or until={} etc. because PGF maths is default. But .process, if={> condition}, until={>} etc. are (relatively) fast, because they are doing stuff at a lower level, without using PGF maths's machinery. If you don't care about efficiency, you can just use the PGF versions. However, in many cases, those are also less intuitive and less readable, you have to be careful if you need integers, spaces must be replaced in names etc. – cfr Jul 23 '17 at 16:24
• The forest doc states: "The result is the result of evaluating numexpr using eTEX’s \number\numexpr" I would like to know more about numexpr and dimexpr, but I am to able to find a proper explanation. – Imperaton Jul 23 '17 at 16:48
• @Imperaton Try the TeX Book or TeX by Topic - or eTeX's documentation (for the eTeX extensions). – cfr Jul 24 '17 at 1:38