Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass{article}


%--------------------------------------------------------------------
%--------------------------------------------------------------------
\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}

%--------------------------------------------------------------------------------
\newenvironment{choices}%
  {\list{\choicelabel}%
     {\usecounter{choice}\def\makelabel##1{\hss\llap{##1}}%
       \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 2.5em}%
       \def\choice{%
         \item
       } % choice
       \labelwidth\leftmargin\advance\labelwidth-\labelsep
       \topsep=0pt
       \partopsep=0pt
     }%
  }%
  {\endlist}
%--------------------------------------------------------------------------------
\def\CorrectChoice{%
      \choice
      \addanswer{\thequestion}{\thechoice}%
    }
    \let\correctchoice\CorrectChoice
%    \par % Uncomment this to have choices always start a new line
   % \let\par\@empty
    % If we're continuing the paragraph containing the question,
    % then leave a bit of space before the first choice:
    \ifvmode\else\enskip\fi
    \ignorespaces
  %
  {}
\makeatother
\newbox\allanswers
\setbox\allanswers=\hbox{}
\newcommand{\addanswer}[2]{%
  \global\setbox\allanswers=\hbox{\unhbox\allanswers \quad #1.~#2}%
}
\newcommand{\showanswers}{%
  \vfill
  \begin{center}
    Answers
  \end{center}
  \unhbox\allanswers
}





%--------------------------------------------------------------------
\begin{document}

\begin{enumerate}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \correctchoice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \choice Yellow
    \choice White
  \end{choices}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \choice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \choice Yellow
    \choice White
  \end{choices}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?

  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \choice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?

  \begin{choices}
    \choice Green
    \choice Yellow
    \choice White
  \end{choices}
\end{enumerate}

\showanswers
\end{document}
share|improve this question
2  
Posting an MWE is often a necessary but not a sufficient condition to get answers on this site. You should at least describe the errors you're getting... –  Jubobs Feb 16 at 9:20
add comment

1 Answer

Since you are numbering the questions using a standard enumerate, you need to use the \theenumi and not \thequestion (which will trigger an error since no question counter was defined) in \addanswer, so you need

\addanswer{\theenumi}{\thechoice}

A complete example:

\documentclass{article}


%--------------------------------------------------------------------
%--------------------------------------------------------------------
\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}

%--------------------------------------------------------------------------------
\newenvironment{choices}%
  {\list{\choicelabel}%
     {\usecounter{choice}\def\makelabel##1{\hss\llap{##1}}%
       \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 2.5em}%
       \def\choice{%
         \item
       } % choice
       \labelwidth\leftmargin\advance\labelwidth-\labelsep
       \topsep=0pt
       \partopsep=0pt
     }%
  }%
  {\endlist}
%--------------------------------------------------------------------------------
\def\CorrectChoice{%
      \choice
      \addanswer{\theenumi}{\thechoice}%
    }
    \let\correctchoice\CorrectChoice
%    \par % Uncomment this to have choices always start a new line
   % \let\par\@empty
    % If we're continuing the paragraph containing the question,
    % then leave a bit of space before the first choice:
    \ifvmode\else\enskip\fi
    \ignorespaces
  %
  {}
\makeatother
\newbox\allanswers
\setbox\allanswers=\hbox{}
\newcommand{\addanswer}[2]{%
  \global\setbox\allanswers=\hbox{\unhbox\allanswers \quad #1.~#2}%
}
\newcommand{\showanswers}{%
  \vfill
  \begin{center}
    Answers
  \end{center}
  \unhbox\allanswers
}





%--------------------------------------------------------------------
\begin{document}

\begin{enumerate}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \correctchoice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \correctchoice Yellow
    \choice White
  \end{choices}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \choice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \choice Yellow
    \correctchoice White
  \end{choices}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?

  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \choice Ringo
    \choice Socrates
  \end{choices}
\item What was the color of George Washinton's white horse?

  \begin{choices}
    \choice Green
    \choice Yellow
    \choice White
  \end{choices}
\end{enumerate}

\showanswers
\end{document}

enter image description here

I wonder why aren't you using a dedicated class (such as exam) or package (exsheets, for example) which offer you the functionality that you are building as well as many other ready to use features.

share|improve this answer
    
Thank You So So Much.... –  Manoj Kumar Feb 16 at 23:07
    
@ManojKumar You're welcome! By the way, don't forget that you can accept answers that best solved your problems by clicking the checkmark to their left. In case of doubt, please see How do you accept an answer?. –  Gonzalo Medina Feb 16 at 23:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.