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.

I am currently working on creating a decision tree for a journal publication and need to save space.

As of right now, my decision tree looks like enter image description here

Although this is nice, I would like to "compress" the tree vertically (i.e. bring the nodes closer to each other but vertically).

Does anyone know how to do this using TikZ? I've tinkered with a number of settings in the code below... but cannot seem to get anything to work.

\documentclass[letterpaper]{article}
\usepackage{aaai}
\usepackage{tikz} 
\usepackage{pgfplots} 
\usetikzlibrary{shapes,arrows}

\frenchspacing
\setlength{\pdfpagewidth}{8.5in}
\setlength{\pdfpageheight}{11in}

\tikzstyle{block} = [rectangle, draw, fill=white!20, 
    text width=6em, text centered, rounded corners, minimum height=1em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, text centered, ellipse,fill=gray!15, node distance=0cm, text width=3.3em,
    minimum height=0.0em]
    %begin tree
  \begin{center}
    \begin{tikzpicture}[node distance =0cm, auto]
    % Place nodes
    \small
    \centering
    \node [block] (init) {\scriptsize{history of petty theft?}};
    \node [cloud, left of=init, below of=init, node distance=1.5cm] (end1) {\scriptsize{no violent crime}};
    \node [block, right of=init, below of=init, node distance=1.5cm] (dec1) {\scriptsize{history of weapon use?}};
    \node [cloud, right of=dec1, below of=dec1, node distance=1.5cm] (end2) {\scriptsize{no violent crime}};
    \node [block, left of=dec1, below of=dec1, node distance=1.5cm] (dec2) {\scriptsize{history of employment?}};
    \node [cloud, left of=dec1, below of=dec2, node distance=1.5cm] (end3) {\scriptsize{no violent crime}};
    \node [cloud, right of=dec1, below of=dec2, node distance=1.5cm] (end4) {\scriptsize{violent crime}};
    % Draw edges
    \path [line] (init) -| node [near start, above] {\scriptsize{yes}} (end1);
    \path [line] (init) -| node [near start] {\scriptsize{no}} (dec1);
    \path [line] (dec1) -| node [near start, above] {\scriptsize{yes}} (dec2);
    \path [line] (dec1) -| node [near start] {\scriptsize{no}} (end2);
    \path [line] (dec2) -| node [near start, above] {\scriptsize{yes}} (end3);
    \path [line] (dec2) -| node [near start] {\scriptsize{no}} (end4);
\end{tikzpicture}
\end{center}
share|improve this question
1  
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz as there are numerous libraries. –  Peter Grill Apr 5 '13 at 5:55
    
Thanks! I think this is done now. –  Berk U. Apr 5 '13 at 6:17
1  
Still not a complete document. A complete document starts with \documentclass, has \usepacakges for the appropriate packages, followed by \begin{document}, the main code and ending with \end{document}. Also what is \usepackage{aaai}? Please ensure that what you post will compile as a standalone document. –  Peter Grill Apr 5 '13 at 6:26

1 Answer 1

up vote 5 down vote accepted

Why not use a tree? The vertical distance can be altered used the level distance key. Note, that I have changed the text widths to cm as the font is now set using the font key (there appears to be a problem with the alignment if ex or em are used as units in the text width key in this example; not sure why).

As always, I've done a bit of extra work in the options before the picture, so the drawing code is less cluttered.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}


\tikzset{
    every node/.style={
        font=\scriptsize
    },
    decision/.style={
        shape=rectangle,
        minimum height=1cm,
        text width=2cm,
        text centered,
        rounded corners=1ex,
        draw,
        label={[yshift=0.125cm]left:yes},
        label={[yshift=0.125cm]right:no},
    },
    outcome/.style={
        shape=ellipse,
        fill=gray!15,
        draw,
        text width=1.5cm,
        text centered
    },
    decision tree/.style={
        edge from parent path={[-latex] (\tikzparentnode) -| (\tikzchildnode)},
        sibling distance=3cm,
        level distance=1.125cm
    }
}


\begin{tikzpicture}

\node [decision] { history of petty theft? }
    [decision tree]
    child { node [outcome] { no violent crime } }
    child { node [decision] { history of weapon use} 
        child { node [decision] { history of employment? } 
            child { node [outcome] { no violent crime } }
            child { node [outcome] { violent crime } }
        }
        child { node [outcome] { no violent crime } }
    };

\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Thank you so much! –  Berk U. Apr 5 '13 at 16:42

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.