3

It is difficult to google for this problem, as I use the to command in tikz in order to bend edges left or right.

\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}

I want to change the arrow colour without affecting the nodes placed along it. Is this straightforwardly possible? I could use edge from the graph library, but I wonder if it can be done with to.

  • 1
    Do you want \draw[->, draw=red]? This colours everything but the text in the node. If you want just the arrow in red, you will have to modify to arrow label/.style={midway,fill=white,draw=black,shape=rectangle,rounded corners=3pt} if feasible. – LaRiFaRi Sep 1 '15 at 9:31
  • I see. Additionally, I have to set text=black on arrow label/.style so the text does not get coloured. – oarfish Sep 1 '15 at 9:45
  • 1
    If you have set \draw[->, draw=red] and not \draw[->, red], text=black should not be needed. Just draw=black in your arrow label style. – LaRiFaRi Sep 1 '15 at 9:52
2

You can use a scope with the option

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

Example:

\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}

To prevent this from making the node borders red, use draw=black in

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

Full code:

\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}

enter image description here

2

Almost the same approach as suggested 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}

In above code I didn't use scope for making arrows red, just simple declare to be all line red and then nodes define to be draw black. I alo slightly simplify the way of determinning of the main nodes. Obtained results is the same as at answer of Harish Kumar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.