# Automata in logical relation

I am trying to write a logical expression involving 2 automata as shown on the attached picture. But I am having the following error message:

Use of \a doesn't match its definition

\documentclass[12pt]{book}
\usepackage[paperwidth=16cm, paperheight=24cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{tikz}

\usetikzlibrary{arrows,automata,matrix,positioning}

\begin{document}
\begin{figure}
\centering
\def\a1
{%
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state](2)[right=of 1]{2};
\node[state, accepting](3)[right=of 2]{3};

\path[->] (1) edge node {$a$}   (2);
\path[->] (2) edge node {$b$}   (3);
\end{tikzpicture}
}

\def\a2
{%
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state, accepting](3)[right=of 1]{3};

\path[->] (1) edge node {$a$}   (2);
\path[->] (2) edge node {$b$}   (3);
\end{tikzpicture}
}
\a1 $\implies$ \a2
\end{figure}
\end{document}


• \a1 and \a2 are not valid macro names. If you really wanted it, you would need \expandafter\def\csname a1\endcsname{...} and then invoke it with \csname a1\endcsname. But it would be easier to just call it \aA and \aB for example. May 31, 2020 at 23:49

Steven is of course right when he says that \a1 is not a valid macro name. You can make it effectively valid as follows:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\usetikzlibrary{arrows,automata,matrix,positioning}

\begin{document}
\def\a#1{\ifcase#1\or
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state](2)[right=of 1]{2};
\node[state, accepting](3)[right=of 2]{3};

\path[->] (1) edge node {$a$}   (2);
\path[->] (2) edge node {$b$}   (3);
\end{tikzpicture}
\or
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state, accepting](3)[right=of 1]{3};

\path[->] (1) edge node {$ab$}   (3);
\end{tikzpicture}
\fi}
$\vcenter{\hbox{\a1}}\implies\vcenter{\hbox{\a2}}$
\end{document}


There are, however, reasons for concern:

1. You should not use \def, especially in conjunction with very short macro names.
2. You should not use single-letter macros.
3. As arrows is deprecated, you might want to switch to arrows.meta.

So you could do something like this:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}

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

\begin{document}
\newcommand\mypic[1]{\vcenter{\hbox{
\begin{tikzpicture}[>=Stealth, shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\ifcase#1\or
\node[state](2)[right=of 1]{2};
\node[state, accepting](3)[right=of 2]{3};
\path[->] (1) edge node {$a$}   (2);
\path[->] (2) edge node {$b$}   (3);
\or
\node[state, accepting](3)[right=of 1]{3};
\path[->] (1) edge node {$ab$}   (3);
\fi
\end{tikzpicture}}}}
$\mypic1\implies\mypic2$
\end{document}


As you see, this also spares you from unnecessary duplication.

• Ugh. I misread and misinterpreted the question. Comments deleted. Jun 1, 2020 at 15:57

There are several things wrong with your code, including content too wide for the margins, and not good vertical alignment. But the big issue is in misunderstanding the naming rules for macros.

\a1 and \a2 are not valid macro names. While non-alphabetic characters can be part of control-sequence names, if you use the proper csname conventions, only alphabetic characters (or more precisely, only \catcode = 11 tokens) can be used by simply prepending a catcode 0 backslash to the name. Thus, \aA is a valid macro name, \a1 is not.

If you really wanted numbers in your macro name, you would need \expandafter\def\csname a1\endcsname{...} and then invoke it with \csname a1\endcsname. But it would be easier to just call it \aA and \aB for example, as I do in the below MWE.

\documentclass[12pt]{book}
\usepackage[paperwidth=16cm, paperheight=24cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{tikz,amsmath}

\usetikzlibrary{arrows,automata,matrix,positioning}

\begin{document}
\begin{figure}
\centering
\def\aA
{%
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state](2)[right=of 1]{2};
\node[state, accepting](3)[right=of 2]{3};

\path[->] (1) edge node {$a$}   (2);
\path[->] (2) edge node {$b$}   (3);
\end{tikzpicture}
}

\def\aB
{%
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=1cm]
\node[initial, state](1){1};
\node[state, accepting](3)[right=of 1]{3};

\path[->] (1) edge node {$ab$}   (3);
\end{tikzpicture}
}
\makebox[0pt]
{\aA \raisebox{12pt}{$\implies$} \aB}
\end{figure}
\end{document}


• \def\a1{...} actually does not define \a with the substitute 1, it defines \a with replacement text {...} but with argument text 1. So \a1 would actually work as expected but then \a always has to be followed by a 1 and \a2 is invalid (and using \def\a2{...} doesn't help either because that would overwrite the existing \a with a version which requires a 2, making \a1 invalid). Jun 1, 2020 at 0:32
• @MarcelKrüger Thank you for the lesson. I should have remembered those details. Jun 1, 2020 at 1:27