![如何获得相同尺寸的盒子?](https://linux22.com/image/304323/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%BE%97%E7%9B%B8%E5%90%8C%E5%B0%BA%E5%AF%B8%E7%9A%84%E7%9B%92%E5%AD%90%EF%BC%9F.png)
我希望所有矩形都具有相同的大小。我应该怎么做?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tikz-qtree}
\tikzset{edge from parent/.style=
{draw,
edge from parent path={(\tikzparentnode.east)
-- +(8pt,0)
|- (\tikzchildnode)}}}
\begin{document}
\begin{tikzpicture}
\tikzset{grow'=right,level distance=1.5cm}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}}
\Tree [.\node(root){S}; [.\node(np){NP}; [.\node(d){Det}; \node(t){the}; ] [.N cat ] ]
[.VP [.V sat ]
[.PP [.P \node(o){on}; ]
[.NP [.Det the ] [.N \node(m){mat}; ] ] ] ] ]
\begin{scope}[every node/.style={align=left, anchor=center, font=\tiny\sffamily\bfseries,rectangle,draw}]
\node[above= 2mm of t] (thenode) {FOURTH};
\node[at =(thenode-|root)] {FIRST};
\node[at =(thenode-|np)] {SECOND};
\node[at =(thenode-|d)] {THIRD};
\node[at =(thenode-|o)] {FIFTH};
\node[at =(thenode-|m)] {SIXTH};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
我有
- 在树周围添加了范围,并将的样式传递
text width=\widthof{Det}
给范围 minimum width=\widthof{FOURTH}
为上部标签的范围添加了样式- 适当调整水平距离以适应这些宽度。
代码。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tikz-qtree}
\tikzset{edge from parent/.style=
{draw,
edge from parent path={(\tikzparentnode.east)
-- +(8pt,0)
|- (\tikzchildnode)}}}
\begin{document}
\begin{tikzpicture}
\tikzset{grow'=right,level distance=4.5em}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}}
\begin{scope}[text width=\widthof{Det}] %%<--- this
\Tree [.\node(root){S}; [.\node(np){NP}; [.\node(d){Det}; \node(t){the}; ] [.N cat ] ]
[.VP [.V sat ]
[.PP [.P \node(o){on}; ]
[.NP [.Det the ] [.N \node(m){mat}; ] ] ] ] ]
\end{scope}
\begin{scope}[every node/.style={align=left, anchor=center,
font=\tiny\sffamily\bfseries,rectangle,draw,
minimum width=\widthof{FOURTH}}]
\node[above= 2mm of t] (thenode) {FOURTH};
\node[at =(thenode-|root)] {FIRST};
\node[at =(thenode-|np)] {SECOND};
\node[at =(thenode-|d)] {THIRD};
\node[at =(thenode-|o)] {FIFTH};
\node[at =(thenode-|m)] {SIXTH};
\end{scope}
\end{tikzpicture}
\end{document}