Search code examples
latextikz

Create crossing branches in a tree using tikzpicture and qtree


To illustrate an illegal prosodic structure, I need to create a metrical structure tree featuring crossing branches. I've seen it done in another paper (from the 80s) and I want to create the same structure in Tex then work from there.

Link to image (can't insert as I only just joined): https://www.pastepic.xyz/image/NtmmH

The code below creates the legal version of the tree. I want to recreate the one in the picture above using similar code.

\usepackage{tikz-qtree,tikz-qtree-compat}
\begin{document}

\begin{tikzpicture} \label{tree3}
[every tree node/.style={align=center,anchor=base},sibling distance=.8cm]
\Tree [.W [.\node(S1) {\\.$\Sigma$}; [.\node(s1) { $\sigma$};[.\node(fan) { fan};]]  [.\node(s2) { $\sigma$}; [.\node(ta) { ta};]]][.\node(S2) { $\Sigma$}; [.\node(s3) { $\sigma$};[.\node(fu) { fu};]]  [.\node(s4) { $\sigma$}; [.\node(cking) { cking};]]] [.\node(S3) { $\Sigma$}; [.\node(s5){ $\sigma$}; [.\node(stic){ stic};]]]]
\end{tikzpicture}

\end{document}

Solution

  • Here is a possible solution.

    The idea is to generate the text independently of the tree, in such a way that every letter is a named node. Then connect the nodes of the tree to these letters. The connection can be at will regular or include irregular branches

    \documentclass{article}
    
    \usepackage{tikz}
    \usepackage{tikz-qtree,tikz-qtree-compat}
    
    \begin{document}
    
    \begin{tikzpicture} \label{tree3}
    [every tree node/.style={align=center,anchor=base},sibling distance=.8cm]
      % to avoid name clash renamed s1 to ss1, s2 to ss2 etc.
    \Tree [.W [.\node(S1) {\\.$\Sigma$};
                [.\node(ss1) { $\sigma$};
                 %[.\node(fan) { fan};]
                ]
                [.\node(ss2) { $\sigma$};
                 %[.\node(ta) { ta};]
                ]]
              [.\node(S2) { $\Sigma$};
                [.\node(ss3) { $\sigma$};
                 %[.\node(fu) { fu};]
                ]
                [.\node(ss4) { $\sigma$};
                 %[.\node(cking) { cking};]
                ]]
              [.\node(S3) { $\Sigma$};
                [.\node(ss5){ $\sigma$};
                  %[.\node(stic){ stic};]
              ]]]
    
        \begin{scope}[every node/.style={inner sep=0, outer xsep=0.1mm,
                      anchor=base west}]
          % adjust outer xsep to control letter spacing
          % anchoring is done on baseline of letters to ensure proper alignment        
          \coordinate[below of=ss1, xshift=-0.3cm, yshift=0] (start);
          % The start of the text.
          %    adjust xshift and yshift to finely control text position
          % Create the letters/node names in a loop
          \foreach \l/\n [remember=\n as \prev (initially start)] in
            % the list is a letter / its node name.
            % Numbering is based on the number of the node that the 
            %   letters are connected to, in order to simplify connections
            %   so f1 is a child of node ss1 for example
            {f/f1,a/a1,n/n1,t/t2,a/a2,f/f3,u/u3,c/c4,k/k4,i/i4,n/n4,g/g4,s/s5,t/t5,i/i5,c/c5} {
            \node at (\prev.base east) (\n) {\l};
          }
        \end{scope}
    
      % connect a node with a leaf with proper alignment to the 
      %   taller letter (the 'f' in node f1)
      % usage to \toleaf{ss1}{f3} connect node ss1 to node f3
      % adjust yshift for the desired effect
      \def\toleaf#1#2{ (#1.south) -- ([yshift=0.1ex]#2|-f1.north) }
      % connect a node to its children node
      % Usage \toleaves{1}{f,a} connect ss1 to f1 and ss1 to a1
      % used for regular tree connections
      \def\toleaves#1#2{\foreach \n in {#2} { \toleaf{ss#1}{\n#1} }}
      \draw
        \toleaves{1}{f,a,n}
        \toleaves{2}{t,a}
        \toleaves{3}{f,u}
        \toleaves{4}{c,k,i,n,g}
        \toleaves{5}{s,t,i,c}
        %% add some crossing  branches with \toleaf
        \toleaf{ss1}{c4}
        \toleaf{ss4}{a2}
        ;
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here