使用样式应用的自定义项

使用样式应用的自定义项

我有一个 LaTeX 文档,其中包含使用该forest软件包制作的几棵树,我想以一种避免代码重复的方式为这些树定义装饰样式。换句话说,我想将树装饰设置(即:)放在for tree= {...}一个独特的位置——这样可读性更强,修改起来也更容易。我的代码如下所示:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,angles,quotes}
\colorlet{linecol}{black!75}
\tikzset{
  my rounded corners/.append style={rounded corners=2pt},
}
\usepackage{forest}

\begin{document}

% ================= FIRST TREE
\begin{forest}
  for tree={
    line width=1pt,
    if={level()<2}{
      my rounded corners,
      draw=linecol,
    }{},
    edge={color=linecol, >={Triangle[]}, ->},
    if level=0{%
      l sep+=1.5cm,
      align=center,
      parent anchor=south,
      tikz={
        \path (!1.child anchor) coordinate (A) -- () coordinate (B) -- (!l.child anchor) coordinate (C) pic [draw, angle radius=20mm, every node/.append style={fill=white}, "based on"] {angle};
      },
    }{%
      if level=1{%
        parent anchor=south west,
        child anchor=north,
        tier=parting ways,
        align=center,
        font=\bfseries,
        for descendants={
          child anchor=west,
          parent anchor=west,
          anchor=west,
          align=left,
        },
      }{
        if level=2{
          shape=coordinate,
          no edge,
          grow'=0,
          calign with current edge,
          xshift=20pt,
          for descendants={
            parent anchor=south west,
            l sep+=-20pt
          },
          for children={
            edge path={
              \noexpand\path[\forestoption{edge}] (!to tier=parting ways.parent anchor) |- (.child anchor)\forestoption{edge label};
            },
            font=\bfseries,
            for descendants={
              no edge,
            },
          },
        }{},
      },
    }%
  },
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}

%================= SECOND TREE
\begin{forest}
  for tree={
    line width=1pt,
    if={level()<2}{
      my rounded corners,
      draw=linecol,
    }{},
    edge={color=linecol, >={Triangle[]}, ->},
    if level=0{%
      l sep+=1.5cm,
      align=center,
      parent anchor=south,
      tikz={
        \path (!1.child anchor) coordinate (A) -- () coordinate (B) -- (!l.child anchor) coordinate (C) pic [draw, angle radius=20mm, every node/.append style={fill=white}, "based on"] {angle};
      },
    }{%
      if level=1{%
        parent anchor=south west,
        child anchor=north,
        tier=parting ways,
        align=center,
        font=\bfseries,
        for descendants={
          child anchor=west,
          parent anchor=west,
          anchor=west,
          align=left,
        },
      }{
        if level=2{
          shape=coordinate,
          no edge,
          grow'=0,
          calign with current edge,
          xshift=20pt,
          for descendants={
            parent anchor=south west,
            l sep+=-20pt
          },
          for children={
            edge path={
              \noexpand\path[\forestoption{edge}] (!to tier=parting ways.parent anchor) |- (.child anchor)\forestoption{edge label};
            },
            font=\bfseries,
            for descendants={
              no edge,
            },
          },
        }{},
      },
    }%
  },
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

有没有办法实现这样的事情? 谢谢。

答案1

我知道两种方法可以做到这一点。第一种方法是将自定义项放在样式中,因此它们将应用于forest您为其指定此样式的每个环境;第二种方法将给定的自定义项应用于给forest定 TeX 组内的所有环境 - 可能是整个文档。

使用样式应用的自定义项

您可以定义一种样式(该样式在下面的示例中\forestset命名),并在您希望它活跃的特定环境中指定它的名称:my forestforest

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,angles,quotes}
\colorlet{linecol}{black!75}
\tikzset{
  my rounded corners/.append style={rounded corners=2pt},
}
\usepackage{forest}

\forestset{
  my forest/.style={
    for tree={
    line width=1pt,
    if={level()<2}{
      my rounded corners,
      draw=linecol,
    }{},
    edge={color=linecol, >={Triangle[]}, ->},
    if level=0{%
      l sep+=1.5cm,
      align=center,
      parent anchor=south,
      tikz={
        \path (!1.child anchor) coordinate (A) -- () coordinate (B) -- (!l.child anchor) coordinate (C) pic [draw, angle radius=20mm, every node/.append style={fill=white}, "based on"] {angle};
      },
    }{%
      if level=1{%
        parent anchor=south west,
        child anchor=north,
        tier=parting ways,
        align=center,
        font=\bfseries,
        for descendants={
          child anchor=west,
          parent anchor=west,
          anchor=west,
          align=left,
        },
      }{
        if level=2{
          shape=coordinate,
          no edge,
          grow'=0,
          calign with current edge,
          xshift=20pt,
          for descendants={
            parent anchor=south west,
            l sep+=-20pt
          },
          for children={
            edge path={
              \noexpand\path[\forestoption{edge}] (!to tier=parting ways.parent anchor) |- (.child anchor)\forestoption{edge label};
            },
            font=\bfseries,
            for descendants={
              no edge,
            },
          },
        }{},
      },
    }%
  }}}

