代码片段来自 Zarko 的代码问题。我还在寻找带标签的箭头,但标签恰好出现在箭头上,有没有办法让它们位于圆圈外面?
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
backgrounds,
fit,
matrix,
positioning}
\usepackage{xcolor}
\begin{document}
\tikzset{block2/.style={rectangle, draw=gray,rounded corners, line width=.9mm,text width=26mm, fill=gray!50,text centered,
minimum height=4em}, line/.style={draw, -latex'}}
\tikzset{level 1/.style={level distance=20mm, sibling distance=30mm},
level 2/.style={level distance=20mm, rounded corners, sibling distance=15mm},
level 3/.style={level distance=20mm, rounded corners,},
myarrow/.style=
{draw=gray,line width=3pt,-stealth,rounded corners
},}
\begin{tikzpicture}[
node distance = 4ex and 0em,
every edge/.style = {draw=gray,line width=3pt , -stealth,rounded corners, bend angle=20},
]
\node [block2] (1) {A};
\node [block2, below right= of 1] (2) {B};
\node [block2, below left= of 2] (3) {C};
\node [block2, above left= of 3] (4) {D};
\path (1.east) edge [bend left] node {some label} (2);
\path (2) edge [bend left] node {some more label } (3.east);
\path (3.west) edge [bend left] node {some even more label} (4);
\path (4) edge [bend left] (1.west);
\end{tikzpicture}
\end{document}
答案1
对于标签使用quotes
库,在其样式定义中添加选项auto
:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
backgrounds,
fit,
matrix,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 5ex and 0em,
block/.style = {draw=gray, rounded corners, line width=.9mm,
fill=gray!30, outer sep=0pt,
minimum height=4em, text width=22mm, align=center},
every edge/.style = {-Stealth, draw=gray, line width=3pt, bend left=24},
every edge quotes/.style= {auto, font=\small, align=center}
]
\node [block] (1) {A};
\node [block, below right= of 1] (2) {B};
\node [block, below left= of 2] (3) {C};
\node [block, above left= of 3] (4) {D};
\path (1.east) edge ["some label"] (2)
(2) edge ["some more label"] (3.east)
(3.west) edge ["some even\\ more label"] (4)
(4) edge (1.west);
\end{tikzpicture}
\end{document}
答案2
只需向节点添加锚点(以及向节点添加一个小的内部分离)。
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
backgrounds,
fit,
matrix,
positioning}
\usepackage{xcolor}
\begin{document}
\tikzset{block2/.style={rectangle, draw=gray,rounded corners, line width=.9mm,text width=26mm, fill=gray!50,text centered,
minimum height=4em}, line/.style={draw, -latex'}}
\tikzset{level 1/.style={level distance=20mm, sibling distance=30mm},
level 2/.style={level distance=20mm, rounded corners, sibling distance=15mm},
level 3/.style={level distance=20mm, rounded corners,},
myarrow/.style=
{draw=gray,line width=3pt,-stealth,rounded corners
},
lbl/.style={inner sep=1pt}}
\begin{tikzpicture}[
node distance = 4ex and 0em,
every edge/.style = {draw=gray,line width=3pt , -stealth,rounded corners, bend angle=20},
]
\node [block2] (1) {A};
\node [block2, below right= of 1] (2) {B};
\node [block2, below left= of 2] (3) {C};
\node [block2, above left= of 3] (4) {D};
\path (1.east) edge [bend left] node[lbl,above right] {some label} (2);
\path (2) edge [bend left] node[lbl,below right] {some more label } (3.east);
\path (3.west) edge [bend left] node[lbl,below left] {some even more label} (4);
\path (4) edge [bend left] (1.west);
\end{tikzpicture}
\end{document}