Context

In the document I'm writing, I have redefined each environment and have included a new counter, called counter, to help with the numbering of the environments. My intention is for each definition, proposition, theorem, and so on to all use the same counter, which displays the section and then the counter number with the counter reset for each section. For example, in Section 2, the first definition reads "Definition 2.1", and the following proposition then reads "Proposition 2.2".

The counter has been defined as follows.

\newcounter{counter}
\renewcommand\thecounter{\thesection.\arabic{counter}}

The environments have been defined similarly to the following.

\newenvironment{definition}[1][]{%
 \refstepcounter{counter}%
 \textsc{Definition}~\thecounter. #1
 }%
 {}%

Each environment has \label{} after \begin{<environment>} to insert the anchor.

To reset the counter for each section, I have included \setcounter{counter}{0} after \section{}. Redefining \thecounter to include \thesection meant that cross-referencing with \ref{} displayed the correct section with each number, and all the environments themselves display the correct numbering.

The Issue

The issue I have is that when I click on the link for Environment 2.X, I am taken to Environment 1.X, and similarly for Environment 3.X. The reference does not seem to be reading the section anchor, even though it displays the number for the environment with the correct section.

My guess is that the redefining of \thecounter is causing the issue. How can I fix this?

up vote 4 down vote accepted

Your problem here should be solved using

\newcounter{counter}[section]

which links the counter counter to the section counter, making it reset (be set to 0) with every step of the section counter (at \section{...}).

Here is a complete example of the code that provides the correct jumps:

\documentclass{article}

\usepackage{hyperref}

\newcounter{counter}[section]
\renewcommand{\thecounter}{\thesection.\arabic{counter}}

\newenvironment{definition}[1][]{%
 \refstepcounter{counter}%
 \noindent\textsc{Definition}~\thecounter. #1
 }%
 {\par}%

\begin{document}

See Definitions~\ref{def:first}, \ref{def:second}, \ref{def:third} and \ref{def:last}.

\section{First section}
\begin{definition}\label{def:first}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}

\section{Second section}
\begin{definition}\label{def:second}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}

\section{Third section}
\begin{definition}\label{def:third}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}

\section{Last section}
\begin{definition}\label{def:last}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}

\end{document}
  • Awesome, an easy fix! Thanks. – Bill Wallis Aug 9 at 20:24

Use a more standard framework, which is supported by hyperref out of the box.

\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}

\usepackage{lipsum} % for context

\newtheoremstyle{wallisdefinition}
  {0pt}      % ABOVESPACE
  {0pt}      % BELOWSPACE
  {\upshape} % BODYFONT
  {0pt}      % INDENT (empty value is the same as 0pt)
  {\upshape} % HEADFONT
  {}         % HEADPUNCT
  { }        % HEADSPACE
  % CUSTOM-HEAD-SPEC follows
  {\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}

\theoremstyle{wallisdefinition}
\newtheorem{definition}{Definition}[section]

\begin{document}

\section{Test}

See definition~\ref{def:foo}.

\clearpage

\lipsum[2]

\begin{definition}[Attribution]\label{def:foo}
A \emph{foo} is something very useful. We will use foos all the
time in this paper. This should be enough to wrap.
\end{definition}

\lipsum[3]

\end{document}

I'd add some vertical space, though.

\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}

\usepackage{lipsum} % for context

\newtheoremstyle{wallisdefinition}
  {\topsep}  % ABOVESPACE
  {\topsep}  % BELOWSPACE
  {\upshape} % BODYFONT
  {0pt}      % INDENT (empty value is the same as 0pt)
  {\upshape} % HEADFONT
  {}         % HEADPUNCT
  { }        % HEADSPACE
  % CUSTOM-HEAD-SPEC follows
  {\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}

\theoremstyle{wallisdefinition}
\newtheorem{definition}{Definition}[section]

\begin{document}

\section{Test}

See definition~\ref{def:foo}.

\clearpage

\lipsum[2]

\begin{definition}[Attribution]\label{def:foo}
A \emph{foo} is something very useful. We will use foos all the
time in this paper. This should be enough to wrap.
\end{definition}

\lipsum[3]

\end{document}

Use the optional argument of \newcounter if you want a counter to reset with another. This here works fine:

\documentclass{article}

