3

I want all the rectangle have the same size. What should I do?

\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
    You might add text width=1cm and align=center to the style declaration in the scope. – egreg Jan 24 '15 at 0:19
  • 1
    You really should consider taking a look at the package's documentation some time. You might be surprised: it includes examples and stuff of trees. – cfr Jan 24 '15 at 4:04
3

I have

  1. added the scope around tree and passed a style of text width=\widthof{Det} to the scope
  2. added a style minimum width=\widthof{FOURTH} to the scope of upper labels
  3. adjusted the level distance appropriately to accomodate these widths.

Code.

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

enter image description here

  • There's a spurious minimum width={\widthof{Det}} in the second scope. – Gonzalo Medina Jan 24 '15 at 2:10
  • @GonzaloMedina Thanks. I am sleep deprived :-) Corrected. – user11232 Jan 24 '15 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.