我正在 Tikz 中创建我的第一个状态图。除了文本对齐外,它已完成。
正如您在此处包含的代码所示,我无法弄清楚如何将文本从箭头中移除。此外,如果中间箭头上的文本与箭头的角度相匹配,效果会更好。我在网上查找过,但似乎人们喜欢以多种不同的方式编写 Tikz 图表。这很难找到适用于我所拥有的解决方案。
我将不胜感激任何帮助!我现在才开始学习 Tikz,这对我来说相当困难。
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb,theorem}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, positioning, quotes}
\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse % we are not running PDFLaTeX
\else
\pdfoutput=1 % we are running PDFLaTeX
\pdftrue
\fi
\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi
\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in
\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .png}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
[
node distance = 10mm and 30 mm,
inner sep=5pt,
every edge/.append style = {draw, -{Straight Barb[scale=0.8]}},
every edge quotes/.style = {auto=center, font=\small, inner sep=10pt}
]
% nodes
\node[state, initial, accepting] (1) {Init};
\node[state, right= of 1] (2) {read\_LC\_on};
\node[state, right= of 2] (3) {check LC\_on count};
\node[state, below= of 3] (4) {read\_LC\_off};
\node[state, below= of 4] (5) {check LC\_off count};
\node[state, left= of 5] (6) {read\_data};
\node[state, left= of 6] (7) {check\_data};
% arrows
\path (1) edge[bend left, "pos\_edge"] (2)
(2) edge[loop above,"else"] ()
edge[bend left, "data=\‘0\’"] (3)
(3) edge[bend left, "LC\_on counter \“out of range\”"] (1)
edge[bend left, "LC\_on counter \“in range\”"] (4)
(4) edge[loop left,"else"] ()
edge[bend left, "pos\_edge"] (5)
(5) edge["LC\_off counter \“out of range\”"] (1)
edge[bend left, "LC\_off counter \“in range\”"] (6)
(6) edge[bend left, "pos\_edge"] (7)
edge[loop below,"else" '] ()
(7) edge[bend left, "data\_counter /= 31"] (6)
edge[bend left, "data\_counter = 31"] (1);
\end{tikzpicture}
\caption{State Diagram for the Remote Controller}
\label{fig:my_label}
\end{figure}
\end{document}
答案1
我猜你的意思是下面这样,我调整了你的代码头;我在这里用 Xelatex 进行编译。
希望这将是适合您的解决方案之一^.^
你可以用 来pos
调整文字和箭头线的相应位置(更靠近起点还是靠近终点,从0到1),类似这样[pos=.5]
设置\path [every node/.style={sloped}]
,让文字的方向和你的线条保持一致。这里的一些文字比较长,容易变成块状,你可以试着[below/above/..=xxx pt]
调整一下距离。
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb,theorem}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, positioning, quotes}
\usepackage{graphicx}
\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in
\DeclareGraphicsExtensions{.pdf, .jpg, .png}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
[
node distance = 10mm and 30 mm,
inner sep=5pt,
every edge/.append style = {draw, -{Straight Barb[scale=0.8]}},
every edge quotes/.style = {auto=center, font=\small, inner sep=6pt}
]
% nodes
\node[state, initial, accepting] (1) {Init};
\node[state, right= of 1] (2) {read\_LC\_on};
\node[state, right= of 2] (3) {check LC\_on count};
\node[state, below= of 3] (4) {read\_LC\_off};
\node[state, below= of 4] (5) {check LC\_off count};
\node[state, left= of 5] (6) {read\_data};
\node[state, left= of 6] (7) {check\_data};
% arrows
\path [every node/.style={sloped,above}]
(1) edge[bend left] node {pos\_edge} (2)
(2) edge[loop above] node {else} ()
edge[bend left] node {data=0} (3)
(3) edge[bend left] node [pos=.3,below]{LC\_on counter out of range} (1)
edge[bend left] node [below=14pt,pos=.4]{LC\_on counter in range} (4)
(4) edge[loop left] node [pos=.8]{else} ()
edge[bend left] node {pos\_edge} (5)
(5) edge node [pos=.3]{LC\_off counter out of range} (1)
edge[bend left] node [below,pos=.4]{LC\_off counter in range} (6)
(6) edge[bend left] node [below]{pos\_edge}(7)
edge[loop below] node [below]{else} ()
(7) edge[bend left] node {data\_counter /= 31} (6)
edge[bend left] node {data\_counter = 31} (1);
\end{tikzpicture}
\caption{State Diagram for the Remote Controller}
\label{fig:my_label}
\end{figure}
\end{document}
此外,如需更多信息,您可以查看以下问题: