[Tex/LaTex] How to make a connection in a tree with a dashed line

tikz-pgftikz-trees

please refer to the answer of how to draw a process flow figure?

Now I want to change the line of "and->path1" to the dashed line, i added [style=dashed], unfortunately it doesn't change, why?

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\tikzset{level 1/.style={level distance=1.5cm, sibling distance=3.5cm}}
\tikzset{level 2/.style={level distance=1.5cm, sibling distance=2cm}}

\tikzset{bag/.style={text width=20em, text centered,yshift=-0.2cm}}

\begin{document}
\begin{tikzpicture}[grow=down, -stealth]
\node[bag]{$(B)$} 
    child{ edge from parent node[right]{t}; \node[bag]{$(Rgood)$}
            child{ edge from parent node[right]{and}; \node[bag]{$(and)$}
                    child[missing]
                    child{ edge from parent node[right=0.1cm][style =dashed]{it}; \node[bag]{$(path1)$}}
                    child{ edge from parent node[right=0.1cm]{else}; \node[bag]{$(path2)$}}
            }
    };
\end{tikzpicture}

\end{document}

Best Answer

It is possible to locally define a style introducing the option near the keyword child.

The example, therefore, should become:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\tikzset{level 1/.style={level distance=1.5cm, sibling distance=3.5cm}}
\tikzset{level 2/.style={level distance=1.5cm, sibling distance=2cm}}

\tikzset{bag/.style={text width=20em, text centered,yshift=-0.2cm}}

\begin{document}
\begin{tikzpicture}[grow=down, -stealth]
\node[bag]{$(B)$} 
  child{ edge from parent node[right]{t}; \node[bag]{$(Rgood)$}
    child{ edge from parent node[right]{and}; \node[bag]{$(and)$}
       child[missing]
       child[dashed]{ edge from parent node[right=0.1cm]{it}; \node[bag]{$(path1)$}}  % <= NOTICE the dashed near child
       child{ edge from parent node[right=0.1cm]{else}; \node[bag]{$(path2)$}}
    }
  };
\end{tikzpicture}
\end{document}

The result is:

enter image description here


For completeness, let see what happens playing with the position of options.

In the previous example, assume each time to just change the line:

child[dashed]{ edge from parent node[right=0.1cm]{it}; \node[bag]{$(path1)$}}

1. Insert a color definition inside the node option.

child[dashed]{ edge from parent node[right=0.1cm,red]{it}; \node[bag]{$(path1)$}}

The result is:

enter image description here

2. Let's now shift the color definition by inserting it as option of edge from parent.

child[dashed]{ edge from parent[red] node[right=0.1cm]{it}; \node[bag]{$(path1)$}}

This gives:

enter image description here

Although it seems the identical previous result, actually the connection is first drawn in red, then the style is override by the child definition in black. The label still remains in black.

3. Let's now shift the color definition by inserting it as option of child.

child[dashed,red]{ edge from parent node[right=0.1cm]{it}; \node[bag]{$(path1)$}}

This gives:

enter image description here

Once the option is put in this position, it holds for all elements.

EDIT

Please, next time insert your real need in the question rather than ask it later on in the comments. Indeed, I developed the previous code to answer:

Now I want to change the line of "and->path1" to the dashed line, i added [style=dashed], unfortunately it doesn't change, why?

not:

so is there any way to make the line bettween "(Rgood)->(and)" dashed while the line between "(and)->path1" remains the same, that is to say, not dashed´╝č

A workaround for that is:

\documentclass[a4paper,12pt]{article}
\usepackage[width=15cm]{geometry}
\usepackage{pgfplots}
\usetikzlibrary{trees}

\tikzset{level 1/.style={level distance=1.5cm, sibling distance=3.5cm}}
\tikzset{level 2/.style={level distance=1.5cm, sibling distance=2cm}}

\tikzset{bag/.style={text width=20em, text centered,yshift=-0.2cm}}

\begin{document}
\scalebox{1.5}{
\begin{tikzpicture}[grow=down, -stealth,dashed]
\node[bag]{$(B)$} 
    child{ edge from parent[solid] node[right]{t}; \node[bag]{$(Rgood)$}
            child{ edge from parent node[right]{and}; \node[bag]{$(and)$}
                    child[missing]
                    child{ edge from parent[solid] node[right=0.1cm]{it}; \node[bag]{$(path1)$}}
                    child{ edge from parent[solid] node[right=0.1cm]{else}; \node[bag]{$(path2)$}}
            }
    };
\end{tikzpicture}
}

\end{document}