포레스트 패키지: 서문에서 \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당신은 서문에서 스타일을 정의 하지만, 당신의 트리가 그것을 사용하기를 원한다고 말하지는 않습니다. 이렇게 하려면 스타일 이름을 트리의 키로 제공해야 합니다.

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}

결과:

여기에 이미지 설명을 입력하세요

대안은 새로운 스타일을 생성하는 것이 아니라 의 키를 사용하여 모든 트리에 지정된 스타일을 자동으로 적용하는 것 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}

여기에 이미지 설명을 입력하세요

관련 정보