我通常需要绘制具有多条边的图形。如果有人能告诉我如何编写多条边的命令,我将不胜感激,这样我就可以只写一行,而不是重复写多行。假设我的边命令的名称意味着\myedge[m]
我将在两个节点之间绘制 m 条边。
每当我需要绘制多重边时,我都希望使用类似以下的方法(以下多重性为 5)
\draw (a) \myedge[5] (b);
这是我的实际代码示例...在必须不断绘制边的图表中继续使用它会很麻烦。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) -- (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=5] (b);
\draw[thick] (a) edge[bend right=5] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left] (b);
\draw[thick] (a) edge (b);
\draw[thick] (a) edge[bend right] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=15] (b);
\draw[thick] (a) edge[bend left=5] (b);
\draw[thick] (a) edge[bend right=5] (b);
\draw[thick] (a) edge[bend right=15] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=16] (b);
\draw[thick] (a) edge[bend left=8] (b);
\draw[thick] (a) edge[bend right=8] (b);
\draw[thick] (a) edge[bend right=16] (b);
\draw[thick] (a) -- (b);
\end{tikzpicture}
\end{center}
\end{document}
输出:
情况:这正是我想在这个新定义中做的事情:假设我想垂直于连接两个节点(u)
和的线弯曲,(v)
其中坐标(a,b)
和(c,d)
。这使我有机会使用((c-a)*0.2*\i,(d-b)*0.2*\i)
而不是$(0,0.2*\i)$
,这是最通用的。但是,我可以看到这里更大的问题是,在定义时edge[me=<number>]
你实际上没有考虑边缘的端点。我们可以这样做吗?
当边缘不在水平线上时,这肯定会防止卷曲边缘(实际上,当边缘垂直时,情况最糟糕)。另一方面,根据 的大小r=\sqrt{(c-a)^2+(d-b)^2}
,我们可能需要用更小/更大的数字替换。也许用0.2
替换 也是一个好主意。0.2
\frac{0.2}{r}
答案1
更新
现在好多了。如果 A 和 B 是顶点,M 是 AB 的中点,则需要确定两个点 P 和 Q,例如与 AM 正交的 PM 和与 AM 正交的 QM。然后,您就可以绘制从 A 到 B 的曲线,并确定一个好的控制点。
如果要在任意位置的顶点之间绘制多条边,则需要使用curveto
如下代码中的命令
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{me/.style={to path={
\pgfextra{%
\pgfmathsetmacro{\startf}{-(#1-1)/2}
\pgfmathsetmacro{\endf}{-\startf}
\pgfmathsetmacro{\stepf}{\startf+1}}
\ifnum 1=#1 -- (\tikztotarget) \else
let \p{mid}=($(\tikztostart)!0.5!(\tikztotarget)$)
in
\foreach \i in {\startf,\stepf,...,\endf}
{%
(\tikztostart) .. controls ($ (\p{mid})!\i*6pt!90:(\tikztotarget) $) .. (\tikztotarget)
}
\fi
\tikztonodes
}}}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (1,1) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (5,3) {};
\draw[thick] (a) edge[me=5] (b);
\end{tikzpicture}
\end{document}
第一个答案
我定义了一个新选项me
,但你可以更改名称。你只需要写[me=<number> ]
可以使用参数来修改 中的 0.2 0.2*\i
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{me/.style={to path={
\pgfextra{%
\pgfmathsetmacro{\startf}{-(#1-1)/2}
\pgfmathsetmacro{\endf}{-\startf}
\pgfmathsetmacro{\stepf}{\startf+1}}
\ifnum 1=#1 -- (\tikztotarget) \else
\foreach \i in {\startf,\stepf,...,\endf}
{%
(\tikztostart) parabola[bend pos=0.5] bend +(0,0.2*\i) (\tikztotarget)
}
\fi
\tikztonodes
}}}
\begin{center}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=1] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=2] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=3] (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=5] (b);
\end{tikzpicture}
\end{center}
\end{document}