10

I've just started using Latex to produce a maths-heavy document. I wanted to include a probability tree and found the Tikz library. The first bit of the code for my tree looks like:

%To create probability trees, use tikz package
\usepackage{tikz}
\usetikzlibrary{trees}

% Insert a probability tree showing first level only
% -------------------------------------------------------------
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=4.0cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text centered]

% Draw probability tree
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {}
    child {
        node[bag] {Not diseased $\left( D^- \right)$}
            edge from parent 
            node[below]  {$0.90$}
    }
    child {
        node[bag] {Diseased $\left( D^+ \right)$}        
            edge from parent         
            node[above] {Prevalence}
            node[below]  {$0.10$}
    };
\end{tikzpicture} \\

The resulting tree looks a bit like this:

             Diseased (D+)
           /
    Prev /
       / 0.10
     /
     \
       \
   0.90  \
           \
             Not diseased (D-)

... if you get my drift.

I would like to be able to enter a line break in the node text so that the (D+) and (D-) appear underneath. I've tried using \\ and \newline but to no avail. Any suggestions?

Thanks in advance.

user1718097
  • 4,090
  • 11
  • 48
  • 63
  • 4
    Change `node[bag]` to `node[bag,align]` and use \\. Duplicate: http://tex.stackexchange.com/questions/123671/manual-automatic-line-breaks-and-text-alignment-in-tikz-nodes and http://tex.stackexchange.com/questions/31096/how-can-i-use-linebreak-inside-a-node-in-tikz. "If you want to manually insert line breaks, you can use \\ and the optional argument align. (If you do not specify an option for align, the line breaking will not happen, and the problem noted by the OP will occur.)" – Ramashalanka Jun 03 '15 at 08:41

2 Answers2

11

Starting from your code,

\documentclass[multi=false,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=4.0cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

\tikzstyle{bag} = [align=center]

\begin{document}

\begin{tikzpicture}[grow=right, sloped]
\node[bag] {}
    child {
        node[bag] {Not diseased\\ $\left( D^- \right)$} %% 1
            edge from parent
            node[below]  {$0.90$}
    }
    child {
        node[bag] {Diseased\\ $\left( D^+ \right)$} %% 2
            edge from parent
            node[above] {Prevalence}
            node[below]  {$0.10$}
    };
\end{tikzpicture}

\end{document}

makes the job: notice that I only changed \tikzstyle{bag} from [text centered] to [align=center] and added \\ in lines marked %% 1 and %% 2.

The resulting tree:

output pdf

MattAllegro
  • 6,455
  • 5
  • 45
  • 52
7

Taken from the Tikz Qtree documentation:

In Qtree, it was allowed to use a line break (\\) inside a node. TikZ nodes by default don’t allow this, but the align option (in PGF/TikZ version 2.1 or later) enables it as a side effect.

Thanks to Alan Munn for figuring this out. Prior to PGF/TikZ version 2.1, the fix was to use the options text width=2em,text centered.

w5l
  • 5,341
  • 1
  • 25
  • 43
  • Good to know. It's a surprise, but `text width` is an ugly manual hack, which is not far from manual positioning alltogether. – Tomasz Gandor May 03 '21 at 10:38