GeneralHyperref and new counters

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bullit
Posts: 4
Joined: Sun Mar 06, 2011 7:42 am

Hyperref and new counters

Post by bullit »

Greetings to Community.

In a recent post (http://www.latex-community.org/forum/vi ... =5&t=12144), i pointed out a referencing problem with \newenvironment, regarding the book class. With help by gmedina (thanks) i have managed to create a new environment for the examples of a book and also to create a \listofexamples. Now that the book is finished and all the \label's and \ref's work fine, one problem still insists bugging me.

I include an example code for discussion.

Code: Select all

\documentclass[11pt,a4paper,notitlepage,fleqn]{book}

% define example counter
%---------------------------
\newcounter{exampleC}[chapter]
\renewcommand\theexampleC{\thechapter.\arabic{exampleC}}

% define example environment
%---------------------------
\usepackage{relsize}             % for setting smaller fonts in the example environment
\newenvironment{example}[1]%
{\refstepcounter{exampleC}%
 \addcontentsline{xmp}{example}{\theexampleC~~#1}% add the entry to the new list
  \vspace{\baselineskip}\par%
  \begin{quote}%
    \noindent\smaller%
    \textbf{Example~\theexampleC~~#1}
  \end{quote}%
  \par\begin{quotation}\smaller%
    }
{\end{quotation}\noindent}

% define List of Examples
%---------------------------
\newcommand\listexamplename{List of Examples}
\makeatletter
\newcommand\listofexamples{\chapter{\listexamplename}\@starttoc{xmp}}
\newcommand\l@example{\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother

% activate references
%---------------------------
\usepackage[hypertex]{hyperref}

\begin{document}
\frontmatter
\tableofcontents
\listofexamples
\mainmatter
\chapter{I am the first chapter}\label{Ch:01}
Here's an example
\begin{example}{I am the first example of the first chapter}\label{ex:1.1}
example text....
\end{example}
Go to Example~\ref{ex:2.2}
\newpage
And here's another example
\begin{example}{I am the second example of the first chapter}\label{ex:1.2}
example text....
\end{example}
\chapter{I am the second chapter}\label{Ch:02}
Here's an example
\begin{example}{I am the first example of the first chapter}\label{ex:2.1}
example text....
\end{example}
\newpage
And here's another example
\begin{example}{I am the second example of the first chapter}\label{ex:2.2}
example text....
\end{example}
Go to Example~\ref{ex:1.1}
\end{document} 
When i don't use hyperref, everything seems to work fine. Yet, when i declare the hyperref package line, the links referring to the examples are invalid. To be more specific, since the exampleC counter is reset after the beginning of a new chapter, all the example links are referring to the examples of the first chapter. If you run the example code, the xmp file looks like this:

Code: Select all

\contentsline {example}{1.1\nobreakspace {}\nobreakspace {}I am the first example of the first chapter}{1}{exampleC.1}
\contentsline {example}{1.2\nobreakspace {}\nobreakspace {}I am the second example of the first chapter}{2}{exampleC.2}
\contentsline {example}{2.1\nobreakspace {}\nobreakspace {}I am the first example of the first chapter}{3}{exampleC.1}
\contentsline {example}{2.2\nobreakspace {}\nobreakspace {}I am the second example of the first chapter}{4}{exampleC.2}
and i think that this is the problem: i think that it should look like this:

Code: Select all

\contentsline {example}{1.1\nobreakspace {}\nobreakspace {}I am the first example of the first chapter}{1}{exampleC.1.1}
\contentsline {example}{1.2\nobreakspace {}\nobreakspace {}I am the second example of the first chapter}{2}{exampleC.1.2}
\contentsline {example}{2.1\nobreakspace {}\nobreakspace {}I am the first example of the first chapter}{3}{exampleC.2.1}
\contentsline {example}{2.2\nobreakspace {}\nobreakspace {}I am the second example of the first chapter}{4}{exampleC.2.2}
Since i had no results by extensive searching, i ask for your kind suggestions.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Hyperref and new counters

Post by kaiserkarl13 »

I think the problem is that \theexampleC isn't getting expanded all the way inside the label. There's an option to hyperref called "naturalnames" which uses LaTeX-computed names for all links. This solved the problem in your example:

Code: Select all

\usepackage[naturalnames]{hyperref}
Last edited by kaiserkarl13 on Wed Apr 27, 2011 3:50 pm, edited 1 time in total.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Hyperref and new counters

Post by sommerfee »

In most cases(*) hyperref should be used this way:
  1. Load packages (except hyperref)
  2. Load hyperref package
  3. Use package commands and \newcounter and \newenvironment etc.
(*) See hyperref README for exceptions

See also: http://www.tex.ac.uk/cgi-bin/texfaq2htm ... perdupdest
bullit
Posts: 4
Joined: Sun Mar 06, 2011 7:42 am

Re: Hyperref and new counters

Post by bullit »

Problem solved. Thank you very much guys.
Post Reply