如何使用 TikZ 將權重定位在對角線上,以便清楚權重屬於哪一條邊?
\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 = 1mm
]
\node[state] (y1) {$y_1$};
\node[state] (y2) [right of=y1] {$y_2$};
\node[state] (y3) [right of=y2] {$y_3$};
\node[state] (x1) [above of=y1]{$x_1$};
\node[state] (x2) [above of=y2] {$x_2$};
\node[state] (x3) [above of=y3] {$x_3$};
\path[->] (x1) edge node {5} (y1);
\path[->] (y1) edge node {-8} (x2);
\path[->] (x1) edge node {4} (y2);
\path[->] (x2) edge node {3} (y2);
\path[->] (x2) edge node {3} (y3);
\path[->] (y2) edge node {-6} (x3);
\path[->] (x3) edge node {3} (y3);
\end{tikzpicture}
答案1
您可以將標籤定位鍵pos=
與放置選項(above
、above left
、above right
、below
、below left
、below right
)一起使用來進一步自訂其放置。pos
表示邊連接的座標之間的一定距離。我使用了pos=0.25
可以很好地對齊標籤的放置選項。TikZ
更多詳細信息,請參閱手冊第 2.21 節和 17.5.2 節。
這給出了一個可能的解決方案。給出此結果的 MWE 如下。
\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows.meta,automata,positioning}
\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
]
\tikzset{every state}=[
draw = black,
thick,
fill = white,
minimum size = 1mm
]
\node[state] (y1) {$y_1$};
\node[state] (y2) [right=of y1] {$y_2$};
\node[state] (y3) [right=of y2] {$y_3$};
\node[state] (x1) [above=of y1]{$x_1$};
\node[state] (x2) [above=of y2] {$x_2$};
\node[state] (x3) [above=of y3] {$x_3$};
\path[->] (x1) edge node[] {5} (y1);
\path[->] (y1) edge node[pos=0.25,below right] {-8} (x2);
\path[->] (x1) edge node[pos=0.25,above right] {4} (y2);
\path[->] (x2) edge node[] {3} (y2);
\path[->] (x2) edge node[pos=0.25,above right] {3} (y3);
\path[->] (y2) edge node[pos=0.25,below right] {-6} (x3);
\path[->] (x3) edge node[] {3} (y3);
\end{tikzpicture}
\end{document}
請始終發布以 開頭\documentclass
和結尾的完整 MWE \end{document}
。歡迎來到 TeX.SE。
答案2
您也可以使用near start
或near end
。
順便提一句,使用\tikzset
,不使用\tikzstyle
,但是,就您而言,這是沒有必要的,並且below/above of
已被棄用,請參閱 Zarko 的答案。
對於節點定位,您也可以使用tikz matrix
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, matrix}
\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
]
every state/.style={%
draw = black,
thick,
fill = white,
minimum size = 1mm
}
\matrix[%
matrix of math nodes,
column sep = 2.1cm,
row sep = 2.1cm,
inner sep = 0pt,
nodes={state}
] (m) {%
x_1 & x_2 & x_3 \\
y_1 & y_2 & y_3 \\
};
\path[->] (m-1-1) edge node {5} (m-2-1)
(m-2-1) edge node[near start, swap] {-8} (m-1-2)
(m-1-1) edge node[near start] {4} (m-2-2)
(m-1-2) edge node {3} (m-2-2)
(m-1-2) edge node[near end, swap] {3} (m-2-3)
(m-2-2) edge node[near end] {-6} (m-1-3)
(m-1-3) edge node {3} (m-2-3);
\end{tikzpicture}
\end{document}
答案3
正確使用 TikZ 庫positioning
right of = ...
是錯誤使用庫,正確的是right=of ...
) ,添加庫quotes
和所有樣式定義確定為 的選項tikzpicture
,代碼可以變得清晰,沒有任何混亂,如奇怪的state
樣式定義等,即簡潔:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{automata,
positioning, quotes}% <-- added libraries
\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, % edge thick
]
\node[state] (y1) {$y_1$};
\node[state] (y2) [right=of y1] {$y_2$};
\node[state] (y3) [right=of y2] {$y_3$};
\node[state] (x1) [above=of y1] {$x_1$};
\node[state] (x2) [above=of y2] {$x_2$};
\node[state] (x3) [above=of y3] {$x_3$};
\path[->] (x1) edge ["$5$"] (y1)
(y1) edge [pos=0.3, "$-8$"] (x2)
(x1) edge [pos=0.3, "$ 4$"] (y2)
(x2) edge ["$3$"] (y2)
(x2) edge [pos=0.3, "$ 3$"] (y3)
(y2) edge [pos=0.3, "$-6$"] (x3)
(x3) edge ["$3$"] (y3);
\end{tikzpicture}
\end{document}
編輯:
如果您想要更小的邊緣標籤,更靠近邊緣,然後在tikzpicture
選項之間添加
every edge quotes/.append style = {font=\footnotesize, inner sep=2pt}
答案4
使用選項sloped
將文字對角放置
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, calc, chains, positioning, shapes, shapes.arrows}
\usepackage{enumitem}
\newlist{tikzitemize}{itemize}{1}% <-- defined new list
\setlist[tikzitemize]{nosep, % <-- new list setup
topsep = 0pt ,
partopsep = 0pt ,
leftmargin = * ,
label = $\bullet$ ,
before = \vspace{-1.5ex},
}
\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{state}=[
draw = black,
thick,
fill = white,
minimum size = 1mm,
circle,
]
\node[state] (y1) {$y_1$};
\node[state] (y2) [right of=y1] {$y_2$};
\node[state] (y3) [right of=y2] {$y_3$};
\node[state] (x1) [above of=y1]{$x_1$};
\node[state] (x2) [above of=y2] {$x_2$};
\node[state] (x3) [above of=y3] {$x_3$};
\path[->] (x1) edge node[sloped,above] {5} (y1);
\path[->] (y1) edge node[sloped] {-8} (x2);
\path[->] (x1) edge node[sloped] {4} (y2);
\path[->] (x2) edge node[sloped,above] {3} (y2);
\path[->] (x2) edge node[sloped] {3} (y3);
\path[->] (y2) edge node[sloped] {-6} (x3);
\path[->] (x3) edge node[sloped,above] {3} (y3);
\end{tikzpicture}
\end{document}