Tikzpicture:节点标签右侧的换行符

Tikzpicture:节点标签右侧的换行符

label=right:如何在以下 MWE 中的末端节点标签中引入换行符? 如果我删除该部分并只保留label=' but that centers the label节点上方而不是右侧,则Tikzpicture 将包含换行符。

我怎样才能让标签包含换行符并保持在结束节点的右侧?

\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}

答案1

应用every label/.style={align=left}选项:

在此处输入图片描述

笔记:

  • 也可以使用,但在这种情况下,根据TorbjørnT 的评论,every node/.style={}使用似乎更合适。every label/.style={}

  • 看来,当使用align=选项时,可以不是label=right:

    所以

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

    还可以,但是

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

    不是。

或者,也可以使用来\parbox{}包装内容。


代码: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}

代码:\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}

相关内容