我正在尝试在箭头上方和下方添加文本,但我似乎无法找到一种方法来做到这一点,而无需在彼此之上创建两个箭头,这看起来很尴尬。
我现在所拥有的看起来有点像这样:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, arrows.meta, positioning}
\begin{document}
\begin{tikzpicture} [node distance = 4cm, auto]
\node (0) [state, initial] {$q_0$};
\node (1) [state, right = of 0] {$q_1$};
\node (2) [state, right = of 1] {$q_2$};
\path [-stealth]
(0) edge node {A} (1)
(1) edge [loop above] node {B} (1)
(1) edge node {sample text above arrow} (2);
\end{tikzpicture}
\end{document}
\end{tikzpicture}
我还希望能够在和sample text below arrow
之间的同一边缘添加一些。(1)
(2)
答案1
一个非常简单的解决方案(在您的代码之前测试过)如下:
代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[-latex,line width=2pt] (1,0)--(5,0) node[midway,above] () {above};
\draw[-latex,line width=2pt] (1,0)--(5,0) node[midway,below] () {below};
\draw[red,-latex,line width=2pt] (1,0)--(5,4) node[midway,above,sloped] () {above};
\draw[red,-latex,line width=2pt] (1,0)--(5,4) node[midway,below,sloped] () {below};
\draw[cyan,-latex,line width=2pt] (1,0) arc (-180:0:2) node[pos=.25,below,sloped] () {below};
\draw[cyan,-latex,line width=2pt] (1,0) arc (-180:0:2) node[pos=.75,above,sloped] () {above};
\end{tikzpicture}
\end{document}
编辑:您还可以考虑这个解决方案:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[-latex,line width=2pt] (1,0)--(5,0)
node[midway,above] {above}
node[midway,below] {below};
\draw[red,-latex,line width=2pt] (1,0)--(5,4)
node[midway,above,sloped] {above}
node[midway,below,sloped] {below};
\draw[cyan,-latex,line width=2pt] (1,0) arc (-180:0:2)
node[midway,below,sloped] {below}
node[midway,above,sloped] {above};
\end{tikzpicture}
\end{document}
输出为:
答案2
您可以简单地使用选项添加另一个边缘节点below
(参见@Qrrbrbirlbel 评论)或使用quotes
库并编写您的 MWE(最小工作示例),如下所示:
\documentclass[margin=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 4cm,
every edge quotes/.style = {auto, align=center}
]
\node (0) [state, initial] {$q_0$};
\node (1) [state, right = of 0] {$q_1$};
\node (2) [state, right = of 1] {$q_2$};
\path[-Stealth]
(0) edge["A"] (1)
(1) edge[loop above, "B"] (1)
(1) edge["sample text\\ above arrow",
"sample text\\ below arrow" swap] (2);
\end{tikzpicture}
\end{document}
swap
相反,您可以简要地写下选项'
。