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'm trying to create an environment containing a single-instance, numbered list (so that I can easily control the spacing, etc.). The problem is that I cannot used \ref to cross-ref to the enumerated item.

The code of a MWE is:

\documentclass{article}    
\newcounter{tesisc}
\AtBeginDocument{\setcounter{tesisc}{1}}
\newenvironment*{Tesis}
  {\begin{list}
    {$(\arabic{tesisc})$}
    {\setlength{\rightmargin}{0cm}
     \setlength{\leftmargin}{2\parindent}}
    \item
  }
  {\end{list}\refstepcounter{tesisc}}
\begin{document}
\begin{Tesis}
  Testing testing testing testing testing testing testing testing testing testing.
  \label{th:one}
\end{Tesis}
\begin{Tesis}
  Testing testing testing testing testing testing testing testing testing testing.
  \label{th:two}
\end{Tesis}
This is one reference: \ref{th:one}. This is another reference: \ref{th:two}.
\end{document}

My environment numbering works fine, but not the cross-referencing.

enter image description here

How can I tweak my definition in order for my references work?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

You need to move the \refstepcounter earlier, or as here use \usecounter so list sets up \item to increment the counter.

\documentclass{article}    
\newcounter{tesisc}

\newenvironment*{Tesis}
  {\begin{list}
    {$(\arabic{tesisc})$}
    {\setlength{\rightmargin}{0cm}
     \refstepcounter{tesisc}\setlength{\leftmargin}{2\parindent}}
    \item
  }
  {\end{list}}
\begin{document}
\begin{Tesis}
  Testing testing testing testing testing testing testing testing testing testing.
  \label{th:one}
\end{Tesis}
\begin{Tesis}
  Testing testing testing testing testing testing testing testing testing testing.
  \label{th:two}
\end{Tesis}
This is one reference: \ref{th:one}. This is another reference: \ref{th:two}.
\end{document}
share|improve this answer
    
My comment was just earlier by 1 second. Need to delete that now :) –  hpesoj626 Dec 24 '12 at 1:06
    
Great! But why just moving \refstepcounter does the trick? –  NVaughan Dec 24 '12 at 1:15
    
It is refstepcounter that sets up the context that \label saves to the aux file, so if you do it in the end code it's too late. –  David Carlisle Dec 24 '12 at 1:18

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.