2

I'm trying to draw some trees using the forest package. For some reason, the leaf sizes are scaled differently depending on how many leaves the tree has (see picture).

I want every leaf to be of same size for all the three trees in the picture. How can I do that?enter image description here

My code is as follows:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{forest}

\begin{document}
\begin{figure}[t]
\centering
\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [$S^e_0$,edge label={node[midway,left] {0}} ] 
      [$S^e_1$,edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^e_2$,edge label={node[midway,left] {0}} ] 
      [$S^e_3$,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}

\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [, edge label={node[midway,left] {0}}
        [$S^{e+1}_0$, edge label={node[midway,left] {0}}]
        [$S^{e+1}_1$, edge label={node[midway,right] {1}}]
        ]
      [$S^{e+1}_2$, edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^{e+1}_3$,edge label={node[midway,left] {0}} ] 
      [$S^{e+1}_4$,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [$S^{e+2}_0$,edge label={node[midway,left] {0}} ] 
      [$S^{e+2}_1$,edge label={node[midway,right] {1}} ] 
    ]
    [$S^{e+2}_2$, edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...} \label{fig:...}
\end{figure}
\end{document}
8
  • Could it be possible that the circles in the first example are smaller because their content takes up less space?
    – leandriis
    Jan 12 '20 at 18:34
  • Related: tex.stackexchange.com/q/366716/134144
    – leandriis
    Jan 12 '20 at 18:38
  • @leandriis I tried the proposed solution in the link. Didn't work though.
    – SpiderRico
    Jan 12 '20 at 18:40
  • Do you also want to enlarge the empty circles?
    – leandriis
    Jan 12 '20 at 18:42
  • What didn't work?
    – Zarko
    Jan 12 '20 at 18:42
3

With an appropriate value of minimum size:

enter image description here

\documentclass{article}
\usepackage{forest}

\begin{document}


\begin{figure}[t]
\centering
\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [$S^e_0$,minimum size=3em,edge label={node[midway,left] {0}} ] 
      [$S^e_1$,minimum size=3em,edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^e_2$,minimum size=3em,edge label={node[midway,left] {0}} ] 
      [$S^e_3$,minimum size=3em,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}

\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [, edge label={node[midway,left] {0}}
        [$S^{e+1}_0$,minimum size=3em, edge label={node[midway,left] {0}}]
        [$S^{e+1}_1$, minimum size=3em,edge label={node[midway,right] {1}}]
        ]
      [$S^{e+1}_2$, minimum size=3em,edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^{e+1}_3$,minimum size=3em,edge label={node[midway,left] {0}} ] 
      [$S^{e+1}_4$,minimum size=3em,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
    [, edge label={node[midway,left] {0}} 
      [$S^{e+2}_0$,minimum size=3em,edge label={node[midway,left] {0}} ] 
      [$S^{e+2}_1$,minimum size=3em,edge label={node[midway,right] {1}} ] 
    ]
    [$S^{e+2}_2$, minimum size=3em,edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...} 
\label{fig:...}
\end{figure}


\end{document}
3

This also uses the minimum width key like leandriis' answer but appends it to all nodes that have no children. Further simplification can be achieved by defining a style that gets used in all trees.

\documentclass{article}
\usepackage{forest}
\forestset{my tree/.style={for tree={circle,
    draw,
    where n children=0{minimum width=3em}{}}}}
\begin{document}
\begin{figure}[t]
\centering
\begin{forest}
my tree
[
    [, edge label={node[midway,left] {0}} 
      [$S^e_0$,edge label={node[midway,left] {0}} ] 
      [$S^e_1$,edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^e_2$,edge label={node[midway,left] {0}} ] 
      [$S^e_3$,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}

\begin{forest}
my tree
[
    [, edge label={node[midway,left] {0}} 
      [, edge label={node[midway,left] {0}}
        [$S^{e+1}_0$, edge label={node[midway,left] {0}}]
        [$S^{e+1}_1$, edge label={node[midway,right] {1}}]
        ]
      [$S^{e+1}_2$, edge label={node[midway,right] {1}} ] 
    ]
    [, edge label={node[midway,right] {1}}
      [$S^{e+1}_3$,edge label={node[midway,left] {0}} ] 
      [$S^{e+1}_4$,edge label={node[midway,right] {1}} ] 
  ] 
]
\end{forest}
~
\begin{forest}
my tree
[
    [, edge label={node[midway,left] {0}} 
      [$S^{e+2}_0$,edge label={node[midway,left] {0}} ] 
      [$S^{e+2}_1$,edge label={node[midway,right] {1}} ] 
    ]
    [$S^{e+2}_2$, edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...} 
\label{fig:...}
\end{figure}
\end{document}

enter image description here

You could remove the need for adding the $ signs by using math content but in this very example I personally would find this confusing.

1
  • Great simplification that will definitely save a lot of typing.
    – leandriis
    Jan 12 '20 at 19:04
2

Based on my answer, with some simplification of writing edge labels and slightly redesign of of trees:

\documentclass{article}
\usepackage{forest}

\begin{document}
\begin{figure}[t]
\tikzset{Circle/.style = {circle, draw, minimum size=8pt, inner sep=0pt}}
\forestset{
  EL/.style 2 args={% shortens for (my) edge label
    edge label={node[midway, font=\footnotesize, #1]{#2}},
                    },
        }% end of forestset
\centering
\begin{forest}
  for tree={circle, draw,
            minimum size=2em, % <-- added
            inner sep=1pt}      % <-- added
[,Circle
    [, Circle, EL={left}{0}
      [$S^e_0$, EL={left}{0} ]
      [$S^e_1$, EL={right}{1}]
    ]
    [, Circle, EL={right}{1}
      [$S^e_2$,EL={left}{0}]
      [$S^e_3$,EL={right}{1}]
  ]
]
\end{forest}

\bigskip
\begin{forest}
  for tree={circle, draw,
            minimum size=2em, % <-- added
            inner sep=1pt}      % <-- added
[,Circle
    [, Circle, EL={left}{0}
      [, Circle, EL={left}{0}, fit=band
        [$S^{e+1}_0$, EL={left}{0}]
        [$S^{e+1}_1$,  EL={right}{1}]
        ]
      [$S^{e+1}_2$, EL={right}{1} ]
    ]
    [, Circle, EL={right}{1}
      [$S^{e+1}_3$, EL={left}{0}]
      [$S^{e+1}_4$, EL={right}{1} ]
  ]
]
\end{forest}
\quad
\begin{forest}
  for tree={circle, draw,
            minimum size=2em, % <-- added
            inner sep=1pt}      % <-- added
[,Circle
    [, Circle,  EL={left}{0}, fit=band
      [$S^{e+2}_0$, EL={left}{0} ]
      [$S^{e+2}_1$, EL={right}{1} ]
    ]
    [$S^{e+2}_2$,  EL={right}{1} ]
]
\end{forest}
\caption{...}
\label{fig:...}
\end{figure}
\end{document}

enter image description here

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.