1

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.

  • Welcome! Please can you complete your code so that we can copy-paste-compile it? – cfr Nov 18 '15 at 0:05
  • What is the point of the \scalebox? – cfr Nov 18 '15 at 0:07
1

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}
  • 1
    An excellent idea, setting the root node's width to \linewidth! (Much easier than the solution I wanted to propose.) But why not just set minimum width=\linewidth,inner xsep=0,outer xsep=0 to the (original) root node and be done with it? – Sašo Živanović Nov 18 '15 at 0:52
  • @SašoŽivanović I thought if I did that that the child anchor would be in the wrong place, so that the edges to its children would not start at the right place. – cfr Nov 18 '15 at 1:14
  • @SašoŽivanović Oh, yes. OK. I see. Thank you. I was thinking in terms of text width for some reason. Didn't occur to me to use minimum width. No idea why. Are you posting an answer or should I simplify mine? – cfr Nov 18 '15 at 1:19
  • Actually, my "simplification" to your answer indeed does not work if the parent anchor is not (vertically) in the center, e.g. if it's north east. So, looking more generally, your worry is a good one. – Sašo Živanović Nov 18 '15 at 1:21
  • 1
    No it's good! Actually, I will post the answer I have originally envisioned, as I have just now realized it can be used to center any node in the tree. Plus, it shows off v2 ;-) – Sašo Živanović Nov 18 '15 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.