TiKz, forest, probabilistic tree ---> new macro or new environment

by projetmbc   Last Updated October 09, 2019 19:23 PM - source

The following code does a good job but I would like to make it reusable by defining a new environment. Is there a way to do that ?

\documentclass[12pt,a4paper]{article}

\usepackage{forest}
\useforestlibrary{linguistics}
\forestset{%
  declare count={connections}{1},
}


\begin{document}

\begin{forest}
    for tree={%
        sn edges,
        grow'  = 0,
        l      = 2.5cm,
        s sep  = 1.2cm,
        anchor = parent,
    },
    before typesetting nodes={%
        where connections=1{}{%
            if = {isodd(connections())}{%
                edge path'={%
                    foreach \i [
                        count=\j from 0, 
                        evaluate=\noexpand\j 
                        as \noexpand\k
                        using \noexpand { 
                            (\j==0) ?
                            0pt :
                            ((isodd(\j)) ?
                            (\j*4pt) :
                            ((-\j+1)*4pt))
                        }
                    ]
                    in {%
                        1, ..., \foresteoption{connections}
                    }{
                        (!u.parent anchor)
                        -- 
                        ([yshift=\noexpand\k].child anchor)    
                    }
                },
            }{%
                edge path'={%
                    foreach \i [
                        count=\j, 
                        evaluate=\noexpand\j
                        as \noexpand\k
                        using \noexpand {
                            (isodd(\j)) ? 
                            (\j*4pt) : 
                            ((-\j+1)*4pt)
                        }
                    ]
                    in {1,...,\foresteoption{connections}} { (!u.parent anchor) -- ([yshift=\noexpand\k].child anchor)    }
                },
            },
        },
    },
    [
        [$A$, edge label={node[midway,fill=white]{$a$}}
            [$B$, edge label={node[midway,fill=white]{$b$}}]
            [$C$, edge label={node[midway,fill=white]{$c$}}]
        ]
        [$D$, edge label={node[midway,fill=white]{$d$}}
            [$E$, edge label={node[midway,fill=white]{$e$}}]
            [$F$, edge label={node[midway,fill=white]{$f$}}]
        ]
    ]
\end{forest}

\end{document}


Related Questions


TikZ, forest and star macro

Updated October 10, 2019 18:23 PM

Using tikz q-tree with an embedded macro

Updated September 21, 2019 22:23 PM

How to make a bracketed environment like this?

Updated September 24, 2018 21:23 PM

High-Quality and Custom Essay writing service

Updated July 31, 2017 05:23 AM