Floresta: ajuste o caminho da borda da árvore

Floresta: ajuste o caminho da borda da árvore

Eu quero desenhar uma árvore como a mostrada emeste tópico. No entanto, o texto nos meus nós pode ser longo e causar quebras de linha. Então, a anchor=150opção do thread atrapalha o alinhamento horizontal dos grandchildrennós. Quando uso anchor=westo alinhamento horizontal está bom, mas os nós se deslocam para a direita e cruzam a próxima coluna.

O que posso fazer para evitar isso?


MWE

\documentclass{standalone}

\usepackage{forest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
% \usetikzlibrary{fpu}
\usetikzlibrary{intersections}
\usetikzlibrary{trees}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{shadows}
\useforestlibrary{edges}

\tikzset{
  parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
  child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
  grandchild/.style={fill=white,text width=2.3cm}
}

\begin{document}

\begin{forest}
  for tree={%
    thick,
    drop shadow,
    l sep=0.8cm,
    s sep=1.0cm,
    node options={
      draw,
      font=\sffamily
    },
    edge={
      semithick,
      -Latex
    },
    where level=0{parent}{},
    where level=1{
      minimum height=1cm,
      child,
      parent anchor=south west,
      tier=p,
      l sep=0.45cm,
      for descendants={%
        grandchild,
        minimum height=0.6cm,
        anchor=west,
        edge path={
          \noexpand\path[\forestoption{edge}]
          (!to tier=p.parent anchor) |-(.child anchor)\forestoption{edge label};
        },
      }
    }{},
  }
  [\large Long text with line break%
    [\textbf{Test 1} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 2} \\ with a lot of subtext% 
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 3} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 4} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
  ]     
\end{forest}

\end{document}

Resultado

insira a descrição da imagem aqui

Responder1

Você está carregando a edgesbiblioteca, mas na verdade não a utiliza, o que é estranho, pois esse é precisamente o tipo de estilo que ela suporta. Além disso, elimina a necessidade de falsificar a estrutura da árvore, permitindo manter as relações intuitivas entre pais e filhos em sua marcação.

Neste caso, o folderestilo é a escolha óbvia, pelo menos ignorando o seu nome enganador. O único problema é que ele não faz a coisa certa quando se aplica apenas além de um certo nível e, como tenta fazer a coisa certa de maneira inteligente, você precisa substituí-lo no final do processamento da árvore.

Por exemplo,

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows,arrows.meta}
\tikzset{
  parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
  child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
  grandchild/.style={fill=white,text width=2.3cm}
}
\begin{document}
\begin{forest}
  for tree={%
    thick,
    drop shadow,
    node options={
      draw,
      font=\sffamily
    },
    edge={
      semithick,
      -Latex
    },
    where level=0{
      parent,
      l sep'=0.8cm,
      s sep'=1.0cm,
    }{
      folder,
      grow'=0,
    },
    where level=1{
      minimum height=1cm,
      child,
      l sep=7.5mm,
      for descendants={%
        grandchild,
        minimum height=0.6cm,
      },
      for children={
        before computing xy={s+=5mm},
      }
    }{},
  }
  [\large Long text with line break%
    [\textbf{Test 1} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 2} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 3} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 4} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
  ]
\end{forest}
\end{document}

informação relacionada