突出显示 TikZ 图片中的边缘

突出显示 TikZ 图片中的边缘

我已附加一张样本图片,我想要一些如图所示的彩色边缘。

在此处输入图片描述


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
%usepackage{algorithm}
 \usepackage{pifont}
\title{test}
\author{xyz}
\date{June 2022}

\begin{document}

 \maketitle

\section{Introduction}
 \begin{figure}[h]
\centering
\resizebox{\textwidth}{!}{\scriptsize
\begin{tikzpicture}[mycircle/.style={circle,draw=black, text opacity=1, inner sep=5pt}, myarrow/.style={-Stealth},node distance=0.6cm and 1.2cm]
  \node[mycircle] (c1) {$s$};
  \node[mycircle,below right=of c1] (c2) {$v_2$};
  \node[mycircle,right=of c2] (c3) {$v_4$};
  \node[mycircle,above right=of c1] (c4) {$v_1$};
  \node[mycircle,right=of c4] (c5) {$v_3$};
  \node[mycircle,below right=of c5] (c6) {$t$};
 \foreach \i/\j/\txt/\p in {% start node/end node/text/position
  c1/c2/2:\textcolor{blue}{2}/below,
  c1/c4/3:\textcolor{blue}{1}/above,
  c2/c3/3:\textcolor{blue}{2}/below,
  c3/c6/2:\textcolor{blue}{1}/below,
  c4/c5/2:\textcolor{blue}{2}/above,
  c5/c6/3:\textcolor{blue}{2}/above,
  c5/c2/1:\textcolor{blue}{1}/below,
  c3/c5/3:\textcolor{blue}{1}/below,
  c2.70/c4.290/3:\textcolor{blue}{1}/below}
   \draw [myarrow] (\i) -- node[sloped,font=\smaller,\p] {\txt} (\j);
\node[below] at (2.75,-2) {(a)};
\end{tikzpicture}
\qquad
  \begin{tikzpicture}[mycircle/.style={circle,draw=black, text opacity=1, inner sep=5pt}, myarrow/.style={-Stealth},node distance=0.6cm and 1.2cm]
  \node[mycircle] (c1) {$s$};
  \node[mycircle,below right=of c1] (c2) {$v_2$};
  \node[mycircle,right=of c2] (c3) {$v_4$};
  \node[mycircle,above right=of c1] (c4) {$v_1$};
  \node[mycircle,right=of c4] (c5) {$v_3$};
  \node[mycircle,below right=of c5] (c6) {$t$};
 \foreach \i/\j/\txt/\p in {% start node/end node/text/position
  c2/c1/2/below,
  c1.60/c4.190/2/above,
  c4.210/c1.35/\textcolor{blue}{1}/below,
  c2.0/c3.180/1/above,
  c3.200/c2.340/\textcolor{blue}{2}/below,
  c3/c6/1/above,
  c6.240/c3.10/\textcolor{blue}{1}/below,
  c5/c4/\textcolor{blue}{2}/above,
  c5.350/c6.110/1/above,
  c6.135/c5.330/\textcolor{blue}{2}/below,
  c2/c5/\textcolor{blue}{1}/below,
  c3.80/c5.280/2/below,
  c5.260/c3.100/\textcolor{blue}{1}/above,
  c4.260/c2.100/\textcolor{blue}{1}/below,
  c2.80/c4.280/2/below}
   \draw [myarrow] (\i) -- node[sloped,font=\smaller,\p] {\txt} (\j);
\node[below] at (2.75,-2) {(b)};


 % draw this outside loop to get proper orientation of 10
 %\draw [myarrow] (c4.260) -- node[sloped,font=\smaller,above,rotate=180] {\textcolor{blue}{1}} (c2.100);

\end{tikzpicture}
}
\caption{Illustrating a residual network: (a) original network $G$: (b) residual network 
$G_f$. Residual network is define based on a flow (a different flow tends to different 
 residual network).}

答案1

\col在循环中添加一个变量\foreach来指定绘制的颜色。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}

\begin{document}
\begin{tikzpicture}[mycircle/.style={circle,draw=black, text opacity=1, inner sep=5pt}, myarrow/.style={-Stealth},node distance=0.6cm and 1.2cm]
\node[mycircle] (c1) {$s$};
\node[mycircle,below right=of c1] (c2) {$v_2$};
\node[mycircle,right=of c2] (c3) {$v_4$};
\node[mycircle,above right=of c1] (c4) {$v_1$};
\node[mycircle,right=of c4] (c5) {$v_3$};
\node[mycircle,below right=of c5] (c6) {$t$};
\foreach \i/\j/\txt/\p/\col/\lw in {% start node/end node/text/position/color/lw
c2/c1/2/below/black/0.8pt,
c1.60/c4.190/2/above/red/2pt,
c4.210/c1.35/\textcolor{blue}{1}/below/black/0.8pt,
c2.0/c3.180/1/above/black/0.8pt,
c3.200/c2.340/\textcolor{blue}{2}/below/red/2pt,
c3/c6/1/above/black/0.8pt,
c6.240/c3.10/\textcolor{blue}{1}/below/red/2pt,
c5/c4/\textcolor{blue}{2}/above/black/0.8pt,
c5.350/c6.110/1/above/black/0.8pt,
c6.135/c5.330/\textcolor{blue}{2}/below/black/0.8pt,
c2/c5/\textcolor{blue}{1}/below/black/0.8pt,
c3.80/c5.280/2/below/black/0.8pt,
c5.260/c3.100/\textcolor{blue}{1}/below/black/0.8pt,
c4.260/c2.100/\textcolor{blue}{1}/below/red/2pt,
c2.80/c4.280/2/below/black/0.8pt}
\draw [myarrow,\col,line width=\lw] (\i) -- node[sloped,font=\tiny,\p,black] {\txt} (\j);
\node[below] at (2.75,-2) {(b)};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

