定義大括號內的節點

定義大括號內的節點

我試圖在大括號 {} 內定義一個節點:

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
\Tree [.CP \node(y){here}; [.C$'$ C [.TP \edge[roof]; {Some text \node(x){here};} ]]]
\end{tikzpicture}

\end{document}

我需要在 {} 環境中定義節點 x,因為我希望 tikz-qtree 將字串「Some text here」視為單一單元。但是,如果我將分號放在第一個或第二個右括號後面,則它不起作用。

答案1

由於屋頂節點內的文字本身就是一個內部節點,而且您無法在節點內嵌入節點,因此您需要以稍微不同的方式解決此問題。基本思想是將屋頂節點的父節點改為命名節點。然後,如果您想要顯示例如來自每個節點的箭頭,您必須相對於父節點偏移箭頭的起點。這是一個例子:

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
\Tree [.CP \node(y){here}; [.C$'$ C [.\node(x){TP}; \edge[roof];  {Some text here} ]]]
\draw [->] (x.south)++(.9,-1) to[out=-90,in=-90,looseness=2] (y.south); 
\end{tikzpicture}

\end{document}

另一種方法(可能更簡單)是將屋頂文字本身定義為節點,然後使用參數[xshift]將箭頭的起點移動到所需的位置:

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
\Tree [.CP \node(y){here}; [.C$'$ C [.TP \edge[roof];  \node(x){Some text here}; ]]]
\draw [->] ([xshift=2.5em]x.south) to[out=-90,in=-90,looseness=2] (y.south); 
\end{tikzpicture}

\end{document}

程式碼的輸出

答案2

以防萬一它逃過了任何人的注意,我更喜歡forest畫樹。

此解決方案不需要手動重複“此處”一詞或手動調整箭頭起點的位置。它基於手冊第 20 頁的範例,儘管我可能對其進行了一些修改。

  • triangle用於屋頂
  • 定義了一個樣式,move={}{}它有兩個參數:第一個指定內容應複製到的節點;第二個指定要新增到目前節點的附加文字。

我假設您可能不想完全執行此操作,因此這更多的是出於說明目的,而不是現成的解決方案。在您給出的範例中,您將放置move={<specification of target node>}{Some text}並將來源節點的內容指定為here

然後,來源節點最終將包含Some text here,目標節點最終將包含,並將在來源節點正下方的點和目標節點正下方的點here之間繪製箭頭。herehere

我使用“相對節點遍歷”來指定目標節點。但是,您同樣可以添加, name=target到目標節點,然後直接放入move={target}{Some text},這在大多數情況下可能會更簡單。我已將此替代方案添加為樹中的註釋代碼來演示該想法。

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\begin{document}
  \newlength{\sourcetextaddwidth}
  \begin{forest}
    for tree={
      parent anchor=south,
      child anchor=north,
      fit=rectangle
    },
    move/.style n args=2{% page 20 of forest manual
      before typesetting nodes={
        TeX={
          \settowidth{\sourcetextaddwidth}{#2}
        },
        #1.content={##1},
        content={#2 ##1},
        tikz={
          \draw[->] (.south) +(.5\sourcetextaddwidth,0) to [out=south west, in=south] (#1);
        },
      },
    },
    [CP
      [this is the target node to which text will be moved%, name=target
      ]
      [C$'$
        [C
        ]
        [TP
          [here, triangle, move={!r1}{Some text}% move={target}{Some text}
          ]
        ]
      ]
    ]
  \end{forest}
\end{document}

自動樹

相關內容