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 was trying to draw something similar to:

enter image description here

without the border around nodes and where arrows should have a line (with a given width to be visible) connecting the tail and the tip.

My code is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows,calc} 

% knowing the angle starting from two coordinates
% by Altermundus:
% http://tex.stackexchange.com/questions/39293/coordinates-a-b-compute-b-a-and-angle-between-x-and-b-a
\newcommand{\pgfextractangle}[3]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
                              {\pgfpointanchor{#3}{center}}
    \global\let#1\pgfmathresult  
}

% style of the arrow
\tikzset{stylefreccia/.style={single arrow, draw=none,fill=gray!80!black,minimum height=#1,minimum width=0.5cm}}

\tikzset{freccia/.code={
\coordinate (A) at (\tikzparentnode.south);
\coordinate (B) at (\tikzchildnode.north);
\pgfextractangle{\angle}{A}{B} 
\node [stylefreccia=1cm,single arrow head indent=1ex,rotate=\angle] (x) at ($(\tikzparentnode.south)!0.5!(\tikzchildnode.north)$){}; % <= insert the node exactly in between parent and child node with the angle computed before
\draw[gray!20,draw,line width=2pt](x.tail)--(x.tip); % <= draw the line connecting tail and tip
}
}

\begin{document}
\tikzset{edge from parent/.style={freccia}}
\begin{center}
\begin{tikzpicture}[level distance=2cm,
level 1/.style={sibling distance=30mm},
level 2/.style={sibling distance=20mm},
level 3/.style={sibling distance=10mm}
]
\node {root}
child {node {left}
child {node{child}}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
\end{center}
\end{document}

which actually gets the point because the output is:

enter image description here

My question is: why leaves seem to be typeset in bold?

share|improve this question
1  
It works for me (no bold childs) with MikTeX 2.9 and pgf-cvs. –  Ignasi May 25 '12 at 17:29
    
Thanks for the feedback. Now I will check if with different pdf-viewer something changes. However I got this with TeXLive 2011, to be precise. –  Claudio Fiandrino May 25 '12 at 17:37
    
The code compiles fine with pgf 2.1 cvs ! –  Alain Matthes May 25 '12 at 20:51
add comment

2 Answers

up vote 5 down vote accepted

It might be the case that the child nodes are printed twice. I think there's something going on when you assign a code to a style but I can't see it. For some reason the following doesn't exhibit the same behavior. I have tried to make the construction in one go except the gray line. Also added an arrow head for a better join at the tip of the arrows.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows,arrows,calc} 
\tikzset{mycomplicatededge/.style={edge from parent path={
[draw=none]                   %It's a nondrawn path
let \p1=(\tikzparentnode.south), 
    \p2=(\tikzchildnode.north),
        \p3=($(\p2)-(\p1)$),  %point diff
        \n3={atan2(\x3,\y3)}  %angle
        in (\p1) -- (\p2)     %path spec
        node [midway,         %put the node in the middle
        single arrow,
        single arrow head indent=1ex,
        minimum height=1cm,
        rotate=\n3,           %use angle var
        draw=none,
        fill=gray!80!black,
        minimum width=0.5cm](x){};
        \draw[gray!20,
            line width=2pt,
            shorten <=.1\pgflinewidth,
            shorten >=.15\pgflinewidth,
            -triangle 90 cap
            ](x.tail)--(x.tip);
        }
    }
}
\begin{document}
\begin{tikzpicture}[level distance=2cm,
level 1/.style={sibling distance=30mm},
level 2/.style={sibling distance=20mm},
level 3/.style={sibling distance=10mm},
mycomplicatededge
]
\node {root}
child {node {left}
child {node{child}}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
\end{document}

enter image description here

I would consider drawing a genuine (meta)arrow and using it as an edge instead of placing a node on the edges though.

share|improve this answer
    
Thanks, this works perfectly. I think next step is to adopt in my solution edge from parent path instead of code. –  Claudio Fiandrino May 26 '12 at 6:09
    
You only need to change minimum height=1cm to minimum height=#1 and use as mycomplicatededge=1cm. –  percusse May 26 '12 at 10:13
add comment

Why do you want use the tree syntax from TikZ for a very simple tree. You can make it with simple commands or use something like tkz-graph. The problem is different for a more complex structure.

\documentclass[11pt]{scrartcl}
\usepackage{tkz-graph}

\begin{document}
\begin{tikzpicture}
\Huge
\GraphInit[unit=4,vstyle=Empty]
 \tikzset{EdgeStyle/.style = {gray!20,
            line width=12pt,
            shorten <=.1\pgflinewidth,
            shorten >=.15\pgflinewidth,
            -triangle 90 cap,->}} 

\Vertex[L=Root]{root}
\SOWE[L=Left](root){lev l1}
\SOEA[L=Right](root){lev r1}
\SO[L=Child](lev l1){lev 2}
\SOWE[L=Child](lev r1){lev l2}
\SOEA[L=Child](lev r1){lev r2}
 \Edges(root,lev l1,lev 2) \Edges(root,lev r1,lev l2) \Edges(lev r1,lev r2)   
\end{tikzpicture}
\end{document}  

enter image description here

share|improve this answer
    
Thanks for the suggestion! :) However, the real structure to build is a bit more complex than the MWE I reported here. –  Claudio Fiandrino May 26 '12 at 6:04
add comment

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.