Graphics, Figures & TablesTikZ: Difficulty Aligning/Rotating Text in State Diagram

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
FrodoSam
Posts: 3
Joined: Tue Dec 06, 2022 12:00 am

TikZ: Difficulty Aligning/Rotating Text in State Diagram

Post by FrodoSam »

Greetings,

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

Image

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}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

TikZ: Difficulty Aligning/Rotating Text in State Diagram

Post by Bartman »

Please use the code tags to mark a code block.

Since I got error messages with your way of inserting quotes, I used a command from the csquotes package or the required characters to get the desired output.

Code: Select all

\documentclass[tikz]{standalone}
\usepackage{csquotes}

\usetikzlibrary{arrows.meta, automata, positioning, quotes}

\begin{document}
\begin{tikzpicture}[
  nodes={align=center},% added to allow line breaks
  node distance = 1cm and 3cm,
  inner sep=5pt,
  every edge/.append style = {draw, -{Straight Barb[scale=0.8]}},
  every edge quotes/.style = {
    auto,% unknown direction "center" removed
    font=\small, 
    inner sep=10pt
  }
]
% nodes
\node[state, initial, accepting] (1) {Init};
\path [nodes=state]
  node[right=of 1] (2) {read\_LC\_on}
  node[right=of 2] (3) {check\\LC\_on\\count}
  node[below=of 3] (4) {read\_LC\_off}
  node[below=of 4] (5) {check\\LC\_off\\count}
  node[left=of 5] (6) {read\_data}
  node[left=of 6] (7) {check\_data}
;
% arrows
\path 
  % option added to change label's position
  (1) edge[bend left, "pos\_edge" above] (2)
  (2) edge[loop above,"else"] ()
      edge[bend left, "data=\enquote*{0}"] (3)
  (3) edge[bend left, "LC\_on counter\\\enquote{out of range}" below right] (1)
      edge[bend left, "LC\_on counter\\``in range''" right] (4)
  (4) edge[loop left,"else"] ()
      edge[bend left, "pos\_edge" right] (5)
  (5) edge["LC\_off counter \enquote{out of range}"' sloped] (1)
      edge[bend left, "LC\_off counter\\``in range''" below] (6)
  (6) edge[bend left, "pos\_edge"] (7)
      edge[loop below,"else"] ()% swap sign removed
  (7) edge[bend left, "data\_counter /= 31"] (6)
      edge[bend left, "data\_counter = 31" left] (1)
;
\end{tikzpicture}
\end{document}
User avatar
MjK
Posts: 89
Joined: Fri Jan 28, 2022 6:09 pm

TikZ: Difficulty Aligning/Rotating Text in State Diagram

Post by MjK »

Note related to the question but to the shown code:

Code: Select all

\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
…
\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .png}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi
This is nonsense. For several years, TeX distributions use the PDFTeX engine not only for pdflatex but also for latex. So \pdfoutput is defined not only with pdflatex but also with latex. With the code above latex would not output a DVI file any longer but also a PDF and this is not intended if using latex instead of pdflatex but would result in issues, e.g., using PS-Tricks.

The code also does not recognize other engines like LuaTeX or XeTeX. In case of the shown code, graphicx would be loaded without option and therefore does automatically use the correct backend. But unfortunately the last part of the shown code would select the wrong extensions!

Moreover, loading graphicx with backend option is not needed. The default graphics.cfg used by graphicx is able to detect all common engines (and even exotic like VTeX) and therefore already uses backend option pdftex automatically, if using pdflatex. And the backend drivers do also know, which extensions are valid with the corresponding backend (and, e.g., for pdftex these are indeed some more with a better order and dvips does not support jpegs). So it would be simply enough and indeed better to just use

Code: Select all

\usepackage{graphicx}
only.

BTW: There are only a very few packages, that would need a backend options. So for most packages, it is mostly better not to use any backend options like pdftex, luatex, xetex etc.
My main topics are KOMA-Script and other questions related to my packages. If I reply to any other topic or if you've not yet added a minimal working example, please do not expect any further response. And please don't forget to tag examples as code.
Post Reply