tikz无向图中的多色边

tikz无向图中的多色边

我正在尝试绘制一个无向图,其中的一些边用两种颜色并排着色。我非常喜欢符号 1 建议的解决方案,但我无法让它适用于直边。

任何帮助将不胜感激!

梅威瑟:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
    semithick,
    node distance = 2cm,
    dot/.style={circle,fill,inner sep=2pt}
}
\tikzset{
    side by side/.style 2 args={
    line width=2pt,
    #1,
    postaction={
        clip,postaction={draw,#2}
        }
    }
}
\tikzstyle{every state}=[draw = black,thick,fill = white,minimum size = 4mm]
\tikzstyle{selected edge} = [draw,line width=2pt,-,red!50]

\begin{document}
     \begin{tikzpicture}
              \node[dot] (0) {};
              \node[dot] [right of=0] (1) {};
              \node[dot] [below right of=1, yshift=15] (2) {};
              \node[dot] [below of=0] (3) {};
              \node[dot] [right of=3] (4) {};

              \path (0) edge[side by side={red!50}{blue!50},bend left] (1);
              \path (0) edge[side by side={red!50}{blue!50},bend right] (3);
              \path (1) edge (2);
              \path (1) edge[selected edge] (4);
              \path (1) edge (4);
              \path (3) edge[selected edge] (1);
              \path (3) edge (1); 
              \path (3) edge[side by side={red!50}{blue!50},bend right] (4);
              \path (4) edge[side by side={red!50}{blue!50},bend right] (2);
      \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

按照我的答案在链接的 Q 上,nfold图书馆可以提供帮助。

也就是说,对于直线,可以使用内置工具来实现,只需将起点和终点垂直于线的方向移动线宽的一半,就像 TikZ-CD 一样shift leftshift rightnfold也可以做到。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{nfold}
\makeatletter
\tikzset{
  side by side/.style args={#1:#2}{
    postaction={path only,draw=#1,offset=+.5\pgflinewidth},
    postaction={path only,draw=#2,offset=+-.5\pgflinewidth}},
  side by side'/.style={path only,side by side={#1}},
  offset/.code=
    \tikz@addoption{%
      \pgfgetpath\tikz@temp
      \pgfsetpath\pgfutil@empty
      \pgfoffsetpath\tikz@temp{#1}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
  dot/.style={circle, fill, inner sep=+2pt, outer sep=+0pt},
  thick,
]
\node foreach[count=\i from 0]\p in {(0, 0), (2, 0), (3.5, -1), (0, -2), (2, -2)}
  [dot] (\i) at \p {};
\path
  (1) edge (2)
  [every edge/.append style={side by side=blue!50:red!50}]
      edge (3) edge (4)
  [bend right]
      edge (0)
           (0) edge (3)
                    (3) edge (4)
                             (4) edge (2);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容