%%%use PDFLATEX
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{positioning}
\tikzset{edge from parent/.style=
{draw,
edge from parent path={(\tikzparentnode.east)
-- +(14pt,0)
|- (\tikzchildnode)}}}
\begin{document}\large
\vspace{1cm}
\begin{tikzpicture}
\tikzset{grow'=right,level distance=100pt}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}}
\Tree
[.\node(yawei){yawei};
[.\node(muqiu){muqiu}; ]
]
\begin{scope}[every node/.style={align=center, anchor=center, font=\normalsize\sffamily\bfseries,rectangle,draw,text width=1.4cm,}]
\node[above= 2mm of muqiu ] (thenode) {22};
\node[at =(thenode-|yawei)] {21};
\end{scope}
\end{tikzpicture}
\end{document}
我想要这个风格
答案1
不确定您想做什么,但是为什么您不像已经开始做的那样添加更多的孩子呢?
% arara: pdflatex
\documentclass[tikz, border=5]{standalone}
\usepackage{tikz-qtree}
\usetikzlibrary{positioning}
\begin{document}\large
\begin{tikzpicture}
\tikzset{grow'=right,level distance=100pt}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}}
\Tree
[.\node(yawei){yawei};
[.\node(muqiu){muqiu};
[.\node(1){1}; [.\node(2){2}; [.\node(3){3}; [.\node(4){4}; ]]]]
]
]
\begin{scope}[every node/.style={align=center, anchor=center, font=\normalsize\sffamily\bfseries,rectangle,draw,text width=1.4cm,}]
\foreach \x/\y in {yawei/21, muqiu/22, 1/23, 2/24, 3/25, 4/26}{%
\node[above= 2mm of \x ] (the\x) {\y};
}
\end{scope}
\end{tikzpicture}
\end{document}
答案2
将节点添加到当前代码的最简单方法可能是将它们简单地定位到 22 的右侧:
\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage{tikz-qtree}
\usetikzlibrary{positioning}
\tikzset{%
edge from parent/.style={%
draw,
edge from parent path={(\tikzparentnode.east) -- +(14pt,0) |- (\tikzchildnode)},
},
}
\begin{document}\large
\begin{tikzpicture}
[
grow'=right,
level distance=100pt,
execute at begin node=\strut,
every tree node/.style={anchor=base west},
]
\Tree
[.\node(yawei){yawei};
[.\node(muqiu){muqiu}; ]
]
\begin{scope}[every node/.style={align=center, anchor=center, font=\normalsize\sffamily\bfseries, rectangle, draw, text width=1.4cm,}]
\node [above=2mm of muqiu] (thenode) {22};
\node at (thenode-|yawei) {21};
\node (next) [right=of thenode] {23};
\node (one after) [right=of next] {24};
\node [right=of one after] {25};
\end{scope}
\end{tikzpicture}
\end{document}
如果图表实际上没有分支,那么将节点放置在网格上或使用chains
库可能会更容易。但是,这将涉及重写现有代码,如果只需要进行少量添加,这可能会带来更多的麻烦。