Tikzpicture 边缘图

Tikzpicture 边缘图

我已附上从代码中获取的图像

    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

  \node (S) {};
  \node[main node] (0) [node distance=1.1cm,below of=S] {$s$};
  \node[main node] (3) [node distance=2.2cm,below of=0] {3};
  \node[main node] (2) [node distance=2.2cm,left of=3] {2};
  \node[main node] (1) [node distance=2.2cm,left of=2] {1};
  \node[main node] (4) [node distance=2.2cm,right of=3] {4};
  \node[main node] (5) [node distance=2.2cm,right of=4] {5};
  \node (6) [node distance=1.1cm,below of=1] {};
  \node (7) [node distance=1.1cm,below of=2] {};
  \node (8) [node distance=1.4cm,below left of=3] {};
  \node (9) [node distance=1.1cm,below of=4] {};
  \node (10) [node distance=1.1cm,below of=5] {};
  \node[main node] (T) [node distance=2.75cm,below of=3] {$t$};

  \path[every node/.style={font=\sffamily\small}]
    (S) edge node [right] {} (0)
    (0) edge node [left] {} (1)
        edge node [left] {} (2)
        edge node [right] {} (3)
        edge node [left] {} (4)
        edge node [right] {} (5)
    (1) edge node [above] {} (2)
            edge node [left] {} (6)
            edge node [left] {} (T)
    (2) edge node [above] {} (3)
        edge node [left] {} (7)
        edge node [left] {} (T)
    (3) edge node [above] {} (4)
            edge node [left] {} (8)
            edge node [left] {} (T)
    (4) edge node [above] {} (5)
        edge node [right] {} (9)
        edge node [right] {} (T)
    (5) edge node [right] {} (10)
        edge node [right] {} (T)
    ;
    \end{tikzpicture}

我想知道如何使用 TIKZ 包绘制“红色”边缘。

谢谢。

图形

答案1

有很多种可能性。其中之一是使用

to[out=<angle>,in=<angle>,looseness=<value>]

语法\draw

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

  \node (S) {};
  \node[main node] (0) [node distance=1.1cm,below of=S] {$s$};
  \node[main node] (3) [node distance=2.2cm,below of=0] {3};
  \node[main node] (2) [node distance=2.2cm,left of=3] {2};
  \node[main node] (1) [node distance=2.2cm,left of=2] {1};
  \node[main node] (4) [node distance=2.2cm,right of=3] {4};
  \node[main node] (5) [node distance=2.2cm,right of=4] {5};
  \node (6) [node distance=1.1cm,below of=1] {};
  \node (7) [node distance=1.1cm,below of=2] {};
  \node (8) [node distance=1.4cm,below left of=3] {};
  \node (9) [node distance=1.1cm,below of=4] {};
  \node (10) [node distance=1.1cm,below of=5] {};
  \node[main node] (T) [node distance=2.75cm,below of=3] {$t$};

\draw (T) to[out=180,in=180,looseness=4] (0);
\draw (5) to[out=-20,in=0,looseness=2] (T);

  \path[every node/.style={font=\sffamily\small}]
    (S) edge node [right] {} (0)
    (0) edge node [left] {} (1)
        edge node [left] {} (2)
        edge node [right] {} (3)
        edge node [left] {} (4)
        edge node [right] {} (5)
    (1) edge node [above] {} (2)
            edge node [left] {} (6)
            edge node [left] {} (T)
    (2) edge node [above] {} (3)
        edge node [left] {} (7)
        edge node [left] {} (T)
    (3) edge node [above] {} (4)
            edge node [left] {} (8)
            edge node [left] {} (T)
    (4) edge node [above] {} (5)
        edge node [right] {} (9)
        edge node [right] {} (T)
    (5) edge node [right] {} (10)
        edge node [right] {} (T)
    ;
    \end{tikzpicture}
\end{document}

在此处输入图片描述

另一个使用.. controls ..calc库:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\begin{document}

    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

  \node (S) {};
  \node[main node] (0) [node distance=1.1cm,below of=S] {$s$};
  \node[main node] (3) [node distance=2.2cm,below of=0] {3};
  \node[main node] (2) [node distance=2.2cm,left of=3] {2};
  \node[main node] (1) [node distance=2.2cm,left of=2] {1};
  \node[main node] (4) [node distance=2.2cm,right of=3] {4};
  \node[main node] (5) [node distance=2.2cm,right of=4] {5};
  \node (6) [node distance=1.1cm,below of=1] {};
  \node (7) [node distance=1.1cm,below of=2] {};
  \node (8) [node distance=1.4cm,below left of=3] {};
  \node (9) [node distance=1.1cm,below of=4] {};
  \node (10) [node distance=1.1cm,below of=5] {};
  \node[main node] (T) [node distance=2.75cm,below of=3] {$t$};

\draw let \p1=(T), \p2=(0) in
  (T) .. controls ([xshift=-8cm]\x1,.5*\y1+.5*\y2) .. (0);
