6

I am writing a question, and I have something like this:

\begin{enumerate}
  \item Question 1 
    \begin{enumerate}
      \item Choice 1
      \item Choice 2
    \end{enumerate}
\end{enumerate}

and now I want to show the solution to this question. Is there a simple way of doing this? I just want a simple thing; using a package will be an overhead.

I tried adding \label{answer:q} after Choice 2, but when I use \ref{answer:q} it gives me 1b for the second choice, the 1 here is referring to the the parent \item, can I restrict this to the child \item to get only b?

  • Well that is exactly what it is supposed to be doing, right? Labels reference the number and pages etc. In this case you would have to create a command for what you are looking for. – azetina Oct 25 '12 at 4:29
7

You can redefine \p@enumii (the prefix used):

\documentclass{article}          

\makeatletter
\renewcommand\p@enumii{}
\makeatother

\newcommand\Ans[1]{\par The answer is~\ref{#1}.}

\begin{document}

\begin{enumerate}
  \item Question 1 
    \begin{enumerate}
      \item Choice 1
      \item Choice 2\label{test}
    \end{enumerate}
\end{enumerate}
\Ans{test}

\end{document}

enter image description here

Wuth the help of the enumitem package you can define a dedicated list for the choices, and you can easily customize the label and string used for cross-references:

\documentclass{article}          
\usepackage{enumitem}

\newlist{choices}{enumerate}{1}
\setlist[choices]{label=(\alph*),ref=(\alph*)}

\newcommand\Ans[1]{\par The answer is~\ref{#1}.}

\begin{document}

\begin{enumerate}
  \item Question 1 
    \begin{choices}
      \item Choice 1
      \item Choice 2\label{test}
    \end{choices}
\end{enumerate}
\Ans{test}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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