TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write the contents of an environment to the document and the toc. Here's a MWE hacked to look (almost) like what I need.






first goal first chapter
\addcontentsline{toc}{section}{first goal first chapter}
%\addtocontents{toc}{first goal first chapter} %% yields error message
second goal first chapter
\addcontentsline{toc}{section}{second goal first chapter}

Chapter contents here \ldots


first goal second chapter
\addcontentsline{toc}{section}{first goal second chapter}

Chapter contents here \ldots


That produces this toc:

the actual toc

What I'd like is this (with no hyperref links for the extra lines):

the desired toc

Problems I don't know how to solve:

  • Most important: how can I capture the write twice behavior in the environment definition so that I don't have to specify the extra toc line with cut and paste each time?

  • Secondary: Why does \addtocontents produce an error when commented in? (An answer to this might solve the entry formatting and link problem.)

I can use the xparse package to define the environment if that makes for an easier solution.

share|improve this question
The environ package gives you access to the body of the environment using a macro \BODY. You could use that to write the TOC line in the start or end code of the environment. – Scott H. Jun 21 '12 at 18:59
@ScottH. Works like a charm almost. Solves the double write problem, but the second goal incarnation in the first chapter in the document has an extra indentation not in my MWE. The addtocontents error continues. You can post this as an answer I'd vote up, but not accept quite yet. – Ethan Bolker Jun 21 '12 at 19:37
Edit your question and add the code you got with @ScottH. 's help so that others can build on it ;) – yo' Jun 21 '12 at 20:16
@tochecz egreg's answer below appeared before I saw this good idea, which I will remember. – Ethan Bolker Jun 22 '12 at 0:55
up vote 6 down vote accepted

When LaTeX is reading the table of contents file it expects specially formatted paragraphs. Here's a way to do what you want.




  \vskip \z@ \@plus \p@ 
  \leavevmode #1\nobreak\hfil\nobreak\null\par 



\goal{first goal first chapter}
\goal{second goal first chapter}


Chapter contents here \ldots

first goal second chapter


Chapter contents here \ldots


If the goal text is not long, perhaps the command form is preferable. Otherwise use goal*.

The macro \goal writes to the .toc file using \addtocontents, because hyperref patches \addcontentsline so that it creates a link. We write \l@goal{<goal text>} and then the macro \l@goal does essentially the same as \l@chapter, which is responsible for typesetting the chapter title in the ToC.

share|improve this answer

Your Answer


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.