LaTeX

I was having a problem with LaTeX where \addcontentsline wasn’t correctly adding a bookmark entry in the resulting PDF. My original code was like this:

\cleardoublepage
\phantomsection

\bibliographystyle{thesis}
\bibliography{common/bibliography}

% finally, index
% necessary to add 'Index' to sidebar bookmarks
\cleardoublepage
\phantomsection
\printindex


With this code, the Bibliography was included as a bookmark entry, whereas the Index was not. I wanted a table of contents listed as the image to the right.

Strangely enough, if I added another \addcontentsline immediately after the Index, this contents line wouldn’t be printed either – unless there was some additional text following the command, then both contents line would be printed.

It turns out the problem is something to do with delayed output. Following these instructions, I could resolve this problem by defining a new command, \immediateaddcontentsline:

\newcommand\immediateaddcontentsline[3]{%
\begingroup
\let\origwrite\write
\def\write{\immediate\origwrite}%