我对 LaTeX 和 tikz 都还不太熟悉。我尝试绘制一个有向图,结果确实如我所愿。我不知道这段代码写得如何,但它确实有效。问题代码:
\begin{tikzpicture}[>=Stealth,->,line width=1pt, domain=0:2,label/.style={%
postaction={ decorate,transform shape,
decoration={ markings, mark=at position 0.5 with \node #1;}}}]
\matrix [matrix of math nodes,
column sep={1.5cm,between origins},
row sep={2.2cm,between origins},
nodes={circle, draw, minimum size=1cm}] {
& |(1)| J & & |(2)| A & \\
& & |(3)| R & \\};
\draw[label={[below]{$50$}}] (2)--(1);
\draw[label={[below]{$25 \%$}}] (3)--(2);
\draw[transform canvas={xshift=2pt, yshift=2pt}, shorten <= -1pt, label= {[below]{$100$}}] (3)--(1);
\draw[transform canvas={xshift=-2pt, yshift=-2pt}, shorten <= -1pt, label= {[below]{$2 \%$}}] (1)--(3);
\end{tikzpicture}
然而,在找到一种可行的解决方案来为绘制的线条添加标签后,我现在遇到了最上面的标签颠倒的问题。为什么会这样,我很清楚,但我不知道如何修复它。
我读过一些关于做类似的事情
\draw[label={[below, rotate=180]{$50$}}] (2)--(1);
但它不起作用。
如果您碰巧知道如何解决我的问题或改进有向图的结构,我将不胜感激!
答案1
你的图片让我想起了自动机,那么为什么不使用automata
图书馆呢?
\documentclass[border=3mm,
tikz,
]{standalone}
\usetikzlibrary{arrows.meta,automata,positioning,babel}
\begin{document}
\begin{tikzpicture}[
node distance = 30mm,
on grid,
auto,
> = {Stealth[length=5pt,width=4pt]},
every state/.style = {draw=blue!50,very thick,fill=blue!20}
]
\node[state] (J) {$J$};
\node[state] (R) [below right=of J] {$R$};
\node[state] (A) [above right=of R] {$A$};
\path[->] (A) edge node[above] {50} (J)
(J) edge[bend left] node[pos=0.4,sloped] {2\%} (R)
(R) edge node[pos=0.7,sloped] {25\%} (A)
(R) edge[bend left] node[pos=0.4,sloped] {100} (J)
;
\end{tikzpicture}
\end{document}
它给:
由于sloped
边缘节点选项没有将边缘标签放在路径的中间,因此我手动纠正了它们的位置(通过试验)。
升级:通过使用库quotes
绘制,这个自动化变得更加简单:
\documentclass[border=3mm,
tikz,
]{standalone}
\usetikzlibrary{arrows.meta,automata,quotes,positioning,babel}
\begin{document}
\begin{tikzpicture}[
node distance = 30mm,
on grid,
> = {Stealth[length=5pt,width=4pt]},
every state/.style = {very thick},
every edge quotes/.style = {sloped, anchor=north}
]
\node[state] (J) {$J$};
\node[state] (R) [below right=of J] {$R$};
\node[state] (A) [above right=of R] {$A$};
\path[->] (A) edge ["50"] (J)
(J) edge[bend left,"2\%"] (R)
(R) edge["25\%"] (A)
(R) edge[bend left,"100"] (J);
\end{tikzpicture}
\end{document}
获得的图片几乎相同(这次是“黑白”版本)。这里不需要手动调整边缘标签...
编辑: 我不知道在提供的代码中使用 babel 可能有什么影响。一个解决方案,如何复制此提供的 OP:
\usepackage[shorthands=off,ngerman]{babel}
另一个更通用的 —— 提醒我 Gonzalo Medina —— 是使用 babel 库:
\usetikzlibrary{arrows.meta,automata,quotes,positioning,%
babel% for avoid complications with any language in babel
}
我相应地修正了以上两个例子。
答案2
您不需要label
样式,但可以立即添加节点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,matrix}
\begin{document}
\begin{tikzpicture}[>=Stealth,->,line width=1pt]
\matrix [matrix of math nodes,
column sep={1.5cm,between origins},
row sep={2.2cm,between origins},
nodes={circle, draw, minimum size=1cm}] {
& |(1)| J & & |(2)| A & \\
& & |(3)| R & \\};
\draw(2)-- node[below]{$50$}(1);
\draw (3)--node[below,sloped]{$25\%$}(2);
\draw[transform canvas={xshift=2pt, yshift=2pt}, shorten <= -1pt,] (3)-- node[rotate=180,below,sloped]{$100$}(1);
\draw[transform canvas={xshift=-2pt, yshift=-2pt}, shorten <= -1pt] (1)-- node[below,sloped]{$2\%$}(3);
\end{tikzpicture}
\end{document}