\newcounter{counter}[section]
\renewcommand\thecounter{\thesection.\arabic{counter}}
\newenvironment{definition}[1][]{%
 \refstepcounter{counter}%
 \textsc{Definition}~\thecounter. #1
 }%
 {}%
\usepackage{hyperref} 
\begin{document}
\section{abc}


\begin{definition}
\label{a}
abc
\end{definition}


\newpage
\section{b}
\begin{definition}
\label{b}
abc
\end{definition}

\newpage
\ref{a} \ref{b}

\end{document}

Let's go through your posting:

In the document I'm writing, I have redefined each environment and have included a new counter, called counter, to help with the numbering of the environments. My intention is for each definition, proposition, theorem, and so on to all use the same counter, which displays the section and then the counter number with the counter reset for each section. For example, in Section 2, the first definition reads "Definition 2.1", and the following proposition then reads "Proposition 2.2".

Instead of doing this, you could define your environments in terms of \newtheorem.
Syntax of \newtheorem is:

\newtheorem{⟨Name of the environment and — in case no already
             existing counter is to be used — name of
             newly to allocate underlying counter.⟩}%
           [⟨Name of already existing counter to be used for 
             numbering the environment.⟩]%
           {⟨Textual phrase denoting the title/heading/the kind of
             item of sectioning produced by an instance of the
             environment.⟩}%
           [⟨Name of superordinate sectioning counter whose value is
             part of the number of the sectioning-item provided by 
             this environment, and whose incrementing via `\refstepcounter`
             triggers resetting the counter in use for numbering the
             environment.⟩]

Thus you could, e.g., do:

\newtheorem{theorem}{Theorem}[section]%
\newtheorem{proposition}[theorem]{Proposition}%
\newtheorem{definition}[theorem]{Definition}%

If with this approach you wish to also use the hyperref package and to use its \autoref-feature, you need to also load the aliascnt package and do:

\documentclass...
...
\usepackage{hyperref}
\usepackage{aliascnt}
...
\newtheorem{theorem}{Theorem}[section]%
%
\newaliascnt{proposition}{theorem}
\newtheorem{proposition}[proposition]{Proposition}%
\aliascntresetthe{proposition}
\newcommand\propositionautorefname{Proposition}
%
\newaliascnt{definition}{theorem}
\newtheorem{definition}[definition]{Definition}%
\aliascntresetthe{definition}
\newcommand\definitionnautorefname{Definition}

Beneath other things, the aliascnt-package solves problems related to the \autoref-feature of the hyperref-package. Also it provides the possibility of having different anchor-name-patterns for hyperlinks. (More information about this can be found in the manual of the hyperref package, section 4: additional user macros, \autoref.)

E.g., the name of the anchor of theorem 2.4 will not be #counter.2.4 but #theorem.2.4.
E.g., the name of the anchor of proposition 2.5 will not be #counter.2.5 but #proposition.2.5.
E.g., the name of the anchor of definition 2.6 will not be #counter.2.6 but #definition.2.6.

The counter has been defined as follows.

\newcounter{counter}
\renewcommand\thecounter{\thesection.\arabic{counter}}

If you wish the counter counter to be reset to the value 0 whenever the counter section is incremented via \refstepcounter, which in turn is used by the sectioning-command \section, apply \newcounter as follows:

\newcounter{counter}[section]

Each environment has \label{} after \begin{<environment>} to insert the anchor.

The assumption that the \label-command would place an anchor is a common misunderstanding.

Anchors are placed by sectioning-commands like \section or \begin{theorem} or \item (of an enumerate-environment). They internally use \refstepcounter as you did with your environments.

\refstepcounter does

  • increment the counter,

  • create an anchor-name,

  • place an anchor of that name,

  • make the name of that just placed anchor available to the \label-macro as the expansion of the macro \@currentHref,

  • make the value of the just incremented counter availabe to the \label-macro as the expansion of the macro \@currentlabel.

\label itself does neither create anchor names nor place anchors. \label only writes information that is provided by sectioning-commands in terms of redefining macros of specific names (\@currentlabel, \@currentHref, ...) into the .aux-file in terms of these nice \newlabel-entries.

In case I wouldn't care about anchor-names for hyperlinks and the \autoref-feature, I would probably do it as follows:

\documentclass{article}
\usepackage{hyperref}
\newcounter{counter}[section]
\renewcommand\thecounter{\thesection.\arabic{counter}}
\renewcommand\theHcounter{\theHsection.\arabic{counter}}

