![tikz-graph 中没有标签或绘图的节点](https://linux22.com/image/397503/tikz-graph%20%E4%B8%AD%E6%B2%A1%E6%9C%89%E6%A0%87%E7%AD%BE%E6%88%96%E7%BB%98%E5%9B%BE%E7%9A%84%E8%8A%82%E7%82%B9.png)
我需要在我的图表(不是图形)中画出一些“无限”或“不断延续”的概念。我经常用它tkz-graph
来绘制图形,但我还没有找到一种方法来添加没有“圆圈”的节点。作为一种解决方法,我使用了常规的\node
,tikz
但为了简单起见,我正在寻找一种更“集成”的解决方案,只使用一个 tikz“库”。以下 MWE 显示了我的解决方法
\documentclass{article}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
\GraphInit[vstyle=Classic]
\SetGraphUnit{1.5}
\tikzset{VertexStyle/.style = {shape = circle,fill = black,minimum size = 7,inner sep=0}}
\SetUpEdge[lw=1.0pt]
\draw[dashed,red,thick] (2,0) -- (3,0); % u_0
\draw[dashed,blue,thick] (1,1) -- (2,2); % u_1
\draw[dashed,brown,thick] (0,1) -- (0,3); % u_2
\draw[dashed,purple,thick] (-1,1) -- (-2,2); % u_3
\draw[dashed,green,thick] (-1.5,0) -- (-2.5,0); % u_{i-1}
\draw[dashed,thick,cyan] (-1,-1) -- (-2,-2); % u_i
\draw[dotted,thick] (-1,.5) -- (-1.1,.3); % ...
\Vertex[Lpos=270,L=$v$]{v}
\EA[Lpos=270,L=$u_0$,unit=2](v){u0}
\Edge[color=black!50](v)(u0)
\NOEA[L=$u_1$,unit=1.14](v){u1}
\Edge[color=red](v)(u1)
\NO[L=$u_2$](v){u2}
\Edge[color=blue](v)(u2)
\NOWE[L=$u_3$,unit=1.14](v){u3}
\Edge[color=brown](v)(u3)
\WE[Lpos=270,L=$u_{i-1}$](v){uim}
\Edge[color=purple](v)(uim)
\SOWE[Lpos=180,L=$u_i$,unit=1.14](v){ui}
\Edge[color=green](v)(ui)
\end{tikzpicture}
\end{document}
有没有办法不使用它\draw
,而是创建“幽灵”顶点并放在\Edge
它们之间。
答案1
您可以将tkz-graph
节点属性设置为节点不可见,即最小尺寸为零且线宽为零。这会以某种方式禁用事后设置边缘颜色的功能,可以通过\SetUpEdge
或 修复颜色来恢复\tikzstyle{EdgeStyle}
。如果您想要不同颜色的虚线边缘,则会变得有点复杂,因为\tikzstyle
禁用了 中的颜色设置\Edge[color=some color]
,可以通过 恢复\SetUpEdge
,但这反过来会禁用\tikzstyle{dashed}
。因此,要么您确定一种颜色并在 中设置它\tikzstyle
,要么不要使用虚线,要么将两者组合在一个新命令中,如下面的 MWE 中所示。
\documentclass{article}
\usepackage{tkz-graph}
\usetikzlibrary{calc}
% new macro to set both color and dashed style for individual edges
\def\DashedEdge[#1](#2)(#3){%
\tikzstyle{EdgeStyle}=[dashed,#1]%
\Edge(#2)(#3)%
}
\begin{document}
\begin{tikzpicture}
\GraphInit[vstyle=Classic]
\SetGraphUnit{1.5}
\tikzset{VertexStyle/.style = {shape = circle,fill = black,minimum size = 7,inner sep=0}}
\SetUpEdge[lw=1.0pt]
\Vertex[Lpos=270,L=$v$]{v}
\EA[Lpos=270,L=$u_0$,unit=2](v){u0}
\Edge[color=black!50](v)(u0)
\NOEA[L=$u_1$,unit=1.14](v){u1}
\Edge[color=red](v)(u1)
\NO[L=$u_2$](v){u2}
\Edge[color=blue](v)(u2)
\NOWE[L=$u_3$,unit=1.14](v){u3}
\Edge[color=brown](v)(u3)
\WE[Lpos=270,L=$u_{i-1}$](v){uim}
\Edge[color=purple](v)(uim)
\SOWE[Lpos=180,L=$u_i$,unit=1.14](v){ui}
\Edge[color=green](v)(ui)
\SetVertexSimple[MinSize=0pt,LineWidth=0pt,LineColor=white,FillColor=white]
\SetGraphUnit{1}
%\tikzstyle{EdgeStyle}=[dashed,color=green]% \Edge[color=X] will not work
%\SetUpEdge[color=black] % \Edge[color=X] will work again but not dashed
\SOWE(ui){uigh}
\DashedEdge[color=cyan](ui)(uigh)
\WE(uim){uimgh}
\DashedEdge[color=green](uim)(uimgh)
\NOWE(u3){u3gh}
\DashedEdge[color=purple](u3)(u3gh)
\NO(u2){u2gh}
\DashedEdge[color=brown](u2)(u2gh)
\NOEA(u1){u1gh}
\DashedEdge[color=blue](u1)(u1gh)
\EA(u0){u0gh}
\DashedEdge[color=red](u0)(u0gh)
\draw[dotted,thick] ($(uim)+(0.4,0.3)$) -- ($(u3)+(0.2,-0.4)$);
\end{tikzpicture}
\end{document}
结果:
注意,该calc
库用于根据 u i-1和 u 3节点定位小黑虚线,水平和垂直方向上有小的偏移。
\node
但是,这也可以使用常规和命令来完成,\draw
使用节点名称作为起点和终点\draw
。优点是更容易绘制空节点,并且语法通常不会影响设置tkz-graph
。相对于其他节点的定位可以使用positioning
库完成。
代码:
\documentclass{article}
\usepackage{tkz-graph}
\usetikzlibrary{calc,positioning}
\begin{document}
\begin{tikzpicture}
\GraphInit[vstyle=Classic]
\SetGraphUnit{1.5}
\tikzset{VertexStyle/.style = {shape = circle,fill = black,minimum size = 7,inner sep=0}}
\SetUpEdge[lw=1.0pt]
\Vertex[Lpos=270,L=$v$]{v}
\EA[Lpos=270,L=$u_0$,unit=2](v){u0}
\Edge[color=black!50](v)(u0)
\NOEA[L=$u_1$,unit=1.14](v){u1}
\Edge[color=red](v)(u1)
\NO[L=$u_2$](v){u2}
\Edge[color=blue](v)(u2)
\NOWE[L=$u_3$,unit=1.14](v){u3}
\Edge[color=brown](v)(u3)
\WE[Lpos=270,L=$u_{i-1}$](v){uim}
\Edge[color=purple](v)(uim)
\SOWE[Lpos=180,L=$u_i$,unit=1.14](v){ui}
\Edge[color=green](v)(ui)
\node[right=of u0] (u0ghost) {};
\node[above right=of u1] (u1ghost) {};
\node[above=of u2] (u2ghost) {};
\node[above left=of u3] (u3ghost) {};
\node[left=of uim] (uimghost) {};
\node[below left=of ui] (uighost) {};
\draw[dashed,red,thick] (u0) -- (u0ghost); % u_0
\draw[dashed,blue,thick] (u1) -- (u1ghost); % u_1
\draw[dashed,brown,thick] (u2) -- (u2ghost); % u_2
\draw[dashed,purple,thick] (u3) -- (u3ghost); % u_3
\draw[dashed,green,thick] (uim) -- (uimghost); % u_{i-1}
\draw[dashed,thick,cyan] (ui) -- (uighost); % u_i
\draw[dotted,thick] ($(uim)+(0.4,0.3)$) -- ($(u3)+(0.2,-0.4)$);
\end{tikzpicture}
\end{document}