如何使 tikz 树的根节点居中?

如何使 tikz 树的根节点居中?

我有以下 tikz 代码:

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

它生成以下树:

在此处输入图片描述

这不是我想要的 - 我希望根节点位于页面的中心,而它的子节点位于页面的左右两侧。我的根节点目前太靠左了(对于树下较远的节点也是如此)。

如何强制节点的子节点在水平方向上距离相等?

编辑:完整上下文中的示例,正​​如合理要求的那样,感谢您理解整个“代码墙”。

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

答案1

你已将anchor=west以下定义放入其中——我觉得它应该是下面的北方

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

结果

在此处输入图片描述

平均能量损失

\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

forest包装:

在此处输入图片描述

(红线表示文字边框)

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

相关内容