TikZ:如何在不改变节点的情况下更改路径颜色?

TikZ:如何在不改变节点的情况下更改路径颜色?

很难用 Google 来解决这个问题,因为我使用totikz 中的命令来向左或向右弯曲边缘。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\newlength{\nodedist}
\setlength{\nodedist}{5cm}
\begin{tikzpicture}[
      mynode/.style={text width=4cm,draw,rounded
      corners=5pt,shape=rectangle,minimum width=1cm,text depth=2cm,inner sep=5pt},
      node distance=\nodedist,
      arrow label/.style={midway,fill=white,draw,shape=rectangle,rounded corners=3pt},
   ]
   \draw (0,0) node[mynode] (model) {
      \makebox[4cm]{Model}\\
      \hrulefill \\
   };
   \node[mynode,right=of model] (view) {
      \makebox[4cm]{View}\\
      \hrulefill \\
   };

   \node[mynode] (controller) at ($(model) !0.5! (view) + (0,-\nodedist)$) {
      \makebox[4cm]{Controller}\\
      \hrulefill \\
   };
   \draw[->] (controller.west) to[bend left,red]  node[arrow label] {Change state} (model.south);
   \draw[->] (controller.east) to[bend right]  node[arrow label] {Update display} (view.south);
   \draw[->]  (view.west) to[bend right]  node[arrow label] {Send user input} (controller.north);
   \draw[->]  (view.north) to[bend right]  node[arrow label] {Request state} (model.north);
   \draw[->]  (model.east) to[bend left] node[arrow label] {Notify of change} (view.north west);

\end{tikzpicture}
\end{document}

我想更改箭头颜色而不影响沿箭头放置的节点。这可以直接实现吗?我可以使用edge图库中的工具,但我想知道是否可以用 来实现to

答案1

您可以使用scope带有选项

every to/.style={append after command={[draw=red]}}

例子:

\begin{scope}[every to/.style={append after command={[draw=red]}}]
   \draw[->] (controller.west) to[bend left]  node[arrow label] {Change state} (model.south);
   \draw[->] (controller.east) to[bend right]  node[arrow label] {Update display} (view.south);
   \draw[->]  (view.west) to[bend right]  node[arrow label] {Send user input} (controller.north);
   \draw[->]  (view.north) to[bend right]  node[arrow label] {Request state} (model.north);
   \draw[->]  (model.east) to[bend left] node[arrow label] {Notify of change} (view.north west);
   \end{scope}

为了防止节点边框变成红色,请draw=black使用

arrow label/.style={midway,fill=white,draw=black,shape=rectangle,rounded corners=3pt},

完整代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\newlength{\nodedist}
\setlength{\nodedist}{5cm}
\begin{tikzpicture}[
      mynode/.style={text width=4cm,draw,rounded
      corners=5pt,shape=rectangle,minimum width=1cm,text depth=2cm,inner sep=5pt},
      node distance=\nodedist,
      arrow label/.style={midway,fill=white,draw=black,shape=rectangle,rounded corners=3pt},
   ]
   \draw (0,0) node[mynode] (model) {
      \makebox[4cm]{Model}\\
      \hrulefill \\
   };
   \node[mynode,right=of model] (view) {
      \makebox[4cm]{View}\\
      \hrulefill \\
   };

   \node[mynode] (controller) at ($(model) !0.5! (view) + (0,-\nodedist)$) {
      \makebox[4cm]{Controller}\\
      \hrulefill \\
   };
   \begin{scope}[every to/.style={append after command={[draw=red]}}]
   \draw[->] (controller.west) to[bend left]  node[arrow label] {Change state} (model.south);
   \draw[->] (controller.east) to[bend right]  node[arrow label] {Update display} (view.south);
   \draw[->]  (view.west) to[bend right]  node[arrow label] {Send user input} (controller.north);
   \draw[->]  (view.north) to[bend right]  node[arrow label] {Request state} (model.north);
   \draw[->]  (model.east) to[bend left] node[arrow label] {Notify of change} (view.north west);
   \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

与 Harish Kumar 建议的方法几乎相同:

\documentclass[border=3mm,
               tikz,
               prewiev]{standalone}
\usetikzlibrary{calc,positioning}
    \begin{document}
\newlength{\nodedist}
\setlength{\nodedist}{5cm}
\begin{tikzpicture}[
     node distance = \nodedist,
              draw = red,
     mynode/.style = {shape=rectangle, draw=black, rounded corners=5pt,
                     text width=4cm, text depth=2cm, inner sep=5pt},
arrow label/.style = {shape=rectangle, draw=black, rounded corners=3pt, fill=white},
   ]
\draw (0,0) node[mynode] (model) {\hfil Model\\ \hrulefill};
\node[mynode,right=of model] (view) {\hfil View\\ \hrulefill};
\node[mynode] (controller)
    at ($(model)!0.5!(view) + (0,-\nodedist)$) {\hfil Controller\\ \hrulefill};
\draw[->] (controller.west) to[bend left,text=red] node[arrow label] {Change state} (model.south);
\draw[->] (controller.east) to[bend right]  node[arrow label] {Update display} (view.south);
\draw[->]  (view.west) to[bend right]  node[arrow label] {Send user input} (controller.north);
\draw[->]  (view.north) to[bend right]  node[arrow label] {Request state} (model.north);
\draw[->]  (model.east) to[bend left] node[arrow label] {Notify of change} (view.north west);
\end{tikzpicture}
    \end{document}

在上面的代码中,我没有使用范围来使箭头变成红色,只是简单地声明所有线条都是红色,然后节点定义为绘制黑色。我还稍微简化了确定主节点的方式。获得的结果与 Harish Kumar 的回答相同。

相关内容