\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}
我只想要一个节点 5 为红色,怎么做?
答案1
例如,您可以使用代码
\tikzstyle{red state}=[
draw = red,
thick,
fill = white,
minimum size = 4mm,
circle
]
\node[red state] (5) {5};
使用以下 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}
得到结果:
答案2
您可以添加样式,
- 排队:
\node[state, draw = red] (5) {5};
- 或者在定义中:
\tikzstyle{red state} = [ state, draw = red ]
如果你从头定义每种风格,比如另一个解决方案
\tikzstyle{red state} = [
draw = red,
thick,
fill = white,
minimum size = 4mm,
circle
]
那么如果您想改变基本样式,就必须调整所有这些样式。
我宁愿定义
- 基本节点样式
- 基本节点样式的彩色版本
[base, draw = ...]
- 另一个强调基节点样式的版本,例如通过
[base, fill = ...]
- ...
所以我可以轻松更改基本节点样式,并且所有派生样式都会立即继承这一更改。