我正在尝试在节点之间的箭头上编写代码。理想情况下,我希望代码位于箭头的左侧(或右侧)和中间。有没有办法实现这一点?
\documentclass[11pt,a4paper,oneside]{article}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{euscript}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{centernot}
\usepackage{mathtools}
\usepackage{color}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.8 cm, scale = 1, transform shape]
\node[state](A){$n_0$};
\node[state](B)[below of=A]{$n_1$};
\node[state](C)[below left of=B]{$n_2$};
\node[state](D)[below right of=B]{$n_3$};
\path[->](A) edge [below] node [align=center] {} (B)
(B) edge [below] node [align=left] {$\text{\texttt{j < inputExpr.length()}}$} (C)
(B) edge [below] node [align=left] {$\text{\texttt{j >= inputExpr.length()}}$} (D);
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您应该增加节点之间的距离,然后应用于sloped
节点,以便它们遵循箭头和较小的字体。
midway
相当于pos=0.5
。如果你想改变节点放置在箭头的哪一侧,你可以添加above
或 ,below
如下所示:
\node [narrow, above] ...
这样上面就添加到了narrow
中定义的(节点箭头)节点样式\tikzset
。
输出
代码
\documentclass[11pt,a4paper,oneside]{article}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{euscript}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{centernot}
\usepackage{mathtools}
\usepackage{color}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\tikzset{
narrow/.style={sloped, font=\scriptsize, midway}
}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=5 cm, scale = 1, transform shape]
\node[state](A){$n_0$};
\node[state](B)[below of=A]{$n_1$};
\node[state](C)[below left of=B]{$n_2$};
\node[state](D)[below right of=B]{$n_3$};
\path[->](A) edge [below] node [align=center] {} (B)
(B) edge [below] node [narrow] {$\text{\texttt{j < inputExpr.length()}}$} (C)
(B) edge [below] node [narrow] {$\text{\texttt{j >= inputExpr.length()}}$} (D);
\end{tikzpicture}
\end{figure}
\end{document}