Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have something like this:

\tableofcontents*
\chapter{chap1}
\chapter{chap2}
\addtocontents{toc}{test}
\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).

share|improve this question
 
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
add comment

migrated from stackoverflow.com May 14 '11 at 23:53

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

1 Answer

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}
\addcontentsline{toc}{chapter}{test}
\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}
\addtocontents{toc}{\bigskip text\par}
\chapter{chap3}

\end{document}
share|improve this answer
 
Thanks, that works. I have another question then. How would I remove dots and page number using \addcontentsline command? I use memoir class. With \addtocontents, I use \addtocontents{toc}{ \protect\noindent test \protect\par } but that wouldnt work with \addcontentsline. –  user5540 May 15 '11 at 0:59
 
@user5540: The information provided in this comment was crucial and you should have included it in your question. In this case, it would be better to use a \addcontents command; I'll update my answer with a nes example. –  Gonzalo Medina May 15 '11 at 1:31
 
originally I used \addtocontents, which caused the problem of incorrect order. I tried your updated code and the problem is still there :( Is there anyway to fix this? Thanks. –  user5540 May 15 '11 at 3:58
1  
@user5540: If you have a huge document, you are probably using the \include command to insert chapter. If that is the case, you have encountered one of the oldest bugs in Latex. To work around this, insert the \addtocontents as the first line of the file (in the example Chap 3) before the \chapter command. –  Danie Els May 15 '11 at 10:38
 
@Danie Els: Yes, I use \include and your trick fixes my problem. Thanks a lot :) –  user5540 May 15 '11 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
http://tex.stackexchange.com/questions/18253/force-addtocontents-to-be-placed-in-the-correct-order
tex.stackexchange.com
crawl=CC-MAIN-2013-48
55127
application/http; msgtype=response
sha1:T2P2PX7ABNOQHTN3A23QRUDRA3QQHXFL
<urn:uuid:6954088a-297f-452f-8913-58f5c6f607cc>
2013-12-12T05:12:36Z
198.252.206.16
sha1:BDQV6BTMY5OUEOJZVFBIGUZBXHMDPZYE
<urn:uuid:2d0a99db-ed9e-4a26-9743-d972dab42b38>
http://tex.stackexchange.com/questions/18253/force-addtocontents-to-be-placed-in-the-correct-order
response
<urn:uuid:bf10b978-901b-4e8b-9c1f-30554f5980ec>
robots: classic
hostname: ip-10-33-133-15.ec2.internal
software: Nutch 1.6 (CC)/CC WarcExport 1.0
isPartOf: CC-MAIN-2013-48
operator: CommonCrawl Admin
description: Wide crawl of the web with URLs provided by Blekko for Winter 2013
publisher: CommonCrawl
format: WARC File Format 1.0
conformsTo: http://bibnum.bnf.fr/WARC/WARC_ISO_28500_version1_latestdraft.pdf