@Tom 的回答令人难以置信。它很好地利用了foreach结构来定制箭头集的每个单独元素。

在探索如何使用 LaTeX 以及如何组织自己的工作的一些哲学时,我仍然可以做出一些小小的贡献。

看了汤姆的回答,我注意到有一些有趣的子集,主要按箭头的类型划分,它们是(a)红色(b)灰色和(c)黑色箭头。因此,为什么不定义整个(子)集合的行为方式,而是定义集合中每个元素的行为方式呢?

按照这样的思路,我定义了三个scope,每个箭头子集一个,然后我可以删除foreach成为整个(子)集的通用定义。它使代码更容易阅读,并更容易找到图中哪一行执行了什么操作。有人可能会说集合的划分是任意的,而且毫无用处。我同意这一点。这里的范例类似于:您是否想要 2 个代码,每个代码有 500 行,或者 500 个代码,每个代码有 2 行?,我更喜欢 10 个 100 行代码或 20 个 50 行代码,并根据哪种更适合问题进行划分。

通过这种拆分,可以更容易地找到应该将leftright添加到below和的箭头above,添加\quad以改变图表中间交叉部分的数字位移,以及插入\rotatebox{}到一些节点以实现原始提出的格式。

将代码分成几部分scope也使更改集合定义变得更简单。假设现在所有红色箭头都必须是green,好的,只需更改集合定义即可。虽然查找和替换操作仍然有效,但更改集合定义更为实用。另一方面,如果红色箭头必须变成黑色箭头,请将其线剪切并粘贴到另一组中。

OBS:我还在s 定义=50pt中添加了node将节点分散到远处的功能,并且一个数字是橙色的。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}[mycircle/.style={circle,draw=black, text opacity=1, inner sep=5pt},
  myarrow/.style={-Stealth},node distance=0.6cm and 1.2cm]
  \node[mycircle] (c1) {$s$};
  \node[mycircle,below right=50pt of c1] (c2) {$v_2$};
  \node[mycircle,right=50pt of c2] (c3) {$v_4$};
  \node[mycircle,above right=50pt of c1] (c4) {$v_1$};
  \node[mycircle,right=50pt of c4] (c5) {$v_3$};
  \node[mycircle,below right=50pt of c5] (c6) {$t$};
  \begin{scope}[red]
    \foreach \i/\j/\txt/\p in {% start node/end node/text/position
      c1.60/c4.190/5/above,
      c2.340/c3.200/\textcolor{blue}{1}/below,
      c3.10/c6.260/\textcolor{blue}{3}/below,
      c4.260/c2.100/\rotatebox{90}{\textcolor{blue}{1}}/below}
    \draw [myarrow,line width=2pt] (\i) -- node[sloped,font=\tiny,\p] {\txt} (\j);
  \end{scope}
  \begin{scope}[black]
    \foreach \i/\j/\txt/\p in {% start node/end node/text/position
      c2/c1/2/above,
      c2/c5/\quad \quad \textcolor{orange}{1}/above right,
      c2.80/c4.280/\rotatebox{-90}1/below,
      c3.180/c2.0/1/above,
      c4.210/c1.35/\textcolor{blue}{1}/below,
      c4.340/c3.110/\quad 1/above right,
      c4.10/c5.170/\textcolor{blue}{2}/above,
      c5.190/c4.350/\textcolor{blue}{2}/below,
      c5.350/c6.110/1/above,
      c6/c3/3/above,
      c6.135/c5.330/\textcolor{blue}{1}/below}
    \draw [myarrow,line width=0.8pt] (\i) -- node[sloped,font=\tiny,\p] {\txt} (\j);
  \end{scope}
  \begin{scope}[gray]
    \foreach \i/\j/\txt/\p in {% start node/end node/text/position
      c1.280/c2.160/0/below,
      c3.130/c4.310/0\quad\quad/below left,
      c5.260/c2.30/0\quad\quad/below left}
    \draw [myarrow,line width=0.8pt] (\i) -- node[sloped,font=\tiny,\p] {\textcolor{gray}\txt} (\j);
  \end{scope}
  
  \node[below] at (2.75,-2.5) {(b)};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容