\begin{document}

\section{First tree}

\begin{forest}
  my forest
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}

\section{Second tree}

\begin{forest}
  my forest
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

截屏

自定义应用于给forest定 TeX 组内的所有环境

使用

\forestset{
  default preamble={...}
}

您可以将...设置应用于当前 TeX 组中的所有forest环境。如果在序言中使用,这将应用于forest文档中的所有环境。

序言中的设置示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,angles,quotes}
\colorlet{linecol}{black!75}
\tikzset{
  my rounded corners/.append style={rounded corners=2pt},
}
\usepackage{forest}

\forestset{
  default preamble={
    for tree={
    line width=1pt,
    if={level()<2}{
      my rounded corners,
      draw=linecol,
    }{},
    edge={color=linecol, >={Triangle[]}, ->},
    if level=0{%
      l sep+=1.5cm,
      align=center,
      parent anchor=south,
      tikz={
        \path (!1.child anchor) coordinate (A) -- () coordinate (B) -- (!l.child anchor) coordinate (C) pic [draw, angle radius=20mm, every node/.append style={fill=white}, "based on"] {angle};
      },
    }{%
      if level=1{%
        parent anchor=south west,
        child anchor=north,
        tier=parting ways,
        align=center,
        font=\bfseries,
        for descendants={
          child anchor=west,
          parent anchor=west,
          anchor=west,
          align=left,
        },
      }{
        if level=2{
          shape=coordinate,
          no edge,
          grow'=0,
          calign with current edge,
          xshift=20pt,
          for descendants={
            parent anchor=south west,
            l sep+=-20pt
          },
          for children={
            edge path={
              \noexpand\path[\forestoption{edge}] (!to tier=parting ways.parent anchor) |- (.child anchor)\forestoption{edge label};
            },
            font=\bfseries,
            for descendants={
              no edge,
            },
          },
        }{},
      },
    }%
  }}}

\begin{document}

\section{First tree}

\begin{forest}
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}

\section{Second tree}

\begin{forest}
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

输出与前面的示例相同。

例如,我们在一个组内进行设置,从该组输出一棵树(因此使用自定义设置),然后离开该组并输出第二棵树,因此使用默认设置:

\documentclass{article}
\usepackage[landscape]{geometry} % the tree in section 2 is rather wide
\usepackage{tikz}
\usetikzlibrary{arrows.meta,angles,quotes}
\colorlet{linecol}{black!75}
\tikzset{
  my rounded corners/.append style={rounded corners=2pt},
}
\usepackage{forest}

\begin{document}

\section{First tree}

{% \forestset settings local to the current TeX group
\forestset{
  default preamble={
    for tree={
    line width=1pt,
    if={level()<2}{
      my rounded corners,
      draw=linecol,
    }{},
    edge={color=linecol, >={Triangle[]}, ->},
    if level=0{%
      l sep+=1.5cm,
      align=center,
      parent anchor=south,
      tikz={
        \path (!1.child anchor) coordinate (A) -- () coordinate (B) -- (!l.child anchor) coordinate (C) pic [draw, angle radius=20mm, every node/.append style={fill=white}, "based on"] {angle};
      },
    }{%
      if level=1{%
        parent anchor=south west,
        child anchor=north,
        tier=parting ways,
        align=center,
        font=\bfseries,
        for descendants={
          child anchor=west,
          parent anchor=west,
          anchor=west,
          align=left,
        },
      }{
        if level=2{
          shape=coordinate,
          no edge,
          grow'=0,
          calign with current edge,
          xshift=20pt,
          for descendants={
            parent anchor=south west,
            l sep+=-20pt
          },
          for children={
            edge path={
              \noexpand\path[\forestoption{edge}] (!to tier=parting ways.parent anchor) |- (.child anchor)\forestoption{edge label};
            },
            font=\bfseries,
            for descendants={
              no edge,
            },
          },
        }{},
      },
    }%
  }}}
%
% Tree in the same TeX group: it uses the settings we just set
\begin{forest}
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}

}% Leave the group where we used custom \forestset{default preamble={...}}
 % settings.

\section{Second tree}

% This tree uses the default settings.
\begin{forest}
  [Concept
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
    [sub-concept
      [
        [method
        [submethod]
        ]
        [method
        [submethod\\details]
        ]
        [method
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

截屏

相关内容