
次のようなツリーがあります。ルーフの幅をテキストの幅より広くするのではなく、同じにしたいです。 でこれを行う方法はありますかforest
? 特に、上部のテキストの幅にする必要があります。これは、翻訳が元のテキストよりはるかに広い場合に重要です。ルーフは元のテキストに合う必要があります。これは分析されるものであり、翻訳は余分なものであり、論理的には構造の一部ではありません。
\documentclass{minimal}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=bottom,where n children=0{tier=word}{}}},
background tree/.style={for tree={text opacity=0.2,draw opacity=0.2,edge={draw opacity=0.2}}}
}
\begin{document}
\begin{forest}
sn edges
[NP
[Det [das\\the] ]
[N'
[N'
[N [Bild\\picture] ]
[PP [vom Gleimtunnel\\of.the Gleimtunnel,triangle ] ] ]
[PP [im Gropiusbau\\in.the Gropiusbau,triangle ] ] ] ]
\end{forest}
\end{document}
編集:以下に、Sašo によるほぼ完璧な解決策があります。ただし、問題が 1 つ残っています。ツリーで使用したいコマンドを定義しました\trace
。これを翻訳コードで使用すると、次のエラーが発生します。
! Argument of \@rsbox has an extra }.
<inserted text>
\par
l.45 \end{forest}
?
コードは次のとおりです:
\documentclass{minimal}
\usepackage{forest}
\forestset{
with translation/.style={
l sep=0,inner xsep=0,
append translation/.expanded/.wrap pgfmath arg={\gettranslation{##1}}{content},
content/.expanded/.wrap pgfmath arg={\gettext{##1}}{content},
},
append translation/.style={append={[#1,no edge,l=0,inner xsep=0,inner ysep=0,outer ysep=0,before
computing xy={l-=2pt}]}},
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top},
where n children=0{tier=word,delay=with translation}{} %,delay=with translation
}
}
\def\gettext#1{\gettextA#1;;\endget}
\def\gettextA#1;#2;{\removesep#1;}
\def\gettranslation#1{\gettranslationA#1;;\endget}
\def\gettranslationA#1;#2;{\removesep#2;}
\def\removesep#1;#2\endget{#1}
\newcommand{\trace}{\raisebox{0.2ex}{\_}\rule{0cm}{0.7em}}
\begin{document}
\begin{forest}
sn edges
[NP
[Det [eine;a] ]
[N$'$
[A [kluge;smart] ]
[N$'$
[N [\trace ] ] ] ] ]
\end{forest}
\end{document}
\end{forest}
翻訳されたツリーでそのようなコマンドを許可する方法はありますか?
答え1
以下では、フォレスト マニュアル (セクション 4.2) の大きな例に示されているトリックを使用して、ノードのコンテンツを解析します。次に、(i) テキストのすぐ下に追加のノードが作成され、翻訳 (つまり、\\
元のコンテンツでその後に続くもの) が入力され、(ii) コンテンツから翻訳が削除されます。(この解析メカニズムは、ツリーのすべてのリーフに適用するように設定されています。)
また、テキスト ノードと翻訳ノードにはtikz
がinner xsep
設定されており0
、三角形がテキストにぴったりとフィットします。
編集: 翻訳のないノードを受け入れるようにコードが変更されました。ただし、これを実行できたのは、より単純なセパレーターを使用した場合のみでした。以下が;
使用されています。
2 番目の編集:.expanded
スタイルと内のハンドラーはappend translation
、によってもたらされる追加の問題に対処するために、content
およびのチェーンに置き換えられました(OP の編集された質問を参照)。.expand once
.expand twice
\trace
\documentclass{minimal}
\usepackage{forest}
\forestset{
with translation/.style={
l sep=0,inner xsep=0,
append translation/.expand once/.expand twice/.wrap pgfmath arg={\gettranslation{##1}}{content},
content/.expand once/.expand twice/.wrap pgfmath arg={\gettext{##1}}{content},
},
append translation/.style={append={[#1,no edge,l=0,inner xsep=0,inner ysep=0,outer ysep=0,before
computing xy={l-=2pt}]}},
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top},
if n children=0{tier=word,delay=with translation}{} %,delay=with translation
}
}
\def\gettext#1{\gettextA#1;;\endget}
\def\gettextA#1;#2;{\removesep#1;}
\def\gettranslation#1{\gettranslationA#1;;\endget}
\def\gettranslationA#1;#2;{\removesep#2;}
\def\removesep#1;#2\endget{#1}
\newcommand{\trace}{\raisebox{0.2ex}{\_}\rule{0cm}{0.7em}}
\begin{document}
\begin{forest}
sn edges,
[NP
[Det [das] ]
[N'
[N'
[N [Bild;picture] ]
[PP [vom Gleimtunnel;of.the Gleimtunnel,triangle]] ] ]
[PP [im Gropiusbau;in.the Gropiusbau,triangle ] ] ] ]
\end{forest}
\begin{forest}
sn edges
[NP
[Det [eine;a] ]
[N$'$
[A [kluge;smart] ]
[N$'$
[N [\trace ] ] ] ] ]
\end{forest}
\end{document}
3 回目の編集:where n children=0
の定義内を、sn edges
論理的により正しい に変更しましたif n children=0
。現在の (v1.0.10) バージョンのパッケージでは、これによって (実行速度が遅くなる以外は) 違いはありませんが、where n children
(近日リリース予定の) v1.1 バージョンのパッケージでは、バージョンによって間違った結果が生成されます。これは、v1.1 で のバグが修正されるためですdelay
。v1.0.x では、delay
が単一ノードで複数回使用されると、最初の呼び出しが忘れられていました。したがって、上記のコードの間違ったバージョンは、 が複数回呼び出されるという事実にもかかわらず、v1.0.x で動作します。v1.1delay=with translation
では、 をdelay=with translation
3 回呼び出すと実際には (後で) 3 回呼び出されるwith translation
ため、余分な空のノードが生成されます。
答え2
1 つのオプション:
\documentclass{article}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=bottom,where n children=0{tier=word,inner xsep=0pt,outer sep=0pt}{}}},
background tree/.style={for tree={text opacity=0.2,draw opacity=0.2,edge={draw opacity=0.2}}}
}
\newcommand\HideWd[1]{%
\makebox[0pt]{#1}%
}
\begin{document}
The default behaviour:\par
\begin{forest}
sn edges,
[NP
[Det [das\\the] ]
[N',s sep=20pt
[N',s sep=15pt
[N [Bild\\picture] ]
[PP [vom Gleimtunnel\\ of.the Gleimtunnel,triangle ] ] ]
[PP [im Gropiusbau\\ in.the Gropiusbau,triangle ] ] ] ]
\end{forest}
Hiding the wider text:\par
\begin{forest}
sn edges,
[NP
[Det [das\\the] ]
[N'
[N'
[N [Bild\\picture] ]
[PP [vom Gleimtunnel\\ \HideWd{of.the Gleimtunnel},triangle ] ] ]
[PP [im Gropiusbau\\ \HideWd{in.the Gropiusbau},triangle ] ] ] ]
\end{forest}
Hiding the wider text and correcting the separation:\par
\begin{forest}
sn edges,
[NP
[Det [das\\the] ]
[N',s sep=20pt
[N',s sep=15pt
[N [Bild\\picture] ]
[PP [vom Gleimtunnel\\ \HideWd{of.the Gleimtunnel},triangle ] ] ]
[PP [im Gropiusbau\\ \HideWd{in.the Gropiusbau},triangle ] ] ] ]
\end{forest}
\end{document}
アイデアは、幅が 0pt のボックスを使用して、幅の広いテキストを「非表示」にすることです (これがコマンドの機能です\HideWd
)。ただし、これにより、影響を受ける兄弟間の距離の計算に望ましくない影響が生じるため (画像の 2 番目のツリーを参照)、適切な値を提供するには手動による介入が必要になりますs sep
(画像の 3 番目の 3 つを参照)。
答え3
これは、以下のコードを組み合わせる問題に関する編集された質問に対する回答です。サショ・ジヴァノヴィッチの回答カスタムマクロを使用します。
\trace
コツは、ツリー内のの使用を新しいスタイルに置き換えることです。trace
これにより、マクロが実行され、with translation
子が 0 個のノードへの の適用がオーバーライドされます。
\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top}, for tree={where n children=0{tier=word, delay=with translation}{}}},
trace/.style={
replace by={[\trace, delay={}, tier=word]}
},
with translation/.style={
l sep=0,
inner xsep=0,
append translation/.expanded/.wrap pgfmath arg={\gettranslation{##1}}{content},
content/.expanded/.wrap pgfmath arg={\gettext{##1}}{content},
},
append translation/.style={
append={
[
#1,
no edge,
l=0,
inner xsep=0,
inner ysep=0,
outer ysep=0,
before computing xy={
l-=2pt
}
]
},
},
}
\def\gettext#1{\gettextA#1;;\endget}
\def\gettextA#1;#2;{\removesep#1;}
\def\gettranslation#1{\gettranslationA#1;;\endget}
\def\gettranslationA#1;#2;{\removesep#2;}
\def\removesep#1;#2\endget{#1}
\newcommand{\trace}{\raisebox{0.2ex}{\_}\rule{0cm}{0.7em}}
\begin{document}
\begin{forest}
sn edges
[NP
[Det [eine;a] ]
[N$'$
[A [kluge;smart] ]
[N$'$
[N [, trace ] ] ] ] ]
\end{forest}
\end{document}