# Draw tree with leaves at different levels

I'm trying to draw a tree similar to the one depicted below (content of nodes is different). My leaves are very wide and I need to pack them closer together by "lifting" every other leaf up.

Is it possible to tweak my code below to get my leaves to be on different levels and thus closer together, as depicted above?

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$}
[{$v_1$} ]
]
[{$r_{2,2} = r_{1,2} \bmod (x-2)$}
[{$v_2$} ]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$}
[{$v_3$} ]
]
[{$r_{4,4} = r_{3,4} \bmod (x-4)$}
[{$v_4$} ]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$}
[{$v_5$} ]
]
[{$r_{6,6} = r_{5,6} \bmod (x-6)$}
[{$v_6$} ]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$}
[{$v_7$} ]
]
[{$r_{8,8} = r_{7,8} \bmod (x-8)$}
[{$v_8$} ]
]
]
]
]
\end{forest}
\end{document}


Later edit: I have tried the following.

First, tiering odd and even r_{i,i}'s differently.

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$},tier=odd
[{$v_1$} ]
]
[{$r_{2,2} = r_{1,2} \bmod (x-2)$},tier=even
[{$v_2$} ]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$},tier=odd
[{$v_3$}  ]
]
[{$r_{4,4} = r_{3,4} \bmod (x-4)$},tier=even
[{$v_4$} ]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$},tier=odd
[{$v_5$}  ]
]
[{$r_{6,6} = r_{5,6} \bmod (x-6)$},tier=even
[{$v_6$} ]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$},tier=odd
[{$v_7$} ]
]
[{$r_{8,8} = r_{7,8} \bmod (x-8)$},tier=even
[{$v_8$} ]
]
]
]
]
\end{forest}
\end{document}


Second, tiering even r_{i,i}'s and v_i's differently.

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$},tier=odd
[{$v_1$},tier=last ]
]
[{$r_{2,2} = r_{1,2} \bmod (x-2)$}
[{$v_2$},tier=last ]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$},tier=odd
[{$v_3$},tier=last  ]
]
[{$r_{4,4} = r_{3,4} \bmod (x-4)$}
[{$v_4$},tier=last ]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$},tier=odd
[{$v_5$},tier=last  ]
]
[{$r_{6,6} = r_{5,6} \bmod (x-6)$}
[{$v_6$},tier=last ]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$},tier=odd
[{$v_7$},tier=last ]
]
[{$r_{8,8} = r_{7,8} \bmod (x-8)$}
[{$v_8$},tier=last ]
]
]
]
]
\end{forest}
\end{document}


Yes. That's what tiers are for. (And if you know German, you understand why I had to choose murmel. ;-)

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$}
[{$v_1$},tier=murmel ]
]
[{$r_{2,2} = r_{1,2} \bmod (x-2)$},tier=murmel
[{$v_2$} ]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$}
[{$v_3$},tier=murmel  ]
]
[{$r_{4,4} = r_{3,4} \bmod (x-4)$},tier=murmel
[{$v_4$} ]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$}
[{$v_5$},tier=murmel  ]
]
[{$r_{6,6} = r_{5,6} \bmod (x-6)$},tier=murmel
[{$v_6$} ]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$}
[{$v_7$},tier=murmel  ]
]
[{$r_{8,8} = r_{7,8} \bmod (x-8)$},tier=murmel
[{$v_8$} ]
]
]
]
]
\end{forest}
\end{document}


ADDENDUM: A version in which all the v_i are at the same level but the levels of the nodes above alternate.

\documentclass{standalone}
\usepackage{forest}
\tikzset{bullet/.style={circle,fill,inner sep=0.2pt,outer sep=0pt}}
\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$},tier=faul
[{$v_1$},tier=murmel ]
]
[,bullet,tier=faul
[{$r_{2,2} = r_{1,2} \bmod (x-2)$},tier=schnabel
[{$v_2$},tier=murmel ]
]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$},tier=faul
[{$v_3$},tier=murmel  ]
]
[,bullet,tier=faul
[{$r_{4,4} = r_{3,4} \bmod (x-4)$},tier=schnabel
[{$v_4$},tier=murmel ]
]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$},tier=faul
[{$v_5$},tier=murmel  ]
]
[,bullet,tier=faul
[{$r_{6,6} = r_{5,6} \bmod (x-6)$},tier=schnabel
[{$v_6$},tier=murmel ]
]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$},tier=faul
[{$v_7$},tier=murmel  ]
]
[,bullet,tier=faul
[{$r_{8,8} = r_{7,8} \bmod (x-8)$},tier=schnabel
[{$v_8$},tier=murmel ]
]
]
]
]
]
\end{forest}
\end{document}


• Thank you @marmot! This is very useful. Is there a way to use tier to line up the v_i leaves on the same row, while still keeping the second-to-last level "compressed" (as depicted in your picture)? – Alin Tomescu Dec 15 '18 at 23:23
• @AlinTomescu You can have several different tiers, e.g. tier=murmel for the upper row and tier=schnabel for the lower one. – marmot Dec 15 '18 at 23:25
• You mean murmel for the level with even r_{i,i}'s and schnabel for the level with odd r_{i,i}s? Or schnabel for the level with v_i's? – Alin Tomescu Dec 15 '18 at 23:29
• @AlinTomescu This is up to you. – marmot Dec 15 '18 at 23:30
• None of them seem to work though. If I set tier=even for r_{2i,2i}'s and tier=odd for r_{2i+1,2i+1}'s, they both get rendered on the same level (i.e., as in my original code). I get the same result if I set tier=even for r_{2i,2i}'s and tier=values for v_i's. – Alin Tomescu Dec 15 '18 at 23:49

I think you could play a bit around with the l specification of child leaves. For example, this code does not produce the most "aesthetic" tree but it seems to be a step forward towards what you need. Further details about leaf placement can be found in the documentation (page 10 for example)

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
[{$r_{1,8} = p \bmod a_{1,8}$}
[{$r_{1,4} = r_{1,8} \bmod a_{1,4}$}
[{$r_{1,2} = r_{1,4} \bmod a_{1,2}$}
[{$r_{1,1} = r_{1,2} \bmod (x-1)$}, l = 22mm,
[{$v_1$} ]
]
[{$r_{2,2} = r_{1,2} \bmod (x-2)$}, l = 12mm,
[{$v_2$}, l = 15mm ]
]
]
[{$r_{3,4} = r_{1,4} \bmod a_{3,4}$}
[{$r_{3,3} = r_{3,4} \bmod (x-3)$}, l = 22mm,
[{$v_3$} ]
]
[{$r_{4,4} = r_{3,4} \bmod (x-4)$}, l = 12mm,
[{$v_4$}, l = 15mm ]
]
]
]
[{$r_{5,8} = r_{1,8} \bmod a_{5,8}$}
[{$r_{5,6} = r_{5,8} \bmod a_{5,6}$}
[{$r_{5,5} = r_{5,6} \bmod (x-5)$}, l = 22mm,
[{$v_5$} ]
]
[{$r_{6,6} = r_{5,6} \bmod (x-6)$}, l = 12mm,
[{$v_6$}, l = 15mm ]
]
]
[{$r_{7,8} = r_{5,8} \bmod a_{7,8}$}
[{$r_{7,7} = r_{7,8} \bmod (x-7)$}, l = 22mm,
[{$v_7$} ]
]
[{$r_{8,8} = r_{7,8} \bmod (x-8)$}, l = 12mm,
[{$v_8$}, l = 15mm ]
]
]
]
]
\end{forest}
\end{document}