# Incorrect ToC/Index cross-references when resetting memoir class counters

The quasi-MWE below uses the memoir class and works fine. It produces a document with the following structure:

• ToC
• Book
• Part1
• Chapter1
• Part2
• Chapter2
• Index

Here, cross-references in the ToC and in the Index all work correctly. But I want to reset Chapter numbers back to "1" in each Part (and ultimately, in each Book, if there are many books). To do so, I uncomment the \counterwithin instructions in the preamble.

Now, depending on the value assigned to hyperref's hypertexnames option, I get the following behaviour:

• if true, the ToC link to Chapter "2" in Part 2 (which is now renumbered to "1") does not works, but the Index link to AAAAA on page "5" does;

• if false, the ToC link to Chapter "2" in Part 2 (which is now renumbered to "1") works, but the Index link to AAAAA on page "5" does not.

Am I doing something wrong?

\documentclass[a4paper,12pt]{memoir}

%\counterwithin*{chapter}{part}           % Restarts chapter count within Part.

\usepackage{hyperref}
\hypersetup{
hypertexnames=true,
}
\indexintoc
\makeindex[main]

\begin{document}
\frontmatter{}
\tableofcontents*
\mainmatter{}
\book{BookTitleA}
\part{PartTitleA}
\chapter{TitleA}
AAAAA\index[main]{AAAAA}.\\
\part{PartTitleB}
\chapter{TitleB}
BBBBB.
\backmatter
\clearpage
\printindex[main]
\end{document}

-
\documentclass[a4paper,12pt]{memoir} %\counterwithin*{chapter}{part} % Restarts chapter count within Part. \usepackage{hyperref} \hypersetup{ hypertexnames=true, linktocpage=false, colorlinks=true, } \indexintoc \makeindex[main] \begin{document} \frontmatter{} \tableofcontents* \mainmatter{} \book{BookTitleA} \part{PartTitleA} \chapter{TitleA} AAAAA\index[main]{AAAAA}. \part{PartTitleB} \chapter{TitleB} BBBBB. \backmatter \clearpage \printindex[main] \end{document} –  Olivier Drolet Oct 19 '11 at 19:45
My apologies for my formatting failures. –  Olivier Drolet Oct 19 '11 at 19:51
If you need to modify your original question, click on the edit and add the relevant code, rather than posting uncommented code in a comment. Once you're done, clean up the comments section be removing the non-relevant ones. –  Werner Oct 19 '11 at 20:03
The latex code initially appeared mangled in my post. That's why I commented in order to fix it (I'll do what you suggest, from now on). However, I see that the code is now properly formatted, I just don't know why or how to ensure that it will be in future posts. –  Olivier Drolet Oct 19 '11 at 20:10
If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). You can use backticks  to mark your inline code‌​. The latter also holds for comments, as you already know. –  Werner Oct 19 '11 at 20:12

Simply moving \counterwithin* after \hypersetup seems to solve the problem:

\usepackage{hyperref}
\hypersetup{
hypertexnames=true,
`