[Tex/LaTex] “Undefined Control Sequence” error despite \newcommand definition!

macrostikz-pgf

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}

\pagestyle{head}      
\firstpageheader{Math110}{October 2}{Quiz 3}
\shadedsolutions
\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?

Best Answer

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}

\pagestyle{head}      
\firstpageheader{Math110}{October 2}{Quiz 3}
\shadedsolutions
\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}

enter image description here