森林包:如何在序言中使用 \forestset?

森林包:如何在序言中使用 \forestset?

我正在尝试自动执行某些我希望重复使用的树的布局,但我似乎无法让命令\forestset在序言中工作。在下面的代码中,我认为一旦将这些信息传递到序言中,主文档中文件的注释掉部分就没有必要了。但就目前情况而言,序言信息无法修改结果树的默认布局。(请注意,这也不会产生编译错误。)

\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}

答案1

您在序言中定义了样式sn edges,但并未说明您希望树使用它。为此,您应该在树中提供样式名称作为键。

梅威瑟:

\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}

结果:

在此处输入图片描述

default preamble另一种方法是不创建新样式,而是使用中的键将给定的样式自动应用于所有树\forestset。在这种情况下,您不需要为单个树提供样式键。

\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}

答案2

\forestset需要定义在环境中考虑哪个名称的样式forest。请参阅下面的 MWE:

\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}

在此处输入图片描述

相关内容