I am also learning forest
. This is my attempt
\documentclass{article}
\usepackage{forest}
\usetikzlibrary{shadows,arrows.meta}
\tikzset{parent/.style={align=center,text width=2cm,fill=green!20,rounded corners=2pt},
child/.style={align=center,text width=2.8cm,fill=green!50,rounded corners=6pt},
grandchild/.style={fill=pink!50,text width=2.3cm}
}
\begin{document}
\begin{forest}
for tree={%
thick,
drop shadow,
l sep=0.6cm,
s sep=0.8cm,
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.25cm,
for descendants={%
grandchild,
minimum height=0.6cm,
anchor=150,
edge path={
\noexpand\path[\forestoption{edge}]
(!to tier=p.parent anchor) |-(.child anchor)\forestoption{edge label};
},
}
}{},
}
[Drawing\\diagrams
[Defining node and arrow styles
[Setting shape
[Choosing color
[Adding shading]
]
]
]
[Positioning the nodes
[Using a Matrix
[Relatively
[Absolutely
[Using overlays]
]
]
]
]
[Drawing arrows between nodes
[Default arrows
[Arrow library
[Resizing tips
[Shortening
[Bending]
]
]
]
]
]
]
\end{forest}
\end{document}
I don't recommend this. For me, it produces an overfull box and, even if it doesn't in this case, it very likely will in others. Moreover, the tree will not look centred if it is asymmetrical.
But you can, if you so wish do it:
The dashed blue line is just to show the centre of the page.
The centring is achieved using an empty, undrawn node as the root of width \linewidth
. The visible root is then aligned with the real root, which puts it dead centre.
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{tikzpicture}[overlay, remember picture]% this is just to show the centre of the page
\draw [dashed, draw=blue!50!white] (current page.north) -- (current page.south);
\end{tikzpicture}
\begin{center}
\begin{forest}
for tree={
if level=0{
text height=0pt,
text width=\linewidth,
inner sep=0pt,
outer sep=0pt,
parent anchor=north
}{
parent anchor=south,
child anchor=north,
align=center,
if level=1{
l=0pt,
no edge,
}{
l=2cm
},
},
},
[
[{[?,?,?,?]}
[{[Intel,?,?,?]}
[{[i7,?,?,?]}
]
]
[{[?,Radeon,?,?]}
]
[{[?,?,4GB,?]}
[{[Intel,?,4GB,?]}
]
[{[?,Radeon,4GB,?]}
]
[{[?,?,8GB,?]}
]
[{[?,?,4GB,Asus]}
]
]
]
]
\end{forest}
\end{center}
\end{document}
EDIT
In this particular case, because the node anchors are centred, as Sašo Živanović points out, a simpler version is possible using minimum width
rather than text width
and applying to the existing root node. The will make vertical alignment with other document elements simpler but will fail if parent anchor
is not centred.
\begin{center}
\begin{forest}% simplified with Sašo Živanović's suggestion (https://tex.stackexchange.com/questions/278708/center-root-of-forest-tree/278793?noredirect=1#comment671409_278793)
for tree={
parent anchor=south,
child anchor=north,
align=center,
l=2cm,
if level=0{
minimum width=\linewidth,
inner xsep=0pt,
outer xsep=0pt,
}{},
},
[{[?,?,?,?]}
[{[Intel,?,?,?]}
[{[i7,?,?,?]}
]
]
[{[?,Radeon,?,?]}
]
[{[?,?,4GB,?]}
[{[Intel,?,4GB,?]}
]
[{[?,Radeon,4GB,?]}
]
[{[?,?,8GB,?]}
]
[{[?,?,4GB,Asus]}
]
]
]
\end{forest}
\end{center}
Best Answer
There's no need to put the diagram in a floating environment. You can instead wrap it in a
\begin{center} ... \end{center}
environment.