如何在 TiKZ 中“勾勒”路径?

如何在 TiKZ 中“勾勒”路径?

我在 TiKZ 中画了一幅图(比如说一张图),我想突出显示图中的两条路径。有没有办法使用我已经定义的节点名称来实现这一点,而无需明确创建新的坐标?

我几乎可以通过让我的额外路径穿过节点锚点来到达那里,但我想将路径偏移到离节点比锚点更远的位置。

(我在下面附上了一个最小的例子。理想情况下,我希望红色和蓝色路径都位于三角形的外部。)

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}    
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners] (4.north) -- (0.north) -- (1.north east) --(1.west) -- (2.west); 
\draw[blue, rounded corners] (1.south east) -- (2.south east) -- (0.south) --(4.south); 
\end{tikzpicture}
\end{document}

mwe 图像

答案1

如果将两者都放在外面,其中一个会部分覆盖另一个,不是吗?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
\draw[blue, rounded corners=1pt] (1.west) -- (2.west) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);
\end{tikzpicture}
\end{document}

三角形

因此,您可以使用该positioning库来微调间距,并避免第二条路径覆盖第一条路径:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through, positioning}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}[node distance=.1mm]
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
\node (a) [left=of 1.west] {};
\node (b) [left=of 2.west] {};
\path [draw, blue, rounded corners=1pt] (a.east) -- (b.east) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);
\end{tikzpicture}
\end{document}

不同的路径

或者,设置node distance=.25mm

更多独特的路径

要仅在局部更改节点距离,使其不影响复杂图中其他节点的位置,可以限制设置的范围。例如,在此图中,我限制了设置,并通过在设置范围结束后node distance=.25mm放置另一个节点来说明这一点:z

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through, positioning}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
{\tikzset{node distance=.25mm}
\node (a) [left=of 1.west] {};
\node (b) [left=of 2.west] {};
\path [draw, blue, rounded corners=1pt] (a.east) -- (b.east) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);}
\node (c) [left=of 1.west] {z};
\end{tikzpicture}
\end{document}

z 向左,采用节点距离的标准设置

请注意

\node (a) [left=of 1.west] {};

\node (c) [left=of 1.west] {z};

就位置而言,前者在范围内,\tikzset{node distance=.25mm}而后者不在范围内,因此使用默认设置。

相关内容