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 picked up a template to write exams a while ago only. Each problem is written between \begin{problem} and \end{problem}. In the preamble I have

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{marvosym}
\usepackage{enumerate}
\usepackage{subfigure}
\usepackage{placeins}
%\pagestyle{empty} %<== DELETE IF YOUR TEST HAS MORE THAN ONE PAGE
%    (this page style suppresses page numbers.)


%Adjust the margins:
\addtolength{\textwidth}{1.75in}
\addtolength{\textheight}{1.75in}
\addtolength{\topmargin}{-0.95in}
\addtolength{\hoffset}{-.95in}


%puts in a horizontal  rule the width of the text with space
%before and after.
\newcommand{\sep}{\ifhmode\par\fi\vskip6pt\hrule\vskip6pt}

%A new environment for a test problem.
\newcounter{problem}
\newenvironment{problem}
{
%\stepcounter{problem}%
\refstepcounter{problem}
\filbreak%
\sep
\noindent\arabic{problem}. 
}
{
\filbreak
}

\begin{document}

I replaced \stepcounter{problem} with \refstepcounter{problem}. I do the following when I try to cross-reference:

\begin{document}
\begin{problem}
\label{labelname}
Answer this question
\end{problem}
\begin{problem}
Refer to problem~\ref{labelname}. Now answer this question.
\end{problem}
\end{document}

However, when I compile the .tex file, the PDF file shows "Refer to problem ??. Now answer this question."

How could I fix this? I appreciate the help.

share|improve this question
add comment

1 Answer

After the first run I get the behavior you describe:

enter image description here

But when run again (without deleting the .aux files that are produced in the first run), you obtain the desired results:

enter image description here

Since it is possible to reference a figure before it is placed, two runs are required. The first determines the figure numbers placing a temporary ?? as a placeholder for the reference, and the second resolves the references.

share|improve this answer
    
Awesome! It does work. Thanks a lot. –  user87317 Nov 30 '12 at 19:36
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.