带有一些虚线边的简单有向图

带有一些虚线边的简单有向图

我的代码实现了我想要的效果,只是一些节点轮廓在应该是实线的地方变成了虚线。我意识到我可能把虚线放错了位置,但我只是尝试了一下。

\tikzstyle{every node}=[circle, draw, fill=black!50, inner sep=0pt, minimum width=4pt]
\begin{tikzpicture}[thick,scale=0.8,->,shorten >=2pt]
    \draw (0,0) node {} -- (1,1) node {};
    \draw (1,1) node {} -- (2,1) node {};
    \draw (2,1) node {} -- (3,2) node {};
    \draw (3,2) node {} -- (4,1) node {};

    \draw (0,2) node {} -- (1,1) [dashed] node {};
    \draw (1,2) node {} -- (2,1) [dashed] node {};
    \draw (2,1) node {} -- (3,2) node {};
    \draw (3,2) node {} -- (4,1) node {};

    \draw (2,1) node {} -- (3,0) [dashed] node {};
    \draw (2,1) node {} -- (2,0) [dashed] node {};
    \draw (3,0) node {} -- (4,0) [dashed] node {};
    \draw (3,0) node {} -- (4,-1) [dashed] node {};
    \draw (1,1) node {} -- (1,0) [dashed] node {};
\end{tikzpicture}

答案1

dashed在正确的位置,它适用于整个draw命令。如果将它直接放在后面,语义上可能更有意义\draw,但它对输出没有影响。节点是虚线的,因为它们是作为虚线\draw命令的一部分构建的。要使节点边框不间断,您可以添加solid到您的every node样式中。

这是您的改编代码。请注意,我使用了当前语法\tikzset{<style>/.style={<options>}},它取代了\tikzstyle语法。

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikzset{
    every node/.style={
        circle,
        draw,
        solid,
        fill=black!50,
        inner sep=0pt,
        minimum width=4pt
    }
}
\begin{tikzpicture}[thick,scale=0.8,->,shorten >=2pt]
    \draw (0,0) node {} -- (1,1) node {};
    \draw (1,1) node {} -- (2,1) node {};
    \draw (2,1) node {} -- (3,2) node {};
    \draw (3,2) node {} -- (4,1) node {};

    \draw  (0,2) node {} -- (1,1) node {};
    \draw (1,2) node {} -- (2,1) [dashed] node {};
    \draw (2,1) node {} -- (3,2) node {};
    \draw (3,2) node {} -- (4,1) node {};

    \draw (2,1) node {} -- (3,0) [dashed] node {};
    \draw (2,1) node {} -- (2,0) [dashed] node {};
    \draw (3,0) node {} -- (4,0) [dashed] node {};
    \draw (3,0) node {} -- (4,-1) [dashed] node {};
    \draw (1,1) node {} -- (1,0) [dashed] node {};
\end{tikzpicture}

\end{document}

答案2

Jake 的回答是正确的,但绘制此图的方式并不好。首先,因为我们可以避免将样式应用于某些组的代码,而且我不喜欢(重新)绘制某些节点。无需命名节点即可获取图表,但我们需要使用shortenfor<>

1)我们绘制节点 2)我们绘制实线边 3)我们绘制虚线边

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikzset{
    every node/.style={
        circle,
        draw,
        %solid, no necessary now because the nodes are drawn first
        fill          = black!50,
        inner sep     = 0pt,
        minimum width =4 pt
    }   
}  

\begin{tikzpicture}[thick,scale=0.8,->,
                   shorten >=2pt+0.5*\pgflinewidth,
                   shorten <=2pt+0.5*\pgflinewidth,
                   every node/.style={circle,
                                      draw,
                                      fill          = black!50,
                                      inner sep     = 0pt,
                                      minimum width =4 pt}]
\path[draw] % we place the nodes once
       node at (0,0) {}  
       node at (1,1) {} 
       node at (2,1) {} 
       node at (3,2) {} 
       node at (4,1) {} 
       node at (3,0) {} 
       node at (2,0) {} 
       node at (4,0) {} 
       node at (4,-1){}
       node at (0,2) {} 
       node at (1,0) {} 
       node at (1,1) {} 
       node at (1,2) {} ; 

    \draw (0,0) -- (1,1) ; % it's possible because we use shorten at each side
    \draw (1,1) -- (2,1) ;
    \draw (2,1) -- (3,2) ;
    \draw (3,2) -- (4,1) ; 
    \draw (0,2) -- (1,1) ;

\begin{scope}   [dashed]  % now dashed is for the lines inside the scope
     \draw (1,2) -- (2,1)  ; 
     \draw (2,1) -- (3,0)  ;
     \draw (2,1) -- (2,0)  ;
     \draw (3,0) -- (4,0)  ;
     \draw (3,0) -- (4,-1) ;
     \draw (1,1) -- (1,0)  ; 
\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容