TikZ:图形中的装饰和引号定位

TikZ:图形中的装饰和引号定位

为了使图表更清晰,这里存在一些定位冲突:

  • 中心的 2 个箭头(从 C1 到 v4 和从 C3 到 v1):它们应该放置在例如 处pos=.48
  • 一些超出边缘的数字应该放在不同的位置,例如pos=.33

是否有一种自动化的方法来获得更清晰的图表?

如果没有,我该如何为样式添加另外 2 个可选参数:decoration位置和quotes位置?

我也非常感激任何有助于改进style定义的意见。

\documentclass[tikz, border=2mm, convert={density=600, outext=.png}]{standalone}

\usepackage{tikz}

\usetikzlibrary{arrows.meta, decorations.markings, quotes}

\begin{document}
    
    \begin{tikzpicture}[
        > = Stealth,
        vertex/.style = {shape=circle, draw, fill=black, inner sep=2pt}, % <---
        edge/.style = {color=#1, thick, % <---
            decoration={markings,
                mark= at position 0.5 with {\arrow{>}},
            },
            postaction={decorate}},
        edge/.default=gray,
        every edge quotes/.style = {font=\scriptsize, inner sep=1.5pt, 
            pos=0.4, sloped, auto=} % <---
        ]
        \node [vertex, label=left:$C_1$] (s1) at (0, 6) {};
        \node [vertex, label=left:$C_2$] (s2) at (0, 3) {};
        \node [vertex, label=left:$C_3$] (s3) at (0, 0) {};
        
        \node [vertex, label=right:$v_1$] (v1) at (6, 6) {};
        \node [vertex, label=right:$v_2$] (v2) at (6, 4) {};
        \node [vertex, label=right:$v_3$] (v3) at (6, 2) {};
        \node [vertex, label=right:$v_4$] (v4) at (6, 0) {};
        %
        \draw [edge] (s1) to ["$8$"] (v1);
        \draw [edge=red] (s1) to ["$6$"] (v2);
        \draw [edge=red] (s1) to ["$10$"] (v3);
        \draw [edge] (s1) to ["$9$"] (v4);
        
        \draw [edge=red] (s2) to ["$9$"] (v1);
        \draw [edge] (s2) to ["$12$"] (v2);
        \draw [edge=red] (s2) to ["$13$"] (v3);
        \draw [edge] (s2) to ["$7$"] (v4);
        
        \draw [edge] (s3) to ["$14$"] (v1);
        \draw [edge=red] (s3) to ["$9$"] (v2);
        \draw [edge] (s3) to ["$16$"] (v3);
        \draw [edge=red] (s3) to ["$5$"] (v4);
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

为了保存图形的整体外观并且不会造成箭头错位,我建议只使用pos=...这里的值。

装饰和引语调整

\documentclass[tikz, border=2mm, convert={density=600, outext=.png}]{standalone}

\usepackage{tikz}

\usetikzlibrary{arrows.meta, decorations.markings, quotes}

\begin{document}
    
    \begin{tikzpicture}[
        > = Stealth,
        vertex/.style = {shape=circle, draw, fill=black, inner sep=2pt}, % <---
        edge/.style = {color=#1, thick, % <---
            decoration={markings,
                mark= at position 0.85 with {\arrow{>}},
            },
            postaction={decorate}},
        edge/.default=gray,
        every edge quotes/.style = {font=\scriptsize, inner sep=1.5pt, 
            pos=0.25, sloped, auto} % <---
        ]
        \node [vertex, label=left:$C_1$] (s1) at (0, 6) {};
        \node [vertex, label=left:$C_2$] (s2) at (0, 3) {};
        \node [vertex, label=left:$C_3$] (s3) at (0, 0) {};
        
        \node [vertex, label=right:$v_1$] (v1) at (6, 6) {};
        \node [vertex, label=right:$v_2$] (v2) at (6, 4) {};
        \node [vertex, label=right:$v_3$] (v3) at (6, 2) {};
        \node [vertex, label=right:$v_4$] (v4) at (6, 0) {};
        %
        \draw [edge] (s1) to ["$8$"] (v1);
        \draw [edge=red] (s1) to ["$6$"] (v2);
        \draw [edge=red] (s1) to ["$10$"] (v3);
        \draw [edge] (s1) to ["$9$"] (v4);
        
        \draw [edge=red] (s2) to ["$9$"] (v1);
        \draw [edge] (s2) to ["$12$"] (v2);
        \draw [edge=red] (s2) to ["$13$"] (v3);
        \draw [edge] (s2) to ["$7$"] (v4);
        
        \draw [edge] (s3) to ["$14$"] (v1);
        \draw [edge=red] (s3) to ["$9$"] (v2);
        \draw [edge] (s3) to ["$16$"] (v3);
        \draw [edge=red] (s3) to ["$5$"] (v4);
    \end{tikzpicture}

\end{document}

相关内容