較小的森林屋頂

較小的森林屋頂

我有下面的樹。我希望屋頂與文字一樣寬而不是更寬。有沒有辦法做到這一點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 提供了一個近乎完美的解決方案。然而,仍然存在一個問題。我定義了一個\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) 從內容中刪除翻譯。 (此解析機制設定為適用於樹的所有葉子。)

另外,文字和翻譯節點的tikzsinner xsep設定0為 ,以便三角形緊密貼合文字。

編輯:程式碼更改為接受沒有翻譯的節點。然而,我只能使用更簡單的分隔符號來做到這一點;下面,;使用。

第二次編輯:.expanded樣式中的處理程序append translationcontent被替換為 和 鏈.expand once.expand twice以處理 帶來的額外問題\trace(請參閱OP的編輯問題)。

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

第三次編輯: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 中運行,儘管事實上程式碼delay=with translation被多次呼叫。在 v1.1 中,呼叫delay=with translation3 次實際上呼叫with translation(稍後)3 次,這會導致多餘的空節點。

答案2

一種選擇:

\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命令的作用);然而,這會對計算受影響的兄弟姐妹之間的距離產生不良影響(請參閱圖像中的第二棵樹),因此需要一些手動幹預來提供一些適當的值s sep(請參閱圖像中的第三個樹) 。

答案3

這是對結合中給出的程式碼的問題的編輯問題的回答薩索·齊萬諾維奇的回答使用自訂巨集。

\trace訣竅是用新樣式取代樹中的使用,該樣式執行巨集並覆蓋對具有 0 個子節點的節點trace的應用。with translation

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

追蹤與翻譯

相關內容