5

I have a latex document. I am using hyperref, makeidx and glossary packages for my document.

Every thing is created fine; table of content (all references works nicely), glossary and index except that page numbers printed in the glossary and index are correct but they point to page numbers starting from the beginning of the document where initial 10 pages are in arabic numbers and then roman numbers from 1 starts.

e.g. I have 10 pages for initial front matter (abstract, declaration, table of contents etc etc). After that, mainmatter begins and so does the page numbers in roman from 1. So on this page 1, I have an index entry "hello"

Now in the index, it prints "hello 1" which is correct except that when one clicks on 1, then it goes to the right at the beginning of the document rather then numbered page 1.

Your help would be much appreciated.

Thanks, Omer

okm
  • 283
  • 6
  • 18
  • This is a new one by me. Are you using a standard class (article, book, report...) or a custom one? – dmckee --- ex-moderator kitten Sep 21 '09 at 17:09
  • A solution using \phantomsection is here: http://stackoverflow.com/questions/782187/latex-table-of-contents-links-to-wrong-section Regards! –  Jul 04 '13 at 16:37
  • Related question: https://tex.stackexchange.com/questions/516267/compatibility-of-hypertexnames-false-and-indexes – Albert Jul 06 '20 at 22:37

6 Answers6

4

OK, googlefu(*) yields the TeX FAQ entry titled Hyperref and repeated page numbers where it says that this is a known problem, and suggests passing two options to the hyperref package:

  • plainpages=false
  • pdfpagelabels

though it warns that these will not work in every situation (but I believe it will in yours). The application would look something like:

\usepackage[plainpages=false,pdfpagelabels,pagebackref]{hyperref}

in your preamble.

Failing that this Wiki article provides an even clunkier trick which will work in places where the above fails. (Scroll down to the "Problems with Links" section.)

(*) Turns out I found this stuff due to a typo, and asked google for "hyperef frontmatter" rather than "hyperref frontmatter". Some days you just get lucky.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • I am trying out...plainpages=fasle didn't work I am not sure how to use pdfpagelables and where?? – okm Sep 21 '09 at 17:52
  • Here: `\usepackage[pdfpagelabels]{hyperref}`. – Thomas Sep 21 '09 at 17:57
  • I have tried all the options including "Problems with links" but it didn't work.. – okm Sep 21 '09 at 18:02
  • I guess I'm out of ideas then. Sorry. – dmckee --- ex-moderator kitten Sep 21 '09 at 18:04
  • Considering that TOC works perfectly...I am wondering if the problem is with hyperref. Since \makeindex and \makeglossary are called in the frontmatter section, I think this creates wrong linking although (strangely) page numbers printed correctly in the index and glossary. – okm Sep 21 '09 at 19:42
2

Ok this question is old, but I encountered the same problem and had to find a solution. The solution involves a bit of tex hacking and is not generic but you should be able to adapt it to your particular case.

The problem

The index package generates links using the \hyperpage macro, which takes only one argument: it will print this number and link to the page that has this absolute number.

Ad-hoc solution

Saving the number of pages in the front matter in a macro, then shifting all page anchors by that same number.

In practice

This solution is fragile since if the code of your book style or the hyperref packages changes, it will not work anymore. Still this is a working solution for me. I modified the \frontmatter macro in my book style:

(New lines added are lines 2 and 3, note that we need to decrement by 1 as the computation occurs on the new page numbered '1')

 \newcommand\mainmatter{\clearemptydoublepage
    \count0=\value{page}\advance\count0 by -1
    \xdef\pagesfrontmatter{\the\count0}
    \@mainmattertrue\pagenumbering{arabic}}

Then comes a trickier part. The \hyperpage embeds a lot of code, and can handle for instance arguments like '2,4' or '3-5', so we need to modify the end macro that actually outputs the text and hyperlink. If you look at hyperref.sty, you will find it named \HyInd@removespaces. We need to redefine it only for the index so that each link has the same text but the anchor is shifted by the right amount (modified lines are those including references to \count0):

{ % open local group
% locally change how hyperpage creates hyperlinks to take
% frontmatter pages into account
\makeatletter
\def\HyInd@removespaces#1 #2\@nil{%
  \toks@=\expandafter{\the\toks@#1}%
  \ifx\\#2\\%
    \edef\x{\the\toks@}%
    \ifx\x\@empty
    \else
      \count0=\the\toks@\advance\count0 by \pagesfrontmatter%
      \hyperlink{page.\the\count0}{\the\toks@}%
    \fi
  \else
    \ltx@ReturnAfterFi{%
      \HyInd@removespaces#2\@nil
    }%
  \fi
}
\makeatother

\printindex
} % close local group
Sparshong
  • 417
  • 4
  • 9
1

I wrote a blog post about this a while ago, explaining how to simply number your pages 1...n, getting rid of the Roman numerals. This solves your problem, and makes for sensible page numbering within the PDF reader as well. If this is what you want, put the following code in your preamble:

\let\oldsetcounter=\setcounter
\renewcommand\setcounter[2]{%
  \ifx\not{#1}{page}\oldsetcounter{#1}{#2}\fi}
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Thanks Thomas...But i need arabic and roman numbers for my document for different sections. – okm Sep 21 '09 at 17:49
0

i am not sure if this will work at all, but have you tried adding

\frontmatter

\mainmatter

\backmatter

in the appropriate places?

note that this only applies to the book & book-like classes, not article, letter, etc.

Mica
  • 18,501
  • 6
  • 46
  • 43
  • Yes! That is all working. It's not an issue of frontmatter, mainmatter and backmatter; if you read my first message I described that every thing is fine except the hyperrefs in the glossary and index which print pages correctly but links wrongly. – okm Sep 21 '09 at 19:33
  • sounds like a page counter problem. have you tried setting a counter at the beginning of \frontmatter, then another counter at \mainmatter? – Mica Sep 22 '09 at 16:08
0

This is a dirty trick, but it works

\newcounter{glshyperpage}%
\def\glshyper#1#2{%
\setcounter{glshyperpage}{#2}%
\addtocounter{glshyperpage}{6}%number of front-matter pages
(\hyperlink{#1.\theglshyperpage}{#2})}
Javier
  • 1
  • Didn't work for me: Clicking on, say, `xvi` in the index sent me straight to the very first page of the book. But, at least, didn't break anything. – schremmer Apr 23 '23 at 20:26
0

Just put the option hypertextnames=true into your \usepackage{hyperref}, i.e. put something like

\usepackage[hypertexnames=true]{hyperref}

into your preamble.

ewi
  • 175
  • 1
  • 9
  • Worked! BUT. I had `hypertexnames=false` as per someone’s advice and had checked [https://tex.stackexchange.com/questions/3188/what-does-the-hyperref-option-hypertexnames-do] which I just re-read but which is still way beyond me. In any case, I now have `hypertexnames=true` as per your advice but, as I keep on trucking, I will keep an eye for unexpected happenings. Hence the above BUT for others to keep an eye too. – schremmer Apr 23 '23 at 20:47
  • @ewi Please don't remove the code highlighting. Your answer is much harder to read without it. – samcarter_is_at_topanswers.xyz Apr 27 '23 at 16:15