\newcommand\underlyingformatting[1]{%
  \refstepcounter{counter}%
  \par\noindent
  \textsc{#1}~\thecounter. #1
}%

\newcommand\counterautorefname{Theorem/Definition/Proposition}

\newenvironment{theorem}[1][]{\underlyingformatting{Theorem}}{}%

\newenvironment{definition}[1][]{\underlyingformatting{Definition}}{}%

\newenvironment{proposition}[1][]{\underlyingformatting{Proposition}}{}%

\begin{document}

\section{A section}

\ref{thm1}

\autoref{thm1}

\ref{def1}

\autoref{def1}

\ref{pro1}

\autoref{pro1}

\ref{thm2}

\autoref{thm2}

\ref{def2}

\autoref{def2}

\ref{pro2}

\autoref{pro2}

\begin{theorem}\label{thm1}
A theorem 
\end{theorem}

\begin{definition}\label{def1}
A definition
\end{definition}

\begin{proposition}\label{pro1}
A proposition
\end{proposition}

\begin{theorem}\label{thm2}
Another theorem 
\end{theorem}

\begin{definition}\label{def2}
Another definition
\end{definition}

\begin{proposition}\label{pro2}
Another proposition
\end{proposition}

\end{document}

In case of caring about anchor-names and \autoref and the like, I would do it as follows—I "borrowed" the \theoremstyle-code from egreg ;-) :

\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}
\usepackage{aliascnt}

\usepackage{lipsum} % for context

\newtheoremstyle{wallisdefinition}
  {\topsep}  % ABOVESPACE
  {\topsep}  % BELOWSPACE
  {\upshape} % BODYFONT
  {0pt}      % INDENT (empty value is the same as 0pt)
  {\upshape} % HEADFONT
  {}         % HEADPUNCT
  { }        % HEADSPACE
  % CUSTOM-HEAD-SPEC follows
  {\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}

\theoremstyle{wallisdefinition}
\newtheorem{theorem}{Theorem}[section]%

\newaliascnt{proposition}{theorem}
\newtheorem{proposition}[proposition]{Proposition}%
\aliascntresetthe{proposition}
\newcommand\propositionautorefname{Proposition}

\newaliascnt{definition}{theorem}
\newtheorem{definition}[definition]{Definition}%
\aliascntresetthe{definition}
\newcommand\definitionautorefname{Definition}

\begin{document}

\section{Test}

See definition~\ref{def:foo}.

See  \autoref{def:foo}.

See theorem~\ref{thm:foo}.

See  \autoref{thm:foo}.

See proposition~\ref{prp:foo}.

See  \autoref{prp:foo}.

See definition~\ref{def:bar}.

See  \autoref{def:bar}.

See theorem~\ref{thm:bar}.

See  \autoref{thm:bar}.

See proposition~\ref{prp:bar}.

See  \autoref{prp:bar}.

\clearpage

\lipsum[2]

\begin{definition}[(Foo-attribution.)]\label{def:foo}
A \emph{foo} is something very useful. Therefore we will not use 
foos all the time in this paper. This should be enough to wrap.
\end{definition}

\lipsum[3]

\begin{theorem}[(Foo's bar-associativity.)]\label{thm:foo}
Foo is bar-associative. The proof is left to a drunken monkey.
\end{theorem}

\lipsum[4]

\begin{proposition}[(Foo's foobar-associativity.)]\label{prp:foo}
Foo is also foobar-associative. Before the break we announce proving
after the break. After the break we proclaim having proven just
before the break and continue with other things.
\end{proposition}

\lipsum[5]

\begin{definition}[(Bar-attribution.)]\label{def:bar}
A \emph{bar} is something very useful but we will never really
use bars in this paper. This should be enough to wrap.
\end{definition}

\lipsum[6]

\begin{theorem}[(Bar's foo-associativity.)]\label{thm:bar}
Bar is foo-associative. The proof is left to a drunken donkey.
\end{theorem}

\lipsum[7]

\begin{proposition}[(Bar's barfoo-assoviativity.)]\label{prp:bar}
Bar is also barfoo-associative. We proclaim that this is obvious
and cause fear of attracting attention in an embarrassing way on
the side of those people that don't agree.
\end{proposition}

\end{document}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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