\draw let \p1=(5), \p2=(T) in
  (5) .. controls ([xshift=2cm]\x1,.5*\y1+.5*\y2) .. (T);

  \path[every node/.style={font=\sffamily\small}]
    (S) edge node [right] {} (0)
    (0) edge node [left] {} (1)
        edge node [left] {} (2)
        edge node [right] {} (3)
        edge node [left] {} (4)
        edge node [right] {} (5)
    (1) edge node [above] {} (2)
            edge node [left] {} (6)
            edge node [left] {} (T)
    (2) edge node [above] {} (3)
        edge node [left] {} (7)
        edge node [left] {} (T)
    (3) edge node [above] {} (4)
            edge node [left] {} (8)
            edge node [left] {} (T)
    (4) edge node [above] {} (5)
        edge node [right] {} (9)
        edge node [right] {} (T)
    (5) edge node [right] {} (10)
        edge node [right] {} (T)
    ;
    \end{tikzpicture}
\end{document}

在此处输入图片描述

您应该考虑加载positioning库并使用= of语法,而不是旧的、有问题的of=语法。此外,使用该node distance=<length> and <lenght>选项tikzpicture可以避免手动指定每个节点的距离。

以下是使用这些建议的第一个选项的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}

    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},node distance=2.2cm and 1.1 cm]

  \node (S) {};
  \node[main node] (0) [below = 0.8cm of S] {$s$};
  \node[main node] (3) [below = of 0] {3};
  \node[main node] (2) [left = of 3] {2};
  \node[main node] (1) [left = of 2] {1};
  \node[main node] (4) [right = of 3] {4};
  \node[main node] (5) [right = of 4] {5};
  \node (6) [below = 0.8cm of 1] {};
  \node (7) [below = 0.8cm of 2] {};
  \node (8) [below left = 0.8cm of 3] {};
  \node (9) [below = 0.8cm of 4] {};
  \node (10) [below = 0.8cm of 5] {};
  \node[main node] (T) [below = of 3] {$t$};

\draw (T) to[out=180,in=180,looseness=3] (0);
\draw (5) to[out=-20,in=0,looseness=2] (T);

  \path[every node/.style={font=\sffamily\small}]
    (S) edge node [right] {} (0)
    (0) edge node [left] {} (1)
        edge node [left] {} (2)
        edge node [right] {} (3)
        edge node [left] {} (4)
        edge node [right] {} (5)
    (1) edge node [above] {} (2)
            edge node [left] {} (6)
            edge node [left] {} (T)
    (2) edge node [above] {} (3)
        edge node [left] {} (7)
        edge node [left] {} (T)
    (3) edge node [above] {} (4)
            edge node [left] {} (8)
            edge node [left] {} (T)
    (4) edge node [above] {} (5)
        edge node [right] {} (9)
        edge node [right] {} (T)
    (5) edge node [right] {} (10)
        edge node [right] {} (T)
    ;
    \end{tikzpicture}
\end{document}

在此处输入图片描述

使用pgfinterruptboundingbox您可以暂时中断边界框的计算,因此新\draw命令不会添加额外的空间:

\documentclass{article}
\usepackage{tikz}
\usepackage{showframe}
\usetikzlibrary{arrows,positioning}

\begin{document}

\begin{figure}
\centering
    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},node distance=2.2cm and 1.1 cm]

  \node (S) {};
  \node[main node] (0) [below = 0.8cm of S] {$s$};
  \node[main node] (3) [below = of 0] {3};
  \node[main node] (2) [left = of 3] {2};
  \node[main node] (1) [left = of 2] {1};
  \node[main node] (4) [right = of 3] {4};
  \node[main node] (5) [right = of 4] {5};
  \node (6) [below = 0.8cm of 1] {};
  \node (7) [below = 0.8cm of 2] {};
  \node (8) [below left = 0.8cm of 3] {};
  \node (9) [below = 0.8cm of 4] {};
  \node (10) [below = 0.8cm of 5] {};
  \node[main node] (T) [below = of 3] {$t$};

  \begin{pgfinterruptboundingbox}
  \draw (T) to[out=180,in=180,looseness=3] (0);
  \draw (5) to[out=-20,in=0,looseness=2] (T);
  \end{pgfinterruptboundingbox}

  \path[every node/.style={font=\sffamily\small}]
    (S) edge node [right] {} (0)
    (0) edge node [left] {} (1)
        edge node [left] {} (2)
        edge node [right] {} (3)
        edge node [left] {} (4)
        edge node [right] {} (5)
    (1) edge node [above] {} (2)
            edge node [left] {} (6)
            edge node [left] {} (T)
    (2) edge node [above] {} (3)
        edge node [left] {} (7)
        edge node [left] {} (T)
    (3) edge node [above] {} (4)
            edge node [left] {} (8)
            edge node [left] {} (T)
    (4) edge node [above] {} (5)
        edge node [right] {} (9)
        edge node [right] {} (T)
    (5) edge node [right] {} (10)
        edge node [right] {} (T)
    ;
    \end{tikzpicture}
\caption[loftitle]{Graph}
\end{figure}

\end{document}

在此处输入图片描述

showframe仅用于为文本区域提供视觉指导。

相关内容