After cleaning up my project and compiling a tex file 10 times in a row:

rm temp.toc temp.pdf temp.out temp.html temp.aux
xelatex temp.tex # executed 10 times!


I still get bad references with hyperref. In the table of contents, I see the hyperlinks. The page number in the TOC is good. But the reference points to a different page.

I cannot provide a minimal working example, because the problem only occurs with long documents. (There is no point in making a table of contents for a two page article at all.) Upon request, I can upload the whole document, but that might not be necessary.

UPDATE: The problem is still there. I have uploaded the complete example here: https://rapidshare.com/files/1197222879/output.zip - contains all tex files, aux files and the generated final pdf file. Engine is xelatex. I have executed the compilation 10 times but the hyperrefs are still wrong. In the table of contents, when you click on a link that says "page 25" then you are directed to page 23. In other words, the page numbers in the TOC are correct, but the hyperref references are incorrect.

UPDATE: This is a bug. If I use \usepackage[hungarian]{babel} and \usepackage{hyperref} together, them the references are wrong. If I remove hungarian babel, then references become good (but then hypenation is wrong, and paragraphs look awful)

-
How are you setting the bookmark to the references? Please, add only that small part, mentioning the class you're using. – egreg Oct 5 '12 at 12:38
I do not explicitly add any references. I'm just creating some tables, adding some footnotes and using sections and subsections. E.g. there are no named anchors at all. The document is the basic "article" class: \documentclass[a4paper,11pt]{article} and I have \usepackage{hyperref} just right before \begin{document}. – nagylzs Oct 5 '12 at 14:03
What are the "wrong references" you mention? To what do they refer? – egreg Oct 5 '12 at 14:23
For example, the "table of contents" has a link to a page. It says that it is on page 8. But the link brings me to page 6. Page 8 is correct. – nagylzs Oct 5 '12 at 14:29
I'm on a Linux box now. I'm going to try this with the windows version too. Within 2 hours. – nagylzs Oct 5 '12 at 14:32

The main problem in your case is that the current version of magyar.ldf which is loaded with

\usepackage[hungarian]{babel}


is broken with respect to the interaction with hyperref.

There is a different version available on TeX Live systems, located in

/usr/local/texlive/2012/texmf-dist/doc/latex/magyar/magyar.ldf


You can copy it in the working directory or in a folder

~/texmf/tex/latex/magyar


(~/Library/texmf/tex/latex/magyar/ with MacTeX on Mac OS X) that you need to create.

Then call babel as

\def\magyarOptions{suggestions=no}
\usepackage[magyar]{babel}


The first line is just to avoid spurious and almost meaningless messages.

It's not the official version of magyar.ldf, but it seems to work.

On the other hand, the Polyglossia module for Hungarian seems to work, so you can consider, when using XeLaTeX, to call

\usepackage{polyglossia}
\setmainlanguage{magyar}

-
On windows: texlive\2012\texmf-dist\doc\latex\magyar\magyar.ldf – nagylzs Oct 8 '12 at 16:44

Ninety percent (more ore less) are covered by this kind of example (that is already the cure):

\cleardoublepage % new page to move the anchor to the right page
\phantomsection % anchor setting
\printindex


Of course, you have adopt it to your needs, chapter instead of section, bibliography instead of index, …

\cleardoublepage
\phantomsection
\tableofcontents


\cleardoublepage
\pdfbookmark[1]{\contentsname}{toc}% for section level
% \pdfbookmark[0]{\contentsname}{toc}% for chapter level
\tableofcontents

-
I'm not using any of these. In fact I dont know what they mean. Where should I put them? – nagylzs Oct 5 '12 at 14:05
At least I have tried an answer without knowing what you are doing, which class you are using, which bookmarks have wrong links, which versions you are using, ... – Heiko Oberdiek Oct 5 '12 at 15:01
Okay, then where should I put these lines? Is it before \begin{document} or after?  ! Undefined control sequence. l.21 \addcontensline {toc}{section}{\indexname}% bookmark with latest anchor  – nagylzs Oct 5 '12 at 15:05
After \begin{docment}. – Heiko Oberdiek Oct 5 '12 at 15:06

There is a workaround using the official magyar.ldf, just include the following macro (which I got from an old Debian teTex mailing list) in your preamble.

\makeatletter
\let\Hy@magyar@saved@refstepcounter\refstepcounter
\let\H@refstepcounter\refstepcounter
\let\refstepcounter\Hy@magyar@saved@refstepcounter
\expandafter\renewcommand\expandafter*\expandafter\@ssect
\expandafter[\expandafter 5\expandafter]\expandafter{%
\expandafter\def\expandafter\@currentlabelname\expandafter{%
\expandafter #\expandafter 5\expandafter
}%
\@ssect{#1}{#2}{#3}{#4}{#5}%
\phantomsection
}%
\expandafter\Hy@magyar@patch@sect\expandafter{%
\@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{#8}%
}{#1#2#3#4#5#6[#7]#8}{#2}{#7}%
}
\def\Hy@magyar@patch@sect#1#2#3#4{%
\def\@sect#2{%
\setcounter{section@level}{#3}%
\def\@currentlabelname{#4}%
\ifnum #3>\c@secnumdepth