0

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} 
5
  • 1
    your preamble may be interfering --would you like to share Commented Mar 16, 2021 at 11:03
  • Unlike any other programming languages, it makes a lot of difference if you change the preamble of your document in terms of the output, such as clashing packages or page settings changed by some detail in the code and so on. That's why we need to have a complete example together with the relevant parts of your preamble included. Otherwise we might not be able to reproduce your problem. Commented Mar 16, 2021 at 14:37
  • Sensible - I will pop back shortly with the full example. Just need to go parent. Commented Mar 16, 2021 at 16:55
  • @jsbibra done :) Commented Mar 17, 2021 at 8:46
  • @Joe have a look now -- I have deleted some extra code not used -- invis/.style={ , punktchain/.style={ , element/.style={ , tuborg/.style={decorate}, tubnode/.style= Commented Mar 17, 2021 at 14:58

2 Answers 2

0

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} 
2

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} \renewcommand\ShowFrameLinethickness{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 Pi\\1976 [,coordinate, E=1.5] [Gravity's Rainbow\\ 1944] ] ] ] \end{forest} \caption{Food} \label{rewrite} \end{figure} \end{document} 
2
  • I tested this - it works perflectly and I learned a lot about the forest package in the process - I accepted the other answer simply because it was a much tighter shorter solution to the problem, but will press the bounty button for this one. Commented Mar 19, 2021 at 7:15
  • @Joe, your claims abot shorter solution simple doesn't hold. Commented Mar 19, 2021 at 7:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.