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.

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?

share|improve this question
    
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
add comment

1 Answer

up vote 7 down vote accepted

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

share|improve this answer
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.