0

I am trying to replicate an automata with tikz and was wondering if there is a keyword that would allow be to mimic the position of the states with relative positioning without a "dummy" node. Below is my current solution with a blank node and an image of what I am trying to replicate the state positions of:

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr, float}

\usepackage{amsmath, amssymb, amsthm}
\usepackage{cleveref}
\usepackage{algorithm2e}
\usepackage{enumerate}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\renewcommand{\a}{\mathtt{a}}
\renewcommand{\b}{\mathtt{b}}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto] 
    \node[state,initial] (A_2) {$A_2$};
    \node (Z) [right=of A_2] {};
    \node[state,accepting] (B_2) [right=of Z] {$B_2$};
    \node[state] (C_2) [below=of Z] {$C_2$};
    \path[->]
        (A_2) edge [bend left=15] node {$\varepsilon$} (B_2)
    edge [swap] node {$\a$} (C_2)
        (B_2) edge [bend left=15] node {$\a$} (A_2)
        (C_2) edge [loop right] node {$\b$} ()
    edge [swap] node {$\a,\b$} (B_2);
\end{tikzpicture}
\end{document}

enter image description here

3

I changed \a,\b to \tta,\ttb, because it's not a good idea to overwrite existing macros. Their original definition (in article) is

> \a=macro:
#1->\expandafter \@changed@cmd \csname \string #1\endcsname \relax .

> \b=macro:
->\OT1-cmd \b \OT1\b .

To get the gray background use state/.append style={fill=black!10}. The arrow can be mimicked by >=stealth.

\documentclass[tikz]{standalone}
\usetikzlibrary{automata,positioning}
\newcommand{\tta}{\mathtt{a}}
\newcommand{\ttb}{\mathtt{b}}
\begin{document}
\begin{tikzpicture}[>=stealth,auto,state/.append style={fill=black!10}] 
    \node[state,initial] (A2) {$A_2$};
    \node[state,below right=of A2] (C2) {$C_2$};
    \node[state,accepting,above right=of C2] (B2) {$B_2$};
    \draw (A2) edge[->,bend left=15] node {$\varepsilon$} (B2);
    \draw (B2) edge[->,bend left=15] node {$\tta$} (A2);
    \draw (C2) edge[<-] node {$\tta$} (A2);
    \draw (B2) edge[<-] node {$\tta,\ttb$} (C2);
    \draw (C2) edge [loop right] node {$\ttb$} (C2);
\end{tikzpicture}
\end{document}

enter image description here

1

Place node C_2 first then use right/left of=C_2,above of=C_2 together to place A_2 and B_2 diagonally. The result of following code looks exactly the same as in OP.

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr, float}

\usepackage{amsmath, amssymb, amsthm}
\usepackage{cleveref}
\usepackage{algorithm2e}
\usepackage{enumerate}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\renewcommand{\a}{\mathtt{a}}
\renewcommand{\b}{\mathtt{b}}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto] 
    \node[state] (C_2) {$C_2$};
    \node[state,initial] (A_2) [left of=C_2,above of=C_2] {$A_2$};
    \node[state,accepting] (B_2) [right of=C_2,above of=C_2] {$B_2$};
    \path[->]
        (A_2) edge [bend left=15] node {$\varepsilon$} (B_2)
    edge [swap] node {$\a$} (C_2)
        (B_2) edge [bend left=15] node {$\a$} (A_2)
        (C_2) edge [loop right] node {$\b$} ()
    edge [swap] node {$\a,\b$} (B_2);
\end{tikzpicture}
\end{document}
  • You can use the more efficient above left=of ... syntax. – Herr K. Jan 17 '14 at 20:35
  • I upvoted the other answer, it's better than mine. note also there is no left above=of .... – Billiska Jan 17 '14 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.