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

3 Answers 3

up vote 12 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
    
Too much typing ;). –  cfr Jun 26 at 3:02

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

This is just me practising by experimenting with a package I wrote to answer another question. That package is justtrees and it is a simplified version of prooftrees which I wrote out of frustration. Both packages provide a forest style and the new versions offer a corresponding environment which has various advantages.

This uses v0.03 of the package. Just ask if you feel brave enough to try it yourself!

The invocation of right justifications at the beginning ensures that right just 1, right just 2 etc. are created even though no visible nodes are wanted there. It isn't necessary to add left justifications because left just={<text>} will ensure this happens anyway.

These nodes get automatically named and are then used to draw the background rectangles behind the tree. This has to be done using forest's end draw because otherwise the rectangles are drawn on top of the tree, even if we try to place them on the background layer.

just format is used to specify the format of the level descriptors. Among other things, this ensures that their height matches that of the nodes in the main tree, which is needed to get appropriately sized background rectangles.

I'm quite surprised how well this worked, to be honest as I wouldn't trust my code for one minute.

\documentclass[tikz,border=5pt,multi]{standalone}

\usepackage{justtrees}
\usetikzlibrary{shadows,fit,backgrounds}
\tikzset{
  my node/.style={
    top color=white,
    bottom color=blue!25,
    rounded corners,
    minimum height=8mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
    text depth = 0pt,
  },
  level descriptors/.style={
    font=\sffamily,
    text=blue!75!black,
    minimum height=8mm,
    text depth = 0pt,
  },
  my edges/.style={
    draw=blue!50,
    ultra thick,
    rounded corners
  },
  my level fill/.style={
    draw=blue!75, fill=blue!20, drop shadow, rounded corners
  }
}

\begin{document}
\begin{justtree}
  {
    right justifications,
    just format/.append style={level descriptors},
    for tree={
      my node,
      edge={my edges},
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-2mm-5pt) -| (.child anchor)\forestoption{edge label};
      },
      l sep+=2mm,
    },
    end draw/.code={
        \begin{scope}[on background layer]
          \foreach \i in {1,...,6}{
            \node [my level fill, fit=(left just \i.north west) (right just \i.south east)] {};}
        \end{scope}
      \end{tikzpicture}
    }
  }
  [Parent, left just=level 1 descriptor
    [one, left just=level 2 descriptor
        [one1, left just=level 3 descriptor
             [one11, left just=level 4 descriptor
                    [one111, left just=level 5 descriptor]
                    [one112]
                    [one113]
              ]
              [one12]
              [one13]
        ]
        [one2
          [one21]
        ]
    ]
    [two
        [two1
          [two11]
        ]
      [two2
         [two21
            [two211]
         ]
         [two22
          [two221
            [two2211]
          ]
          [two222
            [two321, left just=level 6 descriptor]
          ]
        ]
      ]
    ]
    [three
        [three1]
    ]
  ]
\end{justtree}

\end{document}

justtrees level management

share|improve this answer

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.