# Compatibility of hypertexnames=false and indexes

I would like to use hyperref with hypertexnames=false. This leads to a problem with indexes: The correct pages are printed, but they lead to the wrong location. MWE:

\documentclass[index=totoc]{scrbook}
\usepackage{fontspec}
\usepackage{lipsum}

\usepackage{makeidx}
\makeindex

\usepackage[hypertexnames=false]{hyperref}

\begin{document}

\frontmatter

\title{Title}
\maketitle

\mainmatter

\chapter{Chapter 1}
\index{BeginningChapter1}\lipsum\index{EndChapter1}

\chapter{Chapter 2}
\index{BeginningChapter2}\lipsum\index{EndChapter2}

\printindex
\end{document}


It is clear from the documentation that hyperref has problems with the page anchors. By removing hypertexnames=false the problem is solved in the MWE, but in a large 500-page document I have relied heavily on this option to work (e.g. minitocs, KOMA-Script \addchap etc.).

Is it possible to make hypertexnames=false compatible with indexes? I have two guesses, but couldn’t evaluate/implement them:

1. Would it be possible to set up the relative page counter for the link (roman numerals for frontmatter, arabic numerals for the mainmatter, possibly Roman numerals for a backmatter), whilst retaining the functionalies of hypertexnames=false for the sectioning-commands?
2. Is there an option to activate hypertexnames=false after \frontmatter?

You could try this, but it is not much tested. You will have to check if links to pages do the right thing.

\documentclass[index=totoc]{scrbook}
\usepackage{lipsum}
\usepackage{makeidx}
\makeindex

\usepackage[hypertexnames=false]{hyperref}

\makeatletter
\usepackage{etoolbox}
\patchcmd\Hy@EveryPageBoxHook{\Hy@EveryPageAnchor}{\Hy@hypertexnamestrue\Hy@EveryPageAnchor}{}{\fail}
\makeatother

\begin{document}

\frontmatter

\title{Title}
\maketitle

\mainmatter

\chapter{Chapter 1}
\label{chap1}
\index{BeginningChapter1}\lipsum\index{EndChapter1}

\chapter{Chapter 2}
\index{BeginningChapter2}\lipsum\index{EndChapter2}
\pageref{chap1}
\printindex
\end{document}

• Wow, it worked on my bigger document; I’ll have a close look at the links after I’ve completed the index. Thanks!
– Kubo
Nov 13 '19 at 18:29