[Tex/LaTex] Center root of forest tree

forestroot

In an assignment for school I require making a lot of trees and large ones too. I searched for some methods and stumbled upon the package forest which has helped me a lot. I'm almost done but couldn't figure out one last essential thing. I want to center the root node of my tree. I center my tree already but it centers the center of the tree rather than the root node too. I'm looking for a way to center the root node specifically.

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{center}
\scalebox{1}{
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
l=2cm 
}    
[ {[?,?,?,?]}, calign=center
    [ {[Intel,?,?,?]}
        [ {[i7,?,?,?]}
        ]
    ]
    [ {[?,Radeon,?,?]}
    ]
    [ {[?,?,4GB,?]}
        [ {[Intel,?,4GB,?]}
        ]
        [ {[?,Radeon,4GB,?]}
        ] 
        [ {[?,?,8GB,?]}
        ]
        [ {[?,?,4GB,Asus]}
        ]
    ]
]    
\end{forest}
}
\end{center}  
\end{document}

The root node [?,?,?,?] is positioned a couple of centimeters to the left of the center of the page whereas the tree as a whole is in the center.

Best Answer

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:

centred root

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}
Related Question