TransWikia.com

Forest package: How to use forestset in the preamble?

TeX - LaTeX Asked by Nicomachus on January 26, 2021

I’m trying to automate the layout for certain trees that I wish to use repetitively, but I don’t seem to be able to get the forestset command to work in the preamble. In the codes below, I suppose that the commented-out portion of the file in the main document, should be unnecessary, once this information has been passed on to the preamble. But as it currently stands, the preamble information fails to amend the default layout of the resulting tree. (Note that this doesn’t produce a compilation error, either.)

documentclass[10pt,oneside,pdftex,dvipsnames,a4paper]{article}
usepackage{forest}
forestset{ 
  sn edges/.style={
  for tree={
    grow'=0, 
    l=0pt, inner sep=0.075cm, s sep=0.1mm,
    child anchor=west,parent anchor=south,
    anchor=west,calign=first,
    edge path={
      noexpandpath [draw, forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node{} (.child anchor)forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }
 }
}
%-------------------------------------------------------------
begin{document}
begin{forest}
  % for tree={
  %   grow'=0, 
  %   l=0pt, inner sep=0.075cm, s sep=0.1mm,
  %   child anchor=west,parent anchor=south,
  %   anchor=west,calign=first,
  %   edge path={
  %     noexpandpath [draw, forestoption{edge}]
  %     (!u.south west) +(7.5pt,0) |- node{} (.child anchor)forestoption{edge label};
  %   },
  %   before typesetting nodes={
  %     if n=1
  %       {insert before={[,phantom]}}
  %       {}
  %   },
  %   fit=band,
  %   before computing xy={l=15pt},
  % }
[$B$
  [$B_{1}$]
  [$B_{2}$
    [$A$
      [$A_{1}$,tikz={node [draw,inner sep=0.5pt,fit to=tree]{};}]
      [$A_{2}$
        [$D$]
        [$E$]
      ]
      [$A_{3}$
      ]
    ]
  ]
]
end{forest}
end{document}

2 Answers

In forestset you need to define style which name is considered in forest environment. See MWE below:

documentclass[a4paper]{article}
usepackage{forest}
forestset{my tree/.style = {% NEW
    for tree={
        inner sep=0.075cm, 
        l sep=1mm,% CHANGED AND CORRECTED
        s sep=1mm,% changed
        grow'=0,
        child anchor=west,parent anchor=south,
        anchor=west,calign=first,
        edge path={
          noexpandpath [draw, forestoption{edge}]
          (!u.south west) +(7.5pt,0) |- node{} (.child anchor)forestoption{edge label};
        },
        before typesetting nodes={
          if n=1
            {insert before={[,phantom]}}
            {}
        },
        fit=band,
        before computing xy={l=15pt},
            }% end of for tree
                        }% end of my tree style
        }% end of forestset
%-------------------------------------------------------------
begin{document}
begin{forest} my tree
 [$B$
  [$B_{1}$]
  [$B_{2}$
    [$A$
      [$A_{1}$,tikz={node [draw,inner sep=0.5pt,fit to=tree]{};}]
      [$A_{2}$
        [$D$]
        [$E$]
      ]
      [$A_{3}$
      ]
    ]
  ]
]
end{forest}
end{document}

enter image description here

Answered by Zarko on January 26, 2021

You define the style sn edges in the preamble, but you don't say that you want your tree to use it. To do this you should provide the style name as a key in your tree.

MWE:

documentclass{article}
usepackage{forest}
forestset{ 
  sn edges/.style={
  for tree={
    grow'=0, 
    l=0pt, inner sep=0.075cm, s sep=0.1mm,
    child anchor=west,parent anchor=south,
    anchor=west,calign=first,
    edge path={
      noexpandpath [draw, forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node{} (.child anchor)forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }
 }
}
%-------------------------------------------------------------
begin{document}
begin{forest}
sn edges,
[$B$
  [$B_{1}$]
  [$B_{2}$
    [$A$
      [$A_{1}$,tikz={node [draw,inner sep=0.5pt,fit to=tree]{};}]
      [$A_{2}$
        [$D$]
        [$E$]
      ]
      [$A_{3}$
      ]
    ]
  ]
]
end{forest}
end{document}

Result:

enter image description here

An alternative is not to create a new style but to apply the given style automatically to all trees using the key default preamble in forestset. In this case you don't need to provide a style key to individual trees.

documentclass{article}
usepackage{forest}
forestset{ 
  default preamble={
  for tree={
    grow'=0, 
    l=0pt, inner sep=0.075cm, s sep=0.1mm,
    child anchor=west,parent anchor=south,
    anchor=west,calign=first,
    edge path={
      noexpandpath [draw, forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node{} (.child anchor)forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }
 }
}
%-------------------------------------------------------------
begin{document}
begin{forest}
[$B$
  [$B_{1}$]
  [$B_{2}$
    [$A$
      [$A_{1}$,tikz={node [draw,inner sep=0.5pt,fit to=tree]{};}]
      [$A_{2}$
        [$D$]
        [$E$]
      ]
      [$A_{3}$
      ]
    ]
  ]
]
end{forest}
end{document}

Answered by Marijn on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP