代码

代码

我希望 TikZ 树节点内的文本居中;以下是我迄今为止的进展:

\documentclass[10pt, a4paper]{article}   
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=1pt, text centered,
  font=\sffamily},
  arn_m/.style = {treenode, rectangle, rounded corners=1mm, text centered, white,
  font=\sffamily\bfseries, draw=black, fill=black, text width=7em},
  arn_n/.style = {treenode, rectangle, white, font=\sffamily\bfseries, 
  draw=black, fill=black, text width=2em},
  arn_r/.style = {treenode, rectangle, red, draw=red, 
  text width=3em, very thick},
  arn_x/.style = {treenode, rectangle, draw=black,
  minimum width=0.5em, minimum height=0.5em}
}

\begin{center}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1cm}] 
\node [arn_m] {2 3 6 5 7}
    child{ node [arn_r] {15 2} 
            child{ node [arn_n] {10} 
                child{ node [arn_r] {5} edge from parent node[above left]
                         {$x$}} %for a named pointer
                            child{ node [arn_x] {}}
            }
            child{ node [arn_n] {20}
                            child{ node [arn_r] {18}}
                            child{ node [arn_x] {}}
            }                            
    }
    child{ node [arn_r] {47}
            child{ node [arn_n] {38} 
                            child{ node [arn_r] {36}}
                            child{ node [arn_r] {39}}
            }
            child{ node [arn_n] {51}
                            child{ node [arn_r] {49}}
                            child{ node [arn_x] {}}
            }
        }
; 
\end{tikzpicture}

\end{center}
\end{document}

如您所见,节点中的文本未正确居中,因为它偏向右侧,并且常规修复技术(如将文本居中等)无法解决问题。有人能给我提供一些关于如何正确对齐节点文本的提示吗?

答案1

罪魁祸首似乎是text width。如果将其更改为minimum width,则文本将正确居中。

代码

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\tikzset{
  treenode/.style = {align=center, inner sep=1pt,
  font=\sffamily},
  arn_m/.style = {treenode, rectangle, rounded corners=1mm, white,
  font=\sffamily\bfseries, draw=black, fill=black, minimum width=7em},
  arn_n/.style = {treenode, rectangle, white, font=\sffamily\bfseries, 
  draw=black, fill=black, minimum width=2em},
  arn_r/.style = {treenode, rectangle, red, draw=red, 
  minimum width=3em, very thick},
  arn_x/.style = {treenode, rectangle, draw=black,
  minimum width=0.5em, minimum height=0.5em}
}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1cm}] 
\node [arn_m] {2 3 6 5 7}
    child{ node [arn_r] {15 2} 
            child{ node [arn_n] {10} 
                child{ node [arn_r] {5} edge from parent node[above left]
                         {$x$}} %for a named pointer
                            child{ node [arn_x] {}}
            }
            child{ node [arn_n] {20}
                            child{ node [arn_r] {18}}
                            child{ node [arn_x] {}}
            }                            
    }
    child{ node [arn_r] {47}
            child{ node [arn_n] {38} 
                            child{ node [arn_r] {36}}
                            child{ node [arn_r] {39}}
            }
            child{ node [arn_n] {51}
                            child{ node [arn_r] {49}}
                            child{ node [arn_x] {}}
            }
        }
; 
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述


评论

  • text centered可能是一个弃用的选项;我在 TikZ 文档的任何地方都找不到它

  • 正如 Alan Munn 在他的评论中所说,请帮助我们帮助您,在未来发布可编译的 MWE。

相关内容