TransWikia.com

How to make a tree in Latex?

TeX - LaTeX Asked on August 17, 2021

How can we move A to the center?

documentclass[border=10pt]{standalone}
usepackage[edges]{forest}
usepackage[T1]{fontenc}
tikzset{%
  parent/.style={align=center,text width=3cm,rounded corners=3pt},
  child/.style={align=center,text width=3cm,rounded corners=3pt}
}
begin{document}
begin{forest}
  for tree={
    % edge+={->},% uncomment for arrows
    draw,
    rounded corners,
    node options={align=center,},
    text width=2.7cm,
  },
  where level=0{%
    parent anchor=children,
  }{%
    folder,
    grow'=0,
    if level=1{% this changes the edges from level 0 to nodes at level 1
      before typesetting nodes={child anchor=north},
      edge path'={(!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)},
    }{},
  }
  [A, fill=gray!25, parent
  [B, for tree={fill=brown!25, child}
  ]
  [C, for tree={fill=blue!25, child}
  ]
  ]
  ]
end{forest}
end{document}

One Answer

Your code is for this simple tree unnecessary complicated.

documentclass[border=10pt]{standalone}
usepackage[T1]{fontenc}
usepackage[edges]{forest}
usetikzlibrary{arrows.meta}

begin{document}
begin{forest}
  for tree={
% nodes
    draw,
    rounded corners,
    fill=gray!25,
    text width=3 cm,
    text centered,
% tree
    grow=270,
    parent anchor=south,
    child anchor=north,
%    edge = {-Straight Barb},
forked edge,
    l sep=8mm,
 fork sep=4mm,
    s sep=2mm
        }
[A
  [B, fill=brown!25]
  [C, fill=blue!25]
]
end{forest}
end{document}

enter image description here

Correct answer by Zarko on August 17, 2021

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