我有以下图表,其标签是在 for 循环中生成的:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
\tikzstyle{vertex}=[circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt]
\graph[circular placement, radius=4cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {
\foreach \x [evaluate=\x as \sx using int(\x+1)] in {0,...,4} {
\foreach \y in {0,...,4} {
\x -- \y;
\x --["\ifthenelse{\sx<\y}{$\mathsf{alph}(w_{\sx} \cdots w_{\y})$}
{\ifthenelse{\sx=\y}{$\mathsf{alph}(w_{\sx})$}{}}", sloped] \y;
};
};
};
\end{tikzpicture}
\end{document}
结果是,标签
- 跨越图形边缘
- 与其他标签重叠
我发现的唯一一件事是,自动防撞功能似乎不可用:标签自动定位,以便不与任何内容重叠。
无论如何,我的示例图并不罕见,我应该能够以某种方式以可接受的方式放置标签。
如果没有通用的方法,那么针对这个例子可能有哪些解决方案?
我可以想到以下定位(包括图形的一些调整大小)
- 将最外边的标签放在外面
- 将其他标签放在五边形中间
但我不知道如何实现这一点。
答案1
也许最好先定位节点,然后再连接它们:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt},
myedge/.style={sloped,font=\scriptsize},
auto
]
% position the nodes
\graph[circular placement, radius=6cm, group polar shift=(360/5:0),nodes={circle,draw,vertex}]{%
\foreach \x in {0,...,4}\x};
% draw the edges
\foreach[evaluate={\sx=int(\x+1)},evaluate={\sxx=(int(\x+2)}] \x in {0,...,4} {
\foreach \y in {\x,...,4} {
\ifthenelse{\sx=\y}{\graph{(\x)--["\makebox[0pt]{$\mathsf{alph}(w_{\sx})$}",',myedge](\y)};}{%
\ifthenelse{\sx<\y}{
\ifthenelse{\sxx=\y}{\tikzset{d/.style={'}}}{\tikzset{d/.style={}}}
\graph{(\x)--["\makebox[0pt]{$\mathsf{alph}(w_{\sx} \cdots w_{\y})$}",d,myedge](\y)};
}{}}}}
\end{tikzpicture}
\end{document}
如果需要用一个\graph
命令完成,请尝试
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt},
myedge/.style={sloped,font=\scriptsize},
]
\graph[circular placement, radius=6cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {
\foreach \x [evaluate={\sx=int(\x+1)},evaluate={\sxx=(int(\x+2)}] in {0,...,4} {
\foreach \y in {0,...,4} {
\x --["\ifthenelse{\sx=\y}{\makebox[0pt]{$\mathsf{alph}(w_{\sx})$}}{
\ifthenelse{\y=\sxx}{\makebox[0pt]{$\mathsf{alph}(w_{\sx} \cdots w_{\y})$}}{}}",',myedge] \y;
\x --["\ifthenelse{\sx<\y \AND \NOT\y=\sxx}{\makebox[0pt]{$\mathsf{alph}(w_{\sx} \cdots w_{\y})$}}{}", myedge] \y;
}
}
};
\end{tikzpicture}
\end{document}