[Tex/LaTex] Undefined control sequence error

tikz-pgf

I am playing with the code adapted from here.

\documentclass{article}
\usepackage{tikz}
\tikzset{
   nodeline/.style={midway, rotate=#1, minimum height=3.5mm, minimum       width=2.5mm}, 
 }

\newcommand\trigram[2]{%
\begin{scope}[rotate=#1]% around={#1:(0,0)}
\def\alenanno{w}  %if it is ying
\clip (-1.1,3) -- (1.1,3) -- (0,0) -- cycle;
\foreac \x [count=\xx starting from 0, evaluate=\xx as \y using (1.75+.5*\xx)] in #2{%
  \ifx\x\alenanno
  \draw[line width=3mm] (-1.2,\y) -- (1.2,\y) node[nodeline=#1,fill=white] {};
  \else
  \draw[line width=3mm] (-1.2,\y) -- (1.2,\y);
  \fi
}
\end{scope}
}

\begin{document}
   \begin{tikzpicture}
   \trigram{0}{b,w,w};
   \end{tikzpicture}
\end{document}

When I compile, I am getting following error without output. Can anybody help with this?

ERROR: Undefined control sequence.
--- TeX said ---
 \trigram ...-- (1.1,3) -- (0,0) -- cycle; \foreac 
                                              \x [count=\xx starting    fro...l.23   \trigram{0}{b,w,w}
                     ;

Best Answer

You spelled \foreach wrong and you are missing a set of braces around #2.

\documentclass{article}
\usepackage{tikz}
\tikzset{
   nodeline/.style={midway, rotate=#1, minimum height=3.5mm, minimum       width=2.5mm}, 
 }

\newcommand\trigram[2]{%
\begin{scope}[rotate=#1]% around={#1:(0,0)}
\def\alenanno{w}  %if it is ying
\clip (-1.1,3) -- (1.1,3) -- (0,0) -- cycle;
\foreach \x [count=\xx starting from 0, evaluate=\xx as \y using (1.75+.5*\xx)] in {#2} {%
  \ifx\x\alenanno
  \draw[line width=3mm] (-1.2,\y) -- (1.2,\y) node[nodeline=#1,fill=white] {};
  \else
  \draw[line width=3mm] (-1.2,\y) -- (1.2,\y);
  \fi
}
\end{scope}
}

\begin{document}
   \begin{tikzpicture}
   \trigram{0}{b,w,w};
   \end{tikzpicture}
\end{document}

enter image description here