# “Undefined Control Sequence” error despite \newcommand definition!

I have installed texlive full package:

I use TeXmaker and this is a code where I want to draw a grid as in tikzpicture. I want to just call the \gridkar function defined below. So I defined it in \newcommand. Unfortunately it returns two errors:

Undefined Control Sequence \gridkar

and

Package PGF Math Error: Unknown Function 'x' (in 'x')

\documentclass[12 pt]{exam}
\usepackage[T1]{fontenc}
\usepackage{tgtermes}
\usepackage{amsmath,amssymb,color}
\usepackage{enumerate,graphicx}
\usepackage{tikz}
\usepackage{xargs}
\usepackage{latexsym}

\definecolor{SolutionColor}{rgb}{.87,.87,.87}
\newcounter{x}
\newcounter{y}
\newcommand{\gridkar}[2]
{
\begin{tikzpicture}[scale=0.4]
\forLoop{-#1}{#1}{x}
{
\draw[-] (x,-#2) -- (x,#2);
}
\forLoop{-#2}{#2}{y}
{
\draw[-] (-#1,y) -- (#1,y);
}
\end{tikzpicture}
}

\begin{document}
\noindent Name:\hfill Section: 008
\noindent Directions: For each problem please show all your work in the space provided. Calculators are permitted; however, in order to receive partial or full credit on a problem you must show your work. You could use the blank side of this sheet too.\\

\textbf{Maximum Points: 6}\hspace*{\fill}\textbf{Time Limit: 10 minutes}

\begin{questions}
\question [2]\gridkar{7}{8}
\question [3]
\question [1]
\end{questions}
\end{document}


How to fix this? Are there any package I missed out on including?

• What is undefined isn't \gridkar, but \forLoop, which is inside of \gridkar. The forloop package I would expect to be needed, but I don't know if they have a \forLoop... they have a \forloop. – Steven B. Segletes Sep 30 '14 at 22:01
• You need \usepackage{forloop}; but the documentation says “The forloop package defines two commands \forloop (preferred usage) and \forLoop (deprecated)”. – egreg Sep 30 '14 at 22:25

You don't need a for loop operation, TikZ has its own. I've included one possibility but you can also use the TikZ grid key too. I've put also another example.

\documentclass[12 pt]{exam}
\usepackage[T1]{fontenc}
\usepackage{tgtermes}
\usepackage{amsmath,amssymb} % No need and deprecated -> color
\usepackage{enumerate} % % No need -> graphicx
\usepackage{tikz} % Tikz loads graphicx and xcolor
\usepackage{xargs}
\usepackage{latexsym}

\definecolor{SolutionColor}{rgb}{.87,.87,.87}
\newcommand{\gridkar}[2]
{
\begin{tikzpicture}[scale=0.4,baseline]
\foreach\x in {-#1,...,#1}
{
\draw[-] (\x,-#2) -- (\x,#2);
}
\foreach\y in {-#2,...,#2}
{
\draw[-] (-#1,\y) -- (#1,\y);
}
\end{tikzpicture}
}

\begin{document}
\noindent Name:\hfill Section: 008
\noindent Directions: For each problem please show all your work in the space provided.
Calculators are permitted; however, in order to receive partial or full credit on a problem
you must show your work. You could use the blank side of this sheet too.

\textbf{Maximum Points: 6}\hspace*{\fill}\textbf{Time Limit: 10 minutes}

\begin{questions}
\question [2]\gridkar{7}{8}
\question [3]\tikz[baseline,scale=0.4]\draw (-7,-8) grid[step=1] (7,8);
\question [1]
\end{questions}
\end{document}


Your problem is that you read the log file badly. The terminal and log file say:

! Undefined control sequence.
\gridkar ...gin {tikzpicture}[scale=0.4] \forLoop
{-#1}{#1}{x} { \draw [-] (...
l.40 \question [2]\gridkar{7}{8}


The first line says that there is undefined control sequence. That is all. The sequence isn't mentioned here. The second line is broken in the place of the problem. It includes a part of macro body. Which macro is currently processed is mentioned on the left. The problem is in the place of \forLoop. It means that this is the undefined control sequence.

The last line (prefixed by l.40) says that the problem occurs when the input file is read on the line 40. The contents of this line is shown here.