# Force \addtocontents to be placed in the correct order

I have something like this:

\tableofcontents*
\chapter{chap1}
\chapter{chap2}
\chapter{chap3}

and the ToC is produced like this

chap1
chap2
chap3
test

I was wondering how to force test to be placed in correct order (chap1, chap2, test, chap3).

-

## migrated from stackoverflow.comMay 14 '11 at 23:53

This question came from our site for professional and enthusiast programmers.

We need a minimal example. In a rough test this works as expected. –  egreg May 14 '11 at 23:57
Sorry, I know I should have included a minimal working sample but couldnt make one, the original one is a huge text and I cant include that. –  user5540 May 15 '11 at 0:59

The command \addtocontents is mainly intended to be used to enter formatting information (extra spacing, for example) not directly related to any particular actual line of contents of the ToC; if you want to introduce some text together with some information (such as the page number) and some formatting (such as the one used for a particuar sectional unit), then you might want to use \addcontentsline:

\documentclass{book}

\begin{document}

\tableofcontents
\chapter{chap1}
\chapter{chap2}
\chapter{chap3}

\end{document}

If used together with the hyperref package, a \phantomsection command should also be included to get the proper result for the hyperlinks in the table of contents and for the bookmarks.

EDIT: since you don't want the page number nor any special formatting for the text to be included, then you can use \addtocontents:

\documentclass{memoir}

\begin{document}

\tableofcontents
\chapter{chap1}
\chapter{chap2}