TransWikia.com

TikZ - forest - for nodewalk to add a root-to-leaf path

TeX - LaTeX Asked on October 11, 2020

In the following M(erly)WE, here is my goal.

  1. I would like to give the colors as an argument.
  2. I would like to decorate all the edges and nodes from the root to the leaf marked.

Here is the output of the code below.

enter image description here

I am trying to use for nodewalk but I don’t see how to…

  • … define the node as an argument or using a style,
  • … define the color as an argument,
  • … put a frame around each nodes,
  • … have the good left points for the colored edges.

Here is the partial code.

documentclass[12pt,a4paper]{article}

usepackage{forest}

begin{document}

section{Mark all nodes}

begin{forest}
  for tree = {
    grow'  = 0,
    l      = 2.5cm,
    parent anchor=east,
    s sep  = 1.2cm,
  },
  mark/.style = {
    mark node,edge=mark line
  },
  mark node/.style = {
    draw = red,
    line width = 2pt,
    rounded corners
  },
  /tikz/mark line/.style={
    red,
    line width = 2.5pt,
    line cap = round,
    shorten >= -1pt, % this (TikZ) key deals with "links" between edges and nodes
  },
  [
    [A, mark=red
      [B]
      [C,mark=blue]
    ]
    [D, mark=red
      [E, mark=red
        [F]
        [G, mark=red]
        [H]
      ]
    ]
  ]
end{forest}


section{Try to indicate only the leaf}

begin{forest}
  for tree = {
    grow'  = 0,
    l      = 2.5cm,
    parent anchor=east,
    s sep  = 1.2cm,
  },
  before typesetting nodes={
    for nodewalk={
      c,
      every step={
        tikz/.wrap pgfmath arg={
          draw[red,
                line width = 2.5pt,
                line cap = round] ()--(#1);
      }{name("!b")}
    },
    211,ancestors
    }{}
  },
  [
    [A,
      [B]
      [C]
    ]
    [D
      [E
        [F]
        [G]
        [H]
      ]
    ]
  ]
end{forest}

end{document}

One Answer

I don't know if I understood correctly your problem... Using only tikz, I suggest the following solution. There are two styles for the nodes and the edges: snode for simple, cnode for colored...

enter image description here

documentclass[12pt, border=1cm]{standalone}
usepackage{tikz}
usetikzlibrary{positioning}

begin{document}

tikzset{%
  node distance=1.5cm and 0.2cm,
  snode/.style={rectangle, rounded corners},
  cnode/.style={draw, red, rectangle, rounded corners, line width=1.75pt, text=black},
  sedge/.style={black, thin},
  cedge/.style={red, line width=1.75pt}
}

begin{tikzpicture}
  node[inner sep=0pt, outer sep=0pt] (R) {};
  node[snode, above right=1.5cm and 2cm of R] (A) {A};
  node[snode, below right=1.5cm and 2cm of R] (D) {D};
  node[snode, above right=1cm and 2cm of A] (B) {B};
  node[snode, below right=1cm and 2cm of A] (C) {C};
  node[snode, right=2cm of D] (E) {E};
  node[snode, above right=1.3cm and 2cm of E] (F) {F};
  node[snode, right=2cm of E] (G) {G};
  node[snode, below right=1.3cm and 2cm of E] (H) {H};

  path
  (R) edge[sedge] (A) (A.east) edge[sedge] (B)
  (R) edge[sedge] (A) (A.east) edge[sedge] (C)
  (R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (F) 
  (R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (G) 
  (R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (H); 
end{tikzpicture}
qquad
begin{tikzpicture}
  node[inner sep=0pt, outer sep=0pt] (R) {};
  node[snode, above right=1.5cm and 2cm of R] (A) {A};
  node[cnode, below right=1.5cm and 2cm of R] (D) {D};
  node[snode, above right=1cm and 2cm of A] (B) {B};
  node[snode, below right=1cm and 2cm of A] (C) {C};
  node[cnode, right=2cm of D] (E) {E};
  node[cnode, above right=1.3cm and 2cm of E] (F) {F};
  node[snode, right=2cm of E] (G) {G};
  node[snode, below right=1.3cm and 2cm of E] (H) {H};

  path
  (R) edge[sedge] (A) (A.east) edge[sedge] (B)
  (R) edge[sedge] (A) (A.east) edge[sedge] (C)
  (R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (G) 
  (R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (H)
  (R) edge[cedge] (D) (D.east) edge[cedge] (E) (E.east) edge[cedge] (F); 
end{tikzpicture}

end{document}

Answered by Daniel N on October 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP