Tikz setcounter not working

I have tried to emulate other code using letters instead of numbers in the foreach in tikz, so here is my code:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,automata, positioning}
\usepackage{float}
\usepackage{ifthen}
\begin{document}
\begin{figure}[H]\centering
\begin{tikzpicture}[>=stealth', auto,node distance=2.5cm]
\foreach \a in {1,2,...,5} {
\setcounter{nodeCount}{\a}
\ifthenelse{\a <= 2}{
\draw (2*\a, 0) node[state, name = \Alph{nodeCount}] {$\Alph{nodeCount}$};
}
{
\draw (2*\a - 6, 1) node[state, name = \Alph{nodeCount}] {$\Alph{nodeCount}$};
}
\edef\underNode{nodeCount}
\node[state, above of = \Alph{\underNode}, name = \Alph{nodeCount}]{$\Alph{nodeCount}$};
}
\end{tikzpicture}
\end{figure}
\end{document}


I am trying to draw this:

How do I properly code it up?

This may be the code you wanted to produce. First of all, you need

\newcounter{nodeCount}


Then you probably wanted to say

\edef\underNode{\Alph{nodeCount}}


You do not need the ifthen package here, arrows got superseded by arrows.meta, and you were loading but not using the positioning library, now it uses the correct syntax above=of ... instead of above of=....

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,automata,positioning}
\usepackage{float}
\newcounter{nodeCount}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}[>=Stealth, auto,node distance=2.5cm]
\foreach \a in {1,2,...,5} {
\setcounter{nodeCount}{\a}
\ifnum\a<3
\draw (2*\a, 0) node[state, name = \Alph{nodeCount}] {$\Alph{nodeCount}$};
\else
\draw (2*\a - 5, 1.5) node[state, name = \Alph{nodeCount}] {$\Alph{nodeCount}$};
\fi
\edef\underNode{\Alph{nodeCount}}
\node[state, above=of \underNode,
name = \Alph{nodeCount}]{$\Alph{nodeCount}$};
}
\end{tikzpicture}
\end{figure}
\end{document}


This does not look really like the target output, which can be obtained very easily e.g. with a matrix.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{automata,matrix}
\newcounter{nodeCount}
\begin{document}
\begin{tikzpicture}[c/.style={state,execute at begin node={%
$\stepcounter{nodeCount}% \Alph{nodeCount}$}}]
\matrix[matrix of nodes,row sep=-6em,column sep=1em] (mat)
{  & & |[c]| & &  |[c]|  & &  \\
|[c]| & &  &|[c]| &   & & |[c]|  \\
& & |[c]| & &  |[c]|  & &  \\
& |[c]|  &  & |[c]|  &   &  |[c]| & \\
};
\path (mat-1-3.south)  (mat-4-2.north);
\end{tikzpicture}
\end{document}


• @Superman I had some interesting experience with ifthen in the past so I do not use it. If anything I'd use xifthen but I did not encounter a case in which I needed it on top of pgf.
– user194703
May 16, 2020 at 6:54
• @Superman I had some similar experiences, you really do not need it when you have pgf. BTW, with your updated code one does qualitatively reproduce the target output. I also added a matrix solution, in which \path (mat-1-3.south) (mat-4-2.north); corrects the bounding box, which is necessary because the row sep is negative.
– user194703
May 16, 2020 at 7:01
• @Superman You can do \draw[<->] (mat-1-3.south) (mat-4-2.north); to see where these coordinates are. And it is the execute at begin node which allows us to do that (well, not quite, there are plenty alternatives). You can always say something like magic/.code={...}, see section 88.1.2 Quick Guide to Using the Key Mechanism of pgfmanual v3.1.5,
– user194703
May 16, 2020 at 7:22
• @Superman \draw (current bounding box.south west) rectangle (current bounding box.north east); before \end{tikzpicture} or with show background rectangle from the backgrounds library, see p. 577 of pgfmanual v3.1.5.
– user194703
May 16, 2020 at 7:36
• @Superman That's a good attitude. Be aware, though, that if you ask three users you will get five different opinions. For instance, not everyone will agree with my judgement of ifthen, say.
– user194703
May 16, 2020 at 7:39