0

I am creating my first state diagram in Tikz. It is finished, except for the text alignment.

enter image description here

As you can see with the code I included here, I cannot figure out how to get the text off of the arrows. Also, the text on the middle arrow would look better if it matched the angle of the arrow. I looked online, but it seems there are many very different ways people like to code their Tikz diagrams. It makes it hard to find a solution that is applicable to what I have.

I would greatly appreciate any help with this! I am just now learning Tikz, and it is pretty overwhelming.

\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}
  • You can use the auto option so that nodes along the paths are set to the left or right of the path. The key swap or it's alias ' can be used to swap the side for single nodes. This will look award for long lines of text, though. Maybe you want to use align=center and put \\ at certain places in the node's text to start a new line. – Qrrbrbirlbel Dec 06 '22 at 09:10

1 Answers1

3

I guess you meant like this below, and I adjusted the head of your code; I'm compiling with Xelatex here.

Hope this will be one of the solutions that fits you well ^.^

You can use pos to adjust the relevant position of text and the arrow line (closer to start or closer to end, from 0 to 1), something like this [pos=.5] and set \path [every node/.style={sloped}] to make the text's direction consistent with your lines. some of your text here is a bit long that it corrupt into a block, you can try [below/above/..=xxx pt] to adjust the distance.

enter image description here

\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}

Besides, for more information, you can check these questions: