我试图绘制类似的东西:
节点周围没有边框,并且箭头应该有一条线(具有给定的宽度才可见)连接尾部和尖端。
我的代码是:
\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}
这实际上得到了要点,因为输出是:
我的问题是:为什么叶子似乎是用粗体排版的?
答案1
code
子节点可能会被打印两次。我认为将 a 分配给 a时会发生一些事情,style
但我看不到。出于某种原因,以下内容没有表现出相同的行为。除了灰线外,我尝试一次性完成构造。还添加了箭头,以便在箭头尖端更好地连接。
\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}
我会考虑绘制一个真正的(元)箭头并将其用作边,而不是在边缘上放置一个节点。
答案2
为什么您要使用 TikZ 的树语法来制作非常简单的树。您可以使用简单的命令或使用类似 tkz-graph 的工具来制作它。对于更复杂的结构,问题会有所不同。
\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}