TikZ 样式:可选颜色参数,指定圆半径;生成的 PNG 图像的宽度有问题

TikZ 样式:可选颜色参数,指定圆半径;生成的 PNG 图像的宽度有问题

这是使用 TikZ 创建的图表。我想添加以下功能:

  1. 画出更小的圆圈。
  2. 为边缘添加可选的颜色参数。
  3. 生成的 PNG/PDF 图片的宽度不适应 TikZ 图片的宽度。怎么办?
  4. 我希望将 TikZ 图片保存到单独的 PNG/PDF 文件中(我必须将图片插入共享的 Google Docs 中)。

最后,我收到以下警告(在 Linux 中使用 TexStudio 进行编译):

Process started: pdflatex -synctex=1 -interaction=nonstopmode -shell-escape "graph".tex
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `graph.png' @ warning/png.c/MagickPNGWarningHandler/1668.

这是什么意思?

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

\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}

    \begin{tikzpicture} [scale=1,
        vertex/.style={shape=circle, fill=black, draw=black},
        edge/.style={thick,
            decoration={markings,
                mark= at position 0.5 with {\arrow{#1}},
            },
            postaction={decorate}
        }]
        
        \node [vertex, label={left:$s_1$}] (s1) at (-4, 6) {};
        \node [vertex, label={left:$s_2$}] (s2) at (-4, 3) {};
        \node [vertex, label={left:$s_3$}] (s3) at (-4, 0) {};
        
        \node [vertex, label={right:$v_1$}] (v1) at (2, 6) {};
        \node [vertex, label={right:$v_2$}] (v2) at (2, 4) {};
        \node [vertex, label={right:$v_3$}] (v3) at (2, 2) {};
        \node [vertex, label={right:$v_4$}] (v4) at (2, 0) {};
        
        \draw [edge={stealth}] (s1) to node[midway, yshift=.7em]{$8$} (v1);
        \draw [edge={stealth}] (s1) to node[midway, yshift=.7em]{$6$} (v2);
        \draw [edge={stealth}] (s1) to node[midway, xshift=.5em, yshift=.5em]{$10$} (v3);
        \draw [edge={stealth}] (s1) to node[midway, yshift=.7em]{$9$} (v4);
        
        \draw [edge={stealth}] (s2) to node[midway, xshift=-.5em, yshift=.5em]{$9$} (v1);
        \draw [edge={stealth}] (s2) to node[midway, xshift=-.5em, yshift=.6em]{$12$} (v2);
        \draw [edge={stealth}] (s2) to node[midway, yshift=.5em]{$13$} (v3);
        \draw [edge={stealth}] (s2) to node[midway, xshift=.4em, yshift=.4em]{$7$} (v4);
        
        \draw [edge={stealth}] (s3) to node[midway, xshift=-1.2em, yshift=-.1em]{$14$} (v1);
        \draw [edge={stealth}] (s3) to node[midway, xshift=-.5em, yshift=.5em]{$9$} (v2);
        \draw [edge={stealth}] (s3) to node[midway, xshift=-.5em, yshift=.5em]{$16$} (v3);
        \draw [edge={stealth}] (s3) to node[midway, yshift=.6em]{$5$} (v4);
    \end{tikzpicture}


    \begin{tikzpicture} [scale=1,
        vertex/.style={shape=circle, fill=black, draw=black},
        edge/.style={thick,
            decoration={markings,
                mark= at position 0.5 with {\arrow{#1}},
            },
            postaction={decorate}
        }]
    
        \node [vertex, label={left:$S_1$}] (s1) at (-6, 4) {};
        \node [vertex, label={left:$S_2$}] (s2) at (-6, 0) {};
        
        \node [vertex] (n) at (0, 2) {};
        
        \node [vertex, label={right:$C_1$}] (c1) at (6, 4) {};
        \node [vertex, label={right:$C_2$}] (c2) at (6, 0) {};
        
        \draw [edge={stealth}] (s1) to node[midway, yshift=1em]{$I_1$} (n);
        \draw [edge={stealth}] (s2) to node[midway, yshift=1em]{$I_2$} (n);
        \draw [edge={stealth}] (n) to node[midway, yshift=1em]{$I_3$} (c1);
        \draw [edge={stealth}] (n) to node[midway, yshift=1em]{$I_4$} (c2);
    \end{tikzpicture}
        
\end{document}

答案1

在此处输入图片描述

\documentclass[preview,border=2mm,
               convert={density=600,outext=.png}]{standalone}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                decorations.markings,
                quotes}

\begin{document}
\centering
    \begin{tikzpicture}[
            > = Stealth,
vertex/.style = {shape=circle, draw, fill=black, inner sep=2pt}, % <---
  edge/.style = {color=#1, thick, % <---
                 decoration={markings,
                 mark= at position 0.48 with {\arrow{>}},
                },
                postaction={decorate}},
  edge/.default=black,
every edge quotes/.style = {font=\scriptsize, inner sep=1.5pt, 
                            pos=0.48, sloped, auto} % <---
                    ]
\node [vertex, label=left:$s_1$] (s1) at (0, 6) {};
\node [vertex, label=left:$s_2$] (s2) at (0, 3) {};
\node [vertex, label=left:$s_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=red] (s1) to["$8$"] (v1);
\draw [edge] (s1) to ["$6$"] (v2);
\draw [edge] (s1) to ["$10$"] (v3);
\draw [edge] (s1) to ["$9$"] (v4);

\draw [edge] (s2) to ["$9$"] (v1);
\draw [edge] (s2) to ["$12$"] (v2);
\draw [edge] (s2) to ["$13$"] (v3);
\draw [edge] (s2) to ["$7$"] (v4);

\draw [edge] (s3) to ["$14$"] (v1);
\draw [edge] (s3) to ["$9$"] (v2);
\draw [edge] (s3) to ["$16$"] (v3);
\draw [edge] (s3) to ["$5$"] (v4);
    \end{tikzpicture}

    \begin{tikzpicture}[
            > = Stealth,
vertex/.style = {shape=circle, draw, fill=black, inner sep=1.5pt},
  edge/.style = {color=#1, thick,
                 decoration={markings,
                 mark= at position 0.48 with {\arrow{>}},
                },
                postaction={decorate}},
  edge/.default=black,
every edge quotes/.style = {font=\small, inner sep=2pt,
                            pos=0.48, sloped, auto}
                    ]

\node [vertex, label=left:$S_1$] (s1) at (-6, 4) {};
\node [vertex, label=left:$S_2$] (s2) at (-6, 0) {};

\node [vertex] (n) at (0, 2) {};

\node [vertex, label=right:$C_1$] (c1) at (6, 4) {};
\node [vertex, label=right:$C_2$] (c2) at (6, 0) {};

\draw [edge] (s1) to ["$I_1$"] (n);
\draw [edge] (s2) to ["$I_2$"] (n);
\draw [edge] (n)  to ["$I_3$"] (c1);
\draw [edge] (n)  to ["$I_4$"] (c2);
    \end{tikzpicture}
\end{document}

相关内容