
繰り返し使用したい特定のツリーのレイアウトを自動化しようとしていますが、コマンドを\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}