hyperref, toc: Toc links incorrect due maybe to counter reset

Here is my MWE:

\documentclass[11pt,a4paper,oneside]{memoir}

\pagestyle{plain}

\usepackage[explicit]{titlesec}

\usepackage{fontspec}
\usepackage{xltxtra}
\usepackage{polyglossia}

\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}

%% Ensure sequential numbering of subsubsections.
\setsecnumdepth{paragraph}
\counterwithout{paragraph}{subsubsection}
\counterwithout{subsubsection}{subsection}
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\Roman{subsection}}
\renewcommand{\thesubsubsection}{\S\arabic{subsubsection}}
\setcounter{tocdepth}{3} % Must not precede the above

\titleformat{\paragraph}[hang]
{\raggedleft\normalfont\small\itshape\bfseries}{}{0pt}{#1
\arabic{paragraph}}

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter*{}
\section*{Beauty}
\label{sec:beauty}
\addcontentsline{toc}{section}{\nameref{sec:beauty}}

\section*{Preface}
\label{sec:preface}
\addcontentsline{toc}{section}{\nameref{sec:preface}}

\chapter*{First Grade}
\label{ch:firstgrade}
\addcontentsline{toc}{chapter}{\nameref{ch:firstgrade}}
\section*{Introduction}
\label{sec:intro}
\addcontentsline{toc}{section}{\nameref{sec:intro}}
\section{Outline}
\subsubsection{Charlie}
\paragraph{Exercise}
\paragraph{Exercise}
\subsubsection{Delta}
\section{Start}
\subsubsection{Continue}

\chapter*{Second Grade}
\label{ch:secondgrade}
\addcontentsline{toc}{chapter}{\nameref{ch:secondgrade}}

\setcounter{section}{0}
\setcounter{subsubsection}{0}

\section{Declension}
\subsubsection{Echo}
\paragraph{Exercise}
\paragraph{Exercise}

\end{document}


The document structure is a bit unusual (it starts on section level, no first chapter specified, and subsubsection numbers run continuously through a chapter), but it is not for me to modify. There are two troubles:

(1) Echo ToC entry points to Charlie, and Declension to Outline (intuitive, but still wrong);

(2) Beauty, Preface, and Introduction ToC entries appear nameless and linkless, though First Grade and Second Grade chapter entries are OK.

Hope there's a way to put things to rights.

• titlesec and memoir? – user31729 Apr 5 '17 at 11:54
• Yes. \paragraph title is customized with \titleformat, which requires titlesec – Alexey Orlov Apr 5 '17 at 12:11
• Well, memoir comes with its own facilities of making structure command format, no need to screw it up with titlesec ;-) – user31729 Apr 5 '17 at 12:12
• That powerful? Do you care to give me a hint? Preferably in the form of modifying my MWE :) . – Alexey Orlov Apr 5 '17 at 12:24
• Please test if the example shows the same behaviour without the special fonts and xelatex. If they are not needed to reproduce the issue, remove them from the MWE. – Johannes_B Apr 8 '17 at 7:52

Bookmarks

There are jumps in the section hierarchy:

\section{Outline}
\subsubsection{Charlie}
...
\section{Start}
\subsubsection{Continue}


The level subsection is missing. This is not handled well by hyperref, but managed by the redesign of the algorithm in package bookmark. The package should be added after package hyperref, see the example below.

Unique anchor names

The trouble with the links is caused by anchor names that are not unique. This is caused by the counter reset. Both counters subsection and subsubsection are reusing its numbers. But hyperref needs unique numbers to generate the anchor names to generate the link destinations.

Uniqueness can be provided by a proper definition of the companion macros \theH<counter> that are used by hyperref if they are available. The following example adds a macro \ChapterAnchorPrefix to be inserted into the definitions of \theH<counter> for section and subsection. If a counter is reset, then \ChapterAnchorPrefix must be assigned a new value.

Example (without titlesec stuff):

\documentclass[11pt,a4paper,oneside]{memoir}

%% Ensure sequential numbering of subsubsections.
\setsecnumdepth{paragraph}
\counterwithout{paragraph}{subsubsection}
\counterwithout{subsubsection}{subsection}
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\Roman{subsection}}
\renewcommand{\thesubsubsection}{\S\arabic{subsubsection}}
\setcounter{tocdepth}{3} % Must not precede the above

\usepackage{hyperref}
\usepackage{bookmark}

\newcommand*{\ChapterAnchorPrefix}{chapter}
\def\theHsection{\ChapterAnchorPrefix.\the\value{section}}
\def\theHsubsubsection{\ChapterAnchorPrefix.\the\value{subsubsection}}

\begin{document}

\tableofcontents

\chapter*{}
\section*{Beauty}
\label{sec:beauty}
\addcontentsline{toc}{section}{\nameref*{sec:beauty}}

\section*{Preface}
\label{sec:preface}
\addcontentsline{toc}{section}{\nameref*{sec:preface}}

\chapter*{First Grade}
\label{ch:firstgrade}
\addcontentsline{toc}{chapter}{\nameref*{ch:firstgrade}}
\section*{Introduction}
\label{sec:intro}
\addcontentsline{toc}{section}{\nameref*{sec:intro}}
\section{Outline}
\subsubsection{Charlie}
\paragraph{Exercise}
\paragraph{Exercise}
\subsubsection{Delta}
\section{Start}
\subsubsection{Continue}

\chapter*{Second Grade}
\label{ch:secondgrade}
\addcontentsline{toc}{chapter}{\nameref*{ch:secondgrade}}

\renewcommand*{\ChapterAnchorPrefix}{2G}
\setcounter{section}{0}
\setcounter{subsubsection}{0}

\section{Declension}
\subsubsection{Echo}
\paragraph{Exercise}
\paragraph{Exercise}

\end{document}

• Thank you very much, Heiko! It works perfectly. The 'titlesec stuff' is messing things up, indeed. What can I replace it with? I really need to make 10 Exercise into Exercise 10 ragged left (flushed right). Is there a clean way to do it using memoir? – Alexey Orlov Apr 8 '17 at 11:40