Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can i highlight tree levels to be some thing like that...enter image description here

I've drawn my tree with this code

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz-qtree}
\usetikzlibrary{shadows,trees}
\begin{document}
\tikzset{font=\large,
edge from parent fork down,
level distance=60pt,
every node/.style=
    {top color=white,
    bottom color=blue!25,
    rectangle,rounded corners,
    minimum height=8mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
    text depth = 0pt
    },
edge from parent/.style=
    {draw=blue!50,
    thick
    }}

\centering
\begin{tikzpicture}

\Tree [.Parent
        [.one 
            [.one1 
                   [.one11 
                        [.one111 ]
                        [.one112 ]
                        [.one113 ] ]
                   [.one12 ] 
                   [.one13 ] ] 
            [.one2 one21 ] ]
        [.two 
            [.two1 two11 ]
            [.two2 [.two21 two211 ]
                   [.two22 [.two221 two2211 ] 
                           [.two222 two321 ] ] ] ] 
        [.three
            [.three1 ] ] ]

\end{tikzpicture}
\end{document}
share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

One possibility is to name some of the tree nodes, define some auxiliary nodes (for the text) and coordinates and then use the fit and background libraries to draw the background filling (the code can be simplified using \foreach, but this is left as an exercise):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[landscape]{geometry}
\usepackage[T1]{fontenc}
\usepackage{tikz-qtree}
\usetikzlibrary{positioning,shadows,trees,backgrounds,fit}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\definecolor{myfilling}{RGB}{208,215,222}

\begin{document}
\tikzset{font=\large,
edge from parent fork down,
level distance=60pt,
edge from parent/.style=
    {draw=blue!50,
    thick
    },
filling/.style=
    {minimum height=12mm,
     draw=myfilling!75!black,
     very thick,
     drop shadow,
     rectangle,rounded corners,
     fill=myfilling,
    },
ntext/.style=
    {draw=none,
     text width=4cm,
     align=center
    },
}

\centering
\begin{tikzpicture}
\begin{scope}[
every node/.style=
    {top color=white,
    bottom color=blue!25,
    rectangle,rounded corners,
    minimum height=8mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
    text depth = 0pt
    },
]
\Tree [.\node (p) {Parent};
        [.\node (o) {one}; 
            [.\node (1) {one1}; 
                   [.\node (11) {one11}; 
                        [.\node (111) {one111}; ]
                        [.one112 ]
                        [.one113 ] ]
                   [.one12 ] 
                   [.one13 ] ] 
            [.one2 one21 ] ]
        [.two 
            [.two1 two11 ]
            [.two2 [.two21 two211 ]
                   [.two22 [.two221 \node (2211) {two2211}; ] 
                           [.two222 two321 ] ] ] ] 
        [.three
            [.\node (31) {three1}; ] ] ]
\end{scope}
% auxuliary constructs for level 4
\node[left=of 111,ntext] 
  (text4) {Level 4 text};
\coordinate (aux2) at ([xshift=5pt]31.east|-text4);
% auxuliary constructs for level 3
\node[ntext] at (text4|-11) 
  (text3) {Level 3 text};
\coordinate (aux4) at ([xshift=5pt]31.east|-11);
% auxuliary constructs for level 2
\node[ntext] at (text4|-1) 
  (text2) {Level 2 text};
\coordinate (aux6) at ([xshift=5pt]31.east|-1);
% auxuliary constructs for level 1
\node[ntext] at (text4|-o) 
  (text1) {Level 1 text};
\coordinate (aux8) at ([xshift=5pt]31.east|-o);
% auxuliary constructs for level 0
\node[ntext] at (text4|-p) 
  (text0) {Level 0 text};
\coordinate (aux10) at ([xshift=5pt]31.east|-p);
% auxuliary constructs for level 5
\node[ntext] at (text4|-2211) 
  (text5) {Level 5 text};
\coordinate (aux12) at ([xshift=5pt]31.east|-2211);
\begin{pgfonlayer}{background}
\node[filling,fit=(text4) (aux2)] {};
\node[filling,fit=(text3) (aux4)] {};
\node[filling,fit=(text2) (aux6)] {};
\node[filling,fit=(text1) (aux8)] {};
\node[filling,fit=(text0) (aux10)] {};
\node[filling,fit=(text5) (aux12)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
who can i write text in the highlight? –  Misaki Sep 20 '13 at 2:23
    
@Misaki the background highlighting is a node, so you can use it to place text; in some minutes I'll update my answer with some text. –  Gonzalo Medina Sep 20 '13 at 2:24
    
Thank you very much, you are amazing. –  Misaki Sep 20 '13 at 2:31
    
@Misaki please see my updated answer. –  Gonzalo Medina Sep 20 '13 at 2:53
add comment

What I do here is inset the tree you created over a background that contains the yellow bars and text. Care had to be taken to match up the right size of the yellow bars (with the \rule definition in \crow and the gap size (the \vspace in \crow). I also set the width to 9".

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{stackengine}[2013-09-11]
\usepackage{xcolor}
\newcommand\crow[1]{\colorbox{yellow}{\parbox{9in}{%
  \rule[-5pt]{0pt}{25pt}\Huge\bfseries #1\hfill}}\par\vspace*{28pt}}

\usepackage{tikz-qtree}
\usetikzlibrary{shadows,trees}
\begin{document}
\tikzset{font=\large,
edge from parent fork down,
level distance=60pt,
every node/.style=
    {top color=white,
    bottom color=blue!25,
    rectangle,rounded corners,
    minimum height=8mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
    text depth = 0pt
    },
edge from parent/.style=
    {draw=blue!50,
    thick
    }}

\newsavebox{\myboxA}
\newsavebox{\myboxB}
\centering
\sbox{\myboxA}{%
\begin{tikzpicture}
\Tree [.Parent
        [.one 
            [.one1 
                   [.one11 
                        [.one111 ]
                        [.one112 ]
                        [.one113 ] ]
                   [.one12 ] 
                   [.one13 ] ] 
            [.one2 one21 ] ]
        [.two 
            [.two1 two11 ]
            [.two2 [.two21 two211 ]
                   [.two22 [.two221 two2211 ] 
                           [.two222 two321 ] ] ] ] 
        [.three
            [.three1 ] ] ]

\end{tikzpicture}%
}
\sbox{\myboxB}{\parbox{9in}{\crow{Parent}\crow{Level 1}\crow{Level 2}%
  \crow{Level 3}\crow{Level 4}\crow{Level 5}}}
\hsmash{\stackinset{r}{}{t}{3pt}{\usebox{\myboxA}}{\usebox{\myboxB}}}
\end{document}

{tikz-pgf}{graphics}{highlighting}{tikz-trees} 

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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