独立颜色节点

独立颜色节点

下午好,

我想知道如何在不改变其他两个节点的颜色的情况下将节点 A 和 Y 涂成另一种颜色(例如灰色)。

此致。

\begin{figure}[h]
\centering
\begin{tikzpicture}[
  node distance=1cm and 1cm,
  mynode/.style={draw,circle,text width=0.5cm,align=center}
]

\node[mynode] (z) {A};
\node[mynode,right=of z] (x) {Y};
\node[mynode,left=of z] (y) {$U_A$};
\node[mynode,right=of x] (w) {$U_Y$};

\path (x) edge[latex-] (z);
\path (y) edge[latex-] (z);
\path (x) edge[latex-] (w);

\end{tikzpicture}
\caption{Graph.}
\end{figure}

答案1

当你写draw它意味着节点的黑色轮廓,并且没有fill提到

![在此处输入图片描述

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}


\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}[
  node distance=1cm and 1cm,
  mynode/.style={draw,circle,text width=0.5cm,align=center}
]

\node[mynode, draw=red,fill=gray] (z) {A};
\node[mynode,right=of z,draw=green, fill=black!20] (x) {Y};
\node[mynode,left=of z] (y) {$U_A$};
\node[mynode,right=of x] (w) {$U_Y$};

\path (x) edge[latex-] (z);
\path (y) edge[latex-] (z);
\path (x) edge[latex-] (w);

\end{tikzpicture}
\caption{Graph.}
\end{figure}
\end{document}

答案2

您可以my node用两个参数进行定义:第一个参数表示边缘颜色,第二个参数表示填充颜色。例如。让我将其重命名为V

V/.style args = {#1/#2}{circle, draw=#1, semithick, fill=#2,
                        text width=2.1em, inner sep=0pt, align=center,
                        on chain},

用于chains定位节点(与positioning库一起)以及arrows.meta定义箭头,完整的 MWE(仅关注图像)可以是:

\documentclass[margin=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning}


\begin{document}
        \begin{tikzpicture}[
node distance = 1cm and 1cm,
  start chain = V going right, % nodes are in chain with name V
V/.style args = {#1/#2}{circle, draw=#1, semithick, fill=#2, % <--- arguments for colors
                        text width=2.1em, inner sep=0pt, align=center,
                        on chain}, % nodes are in chain
   V/.default = black/white,       % default nodes colors
every edge/.style = {draw,-{Triangle[angle=45:2pt 4]}}
                        ] 
\node[V]                {$U_A$};    % node name is V-1, node has default colors
\node[V=red/gray]       {$A$};
\node[V=teal/gray!30]   {$Y$};
\node[V]                {$U_Y$};    % node name is V-4
%
\path   (V-2) edge  (V-1)
        (V-2) edge  (V-3)
        (V-3) edge  (V-4);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容