4
\documentclass{article}

\usepackage{tikz,tkz-graph,tkz-berge}
\usetikzlibrary{arrows, automata}

\begin{document}

\begin{tikzpicture}[
        > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        auto,
        node distance = 3cm, % distance between nodes
        semithick % line style
    ]

\tikzstyle{every state}=[
        draw = black,
        thick,
        fill = white,
        minimum size = 4mm
    ]

    \node[state] (5)  {5};
    \node[state] (2) [right of=5] {2};
    \node[state] (4) [below left of=2] {4};
    \node[state] (3) [below right of=4] {3};
    \node[state] (6) [left of=3] {6};
    \node[state] (1) [above right of=3] {1};

    \path[->] (2) edge node {5} (5);
    \path[->] (5) edge node {15} (4);
    \path[->] (6) edge node {10} (5);
    \path[->] (6) edge node {4} (4);
    \path[->] (2) edge node {30} (6);
    \path[->] (3) edge node {15} (6);
    \path[->] (3) edge node {4} (2);
    \path[->] (2) edge node {20} (1.west);
    \path[->] (1) edge node {2} (2.east);
    \path[->] (1) edge node {15} (3);
    \end{tikzpicture}

\end{document}

I only want one node which is 5 to be red, how to do it?

enter image description here

  • \node[state, red] (5) {5}; ? – cfr Dec 20 '17 at 3:42
  • @cfr It only changes the color of "5", but I think it has reached my purpose. – Boris Dec 20 '17 at 3:48
  • \node[state, red, draw=red] ... – user121799 Dec 20 '17 at 3:54
3

For example you can use the code

\tikzstyle{red state}=[
        draw = red,
        thick,
        fill = white,
        minimum size = 4mm,
        circle
    ]

    \node[red state] (5)  {5};

With the following MWE

\documentclass{article}

\usepackage{tikz,tkz-graph,tkz-berge}
\usetikzlibrary{arrows, automata}

\begin{document}

\begin{tikzpicture}[
        > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        auto,
        node distance = 3cm, % distance between nodes
        semithick % line style
    ]

\tikzstyle{every state}=[
        draw = black,
        thick,
        fill = white,
        minimum size = 4mm
    ]

\tikzstyle{red state}=[
        draw = red,
        thick,
        fill = white,
        minimum size = 4mm,
        circle
    ]

    \node[red state] (5)  {5};
    \node[state] (2) [right of=5] {2};
    \node[state] (4) [below left of=2] {4};
    \node[state] (3) [below right of=4] {3};
    \node[state] (6) [left of=3] {6};
    \node[state] (1) [above right of=3] {1};

    \path[->] (2) edge node {5} (5);
    \path[->] (5) edge node {15} (4);
    \path[->] (6) edge node {10} (5);
    \path[->] (6) edge node {4} (4);
    \path[->] (2) edge node {30} (6);
    \path[->] (3) edge node {15} (6);
    \path[->] (3) edge node {4} (2);
    \path[->] (2) edge node {20} (1.west);
    \path[->] (1) edge node {2} (2.east);
    \path[->] (1) edge node {15} (3);
    \end{tikzpicture}

\end{document}

you get the result:

enter image description here

3

You can add to a style,

  • inline: \node[state, draw = red] (5) {5};
  • or in a definition: \tikzstyle{red state} = [ state, draw = red ]

If you define every style from scratch, such as the other solution

\tikzstyle{red state} = [
        draw = red,
        thick,
        fill = white,
        minimum size = 4mm,
        circle
    ]

then you have to adjust all such styles if you would change the base style.

I rather define

  • a base node style
  • a colored version of the base node style via [base, draw = ...]
  • another emphasized version of the base node style such as via [base, fill = ...]
  • ...

so I can easily change the base node style and all derived styles inherit the change immediately.

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.