I'd like to have my text inside TikZ tree nodes centered; below is my progress so far:
\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}
As you can see the text in the nodes is not centered properly as it is biased to the right and normal fixing techniques (like putting text centered and so on) do not resolve the problem. Could someone provide me a tip on how to align the node text properly?
Best Answer
The culprit seems to be
text width
. If you change it tominimum width
, then the texts will be centered properly.Code
Output
Comments
text centered
may be a deprecated option; I couldn't find it anywhere in the TikZ documentationAs Alan Munn says in his comment, please help us help you by posting a compilable MWE in the future.