4

How can I introduce a line break into the labels of the end nodes in the following MWE? The Tikzpicture will include the line breaks if I remove the label=right: part and just leave label=' but that centers the labelabove' the node, rather than to the right.

How can I get the label to include the line breaks and also stay to the right of the end node?

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels. 
\begin{tikzpicture}[grow=right, sloped]
\hspace{-60pt}
\node(A)[bag] {$S(3)$ $(Bet~3)$}
        child {
                node[bag] {$S(4)$ $Bet~4$}        
                        child {node[end, label=right: {Some text \\ Some text \\ Some text }] {}
                                edge from parent node[below] {$Quit$} node[above]  {}
                        }
                        child {
                                node[end, label=right: {Some text \\ Some text \\ Some text }] {}
                                edge from parent node[above] {$Bid$} node[below]  {}
                        }
                        edge from parent node[below] {$LLL$} node[above]  {}
        }
        child {
                node[bag] {$S(4)$ $Bet~4$}       
                child {
                                node[end, label=right: {Some text \\ Some text \\ Some text }] {}
                                edge from parent node[below] {$Quit$} node[above]  {}
                        }
                        child {
                                node[end, label=right:{Some text \\ Some text \\ Some text }] {}
                                edge from parent node[above] {$Bid$} node[below]  {}
                        }
                edge from parent node[above] {$WWW$} node[below]  {}
        };
\begin{scope}[]



\end{scope}
\end{tikzpicture}

\vspace{60pt}



\end{document}
user2146441
  • 1,226

1 Answers1

3

Apply the every label/.style={align=left} option:

enter image description here

Note:

  • One can also use every node/.style={}, but in this case it seems more appropriate to use every label/.style={} as per TorbjørnT's comment.

  • It appears that when using the align= option one can not leave a blank space after the label=right:

    So

      label=right:{Some text \\ Some text \\ Some text}
    

    is ok, but

      label=right: {Some text \\ Some text \\ Some text}
    

    is not.

Alternatively, one could also use a \parbox{} to wrap the content in.


Code: every label/.style={align=left}:

\documentclass{article}

\usepackage[latin1]{inputenc} \usepackage{tikz} \usetikzlibrary{trees} \begin{document} \pagestyle{empty}

% Set the overall layout of the tree \tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm] \tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs \tikzstyle{bag} = [text width=4em, text centered] \tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

% The sloped option gives rotated edge labels. Personally % I find sloped labels a bit difficult to read. Remove the sloped options % to get horizontal labels. \begin{tikzpicture}[grow=right, sloped, every label/.style={align=left}] \hspace{-60pt} \node(A)[bag] {$S(3)$ $(Bet~3)$} child { node[bag] {$S(4)$ $Bet~4$}
child {node[end, align=center, label=right:{Some text \ Some text \ Some text}] {} edge from parent node[below] {$Quit$} node[above] {} } child { node[end, label=right:{Some text \ Some text \ Some text}] {} edge from parent node[above] {$Bid$} node[below] {} } edge from parent node[below] {$LLL$} node[above] {} } child { node[bag] {$S(4)$ $Bet~4$}
child { node[end, label=right:{Some text \ Some text \ Some text}] {} edge from parent node[below] {$Quit$} node[above] {} } child { node[end, label=right:{Some text \ Some text \ Some text}] {} edge from parent node[above] {$Bid$} node[below] {} } edge from parent node[above] {$WWW$} node[below] {} }; \end{tikzpicture} \end{document}


Code: \parbox

\documentclass{article}

\usepackage[latin1]{inputenc} \usepackage{tikz} \usetikzlibrary{trees} \begin{document} \pagestyle{empty}

% Set the overall layout of the tree \tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm] \tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs \tikzstyle{bag} = [text width=4em, text centered] \tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

% The sloped option gives rotated edge labels. Personally % I find sloped labels a bit difficult to read. Remove the sloped options % to get horizontal labels. \begin{tikzpicture}[grow=right, sloped] \hspace{-60pt} \node(A)[bag] {$S(3)$ $(Bet~3)$} child { node[bag] {$S(4)$ $Bet~4$}
child {node[end, label=right: {\parbox[c]{3.0cm}{Some text \ Some text \ Some text}}] {} edge from parent node[below] {$Quit$} node[above] {} } child { node[end, label=right: {\parbox[c]{3.0cm}{Some text \ Some text \ Some text}}] {} edge from parent node[above] {$Bid$} node[below] {} } edge from parent node[below] {$LLL$} node[above] {} } child { node[bag] {$S(4)$ $Bet~4$}
child { node[end, label=right: {\parbox[c]{3.0cm}{Some text \ Some text \ Some text}}] {} edge from parent node[below] {$Quit$} node[above] {} } child { node[end, label=right:{\parbox[c]{3.0cm}{Some text \ Some text \ Some text}}] {} edge from parent node[above] {$Bid$} node[below] {} } edge from parent node[above] {$WWW$} node[below] {} }; \end{tikzpicture} \end{document}

Peter Grill
  • 223,288