TransWikia.com

How to centre the root node of a tikz tree?

TeX - LaTeX Asked on April 8, 2021

I have the following tikz code:

begin{tikzpicture}[->,>=stealth',level/.style={text width=2cm, level distance = 2.8cm,level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2.5cm}, 
level 3/.style={sibling distance=3.5cm}, }] 
node[text width=2.5cm]  {{em The Book Thief}1939}
 child{ node [text width=2.5cm] {{em The Great Train Robbery}1855} 
      child{ node[text width=2.5cm]  {{em Ross Poldark}1782} 
      child{ node[text width=2.5cm]  {{em A Tale of Two Cities}1755}
      child{ node[text width=2.5cm]  {{em Girl with a Pearl Earring}1664 }}
      child{ node[draw=none]  {{}} }
 }
      child{ node[text width=2.5cm]  {{em The Revenant}1823} }
}
      child{ node[draw=none]  {{}} }
      }
      child{ node[text width=2.5cm]  {{em The Lion, the Witch, and the Wardrobe}1940}
      child{ node[draw=none]  {{}} }
      child{ node [text width=2.5cm] {{em The Life of Pi}1976}
      child{ node[draw=none]  {{}} }
      child{ node[text width=2.5cm]  {{em Gravity's Rainbow}1944} }                            }
  }
; 
end{tikzpicture}

It produces the following tree:

enter image description here

This isn’t what I’m looking for – I want the root to be the centre of the page and it’s children to be equal amounts to the left and right of it. My root note is currently too far left (this is also true for nodes further down the tree).

How can I enforce a nodes children being equally distant horizontally?

EDIT: example in full context, as sensibly requested, thank you understanding the whole ‘wall of code’.

documentclass[12pt,a4paper]{book}
UseRawInputEncoding
usepackage{tikz}
usetikzlibrary {positioning}
usetikzlibrary{trees}
usetikzlibrary{arrows}
usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols}


begin{document}
tikzstyle{every node}=[draw=black,thick,text centered,anchor=west,text width=2.4cm]
tikzstyle{selected}=[draw=red,fill=red!30]
tikzstyle{optional}=[dashed,fill=gray!50]
tikzset{
>=stealth',
  invis/.style={
    rectangle, 
    rounded corners, 
    % fill=black!10,
    draw=white, very thick,
    text width=4em, 
    minimum height=2em, 
    text centered, 
    on chain},
  punktchain/.style={
    rectangle, 
    rounded corners, 
    % fill=black!10,
    draw=black, very thick,
    text width=5em, 
    minimum height=2em, 
    text centered, 
    on chain},
  line/.style={draw, thick, <-},
  element/.style={
    tape,
    top color=white,
    bottom color=blue!50!black!60!,
    minimum width=4em,
    draw=blue!40!black!90, very thick,
    text width=5em, 
    minimum height=2.0em, 
    text centered, 
    on chain},
  every join/.style={->, thick,shorten >=1pt},
  decoration={brace},
  tuborg/.style={decorate},
  tubnode/.style={midway, right=7pt,draw=gray!01,ultra thin},
}

%The Books
begin{figure} 
tikzstyle{fade}=[dashed,fill=gray!20]
begin{center}
begin{tikzpicture}[->,>=stealth',level/.style={text width=2cm, level distance = 2.8cm,level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2.5cm}, 
level 3/.style={sibling distance=3.5cm}, }] 
node[text width=2.5cm]  {{em The Book Thief}1939}
 child{ node [text width=2.5cm] {{em The Great Train Robbery}1855} 
      child{ node[text width=2.5cm]  {{em Ross Poldark}1782} 
      child{ node[text width=2.5cm]  {{em A Tale of Two Cities}1755}
      child{ node[text width=2.5cm]  {{em Girl with a Pearl Earring}1664 }}
      child{ node[draw=none]  {{}} }
 }
      child{ node[text width=2.5cm]  {{em The Revenant}1823} }
}
      child{ node[draw=none]  {{}} }
      }
      child{ node[text width=2.5cm]  {{em The Lion, the Witch, and the Wardrobe}1940}
      child{ node[draw=none]  {{}} }
      child{ node [text width=2.5cm] {{em The Life of Pi}1976}
      child{ node[draw=none]  {{}} }
      child{ node[text width=2.5cm]  {{em Gravity's Rainbow}1944} }                            }
  }
; 
end{tikzpicture}
end{center}
caption{Food}
label{rewrite} 
end{figure}

end{document} 

2 Answers

you have placed anchor=west in the following definition -- I feel it should be north as below

tikzstyle{every node}=[draw=black,thick,text centered,anchor=north,text width=2.4cm]

the result

enter image description here

MWE

documentclass[12pt,a4paper]{book}
%UseRawInputEncoding
usepackage{tikz}
usetikzlibrary {positioning}
usetikzlibrary{trees}
usetikzlibrary{arrows}
usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols}


begin{document}
tikzstyle{every node}=[draw=black,thick,text centered,anchor=north,text width=2.4cm]

%The Books
begin{figure} 

begin{center}
begin{tikzpicture}[
    ->,>=stealth',
    level/.style={text width=2cm, level distance = 2.8cm,
        level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2.5cm}, 
level 3/.style={sibling distance=3.5cm}, }] 
node[text width=2.5cm]  {{em The Book Thief}1939}
 child{ node [text width=2.5cm] {{em The Great Train Robbery}1855} 
      child{ node[text width=2.5cm]  {{em Ross Poldark}1782} 
      child{ node[text width=2.5cm]  {{em A Tale of Two Cities}1755}
      child{ node[text width=2.5cm]  {{em Girl with a Pearl Earring}1664 }}
      child{ node[draw=none]  {{}} }
 }
      child{ node[text width=2.5cm]  {{em The Revenant}1823} }
}
      child{ node[draw=none]  {{}} }
      }
      child{ node[text width=2.5cm]  {{em The Lion, the Witch, and the Wardrobe}1940}
      child{ node[draw=none]  {{}} }
      child{ node [text width=2.5cm] {{em The Life of Pi}1976}
      child{ node[draw=none]  {{}} }
      child{ node[text width=2.5cm]  {{em Gravity's Rainbow}1944} }                            }
  }
; 
end{tikzpicture}
end{center}
caption{Food}
label{rewrite} 
end{figure}

end{document} 

Correct answer by js bibra on April 8, 2021

With forest package:

enter image description here

(red lines show text border)

documentclass[12pt,a4paper]{book}usepackage{forest}
usepackage{tikz}
usetikzlibrary{arrows.meta,
                shadows}
%---------------- show page layout. don't use in a real document!
usepackage{showframe}
renewcommandShowFrameLinethickness{0.15pt}
renewcommand*ShowFrameColor{color{red}}
%---------------------------------------------------------------%

begin{document}
begin{figure}[ht]
centering
    begin{forest}
for tree = {
% nodes style
           draw,
           fill = white, drop shadow,
     text width = 6em, text badly centered,
      inner sep = 2pt,
% tree with fixed angles of edges
                   l sep = 8mm,
                  calign = fixed edge angles,
    calign primary angle = -30,
  calign secondary angle =  37,
       E/.style = {edge={shorten >=1.5em}},
          }
[The Book Thief 1939
    [The Great Train Robbery 1855
        [Ross Poldark 1782
            [A Tale of Two Cities 1755
                [Girl with a Pearl Earring 1664]
                [, coordinate, E]            
            ]
            [The Revenant 1823]
        ]
        [,coordinate, E]
    ]
    [{The Lion, the Witch, and the Wardrobe} 1940
        [,coordinate, E]
        [The Life of Pi1976
            [,coordinate, E=1.5]
            [Gravity's Rainbow 1944]
        ]    
    ]
]
    end{forest}
caption{Food}
label{rewrite}
end{figure}
end{document} 

Answered by Zarko on April 8, 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