Pacote Forest: Como usar \forestset no preâmbulo?

Pacote Forest: Como usar \forestset no preâmbulo?

Estou tentando automatizar o layout de certas árvores que desejo usar repetidamente, mas não consigo fazer o \forestsetcomando funcionar no preâmbulo. Nos códigos abaixo, suponho que a parte comentada do arquivo no documento principal seja desnecessária, uma vez que essa informação tenha sido repassada ao preâmbulo. Mas da forma como está atualmente, as informações do preâmbulo não alteram o layout padrão da árvore resultante. (Observe que isso também não produz um erro de compilação.)

\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={
      \noexpand\path [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={
  %     \noexpand\path [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}

Responder1

Você define o estilo sn edgesno preâmbulo, mas não diz que deseja que sua árvore o utilize. Para fazer isso você deve fornecer o nome do estilo como uma chave na sua árvore.

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={
      \noexpand\path [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}

Resultado:

insira a descrição da imagem aqui

Uma alternativa não é criar um novo estilo, mas aplicar o estilo fornecido automaticamente a todas as árvores usando a default preambletecla \forestset. Neste caso você não precisa fornecer uma chave de estilo para árvores individuais.

\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={
      \noexpand\path [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}

Responder2

Você \forestsetprecisa definir o estilo cujo nome é considerado no forestambiente. Veja MWE abaixo:

\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={
          \noexpand\path [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}

insira a descrição da imagem aqui

informação relacionada