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.

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 have a book that comes with an answer manual. So when I define a section or subsection I want to also write the name of that to the file containing the answers. Thus, I want to end with this type of thing in bookans.tex.

\subsection{One.I.1: Gauss' Method}
      We can perform Gauss' method can in different ways ..

(I write to that file using the answers package.) I see in the LaTeX source that the last thing in argument 6 to \@startsection, the style, can be a command that takes an argument, so I tried variations on this.

   {subsection}{2}{0em}{-10ex plus1ex minus1ex}{1em}%

  \typeout{SUBSECTION\space START:\space\sectioningname}}

But it does not get the name; it gets "\setbox \@tempboxa \hbox {{\hskip 0em\relax I.1\hskip 1em\relax \relax }}\hangindent \wd \@tempboxa \noindent \box \@tempboxa \interlinepenalty \@M Gauss' Method\@@par". Then, for instance, hyperref gets annoyed at the section title.

To write the Chapter name I modified \@chapter. Do I need to do the same torturous thing with the sectioning commands (maybe \@sect)? It seems perilous to me; for instance, what if the LaTeX3 folks change those definitions?

share|improve this question

Probably the easiest thing would be to redefine \addcontentsline as all the sectioning commands pretty much do something like


as long as tocdepth is set suitably. You could make that write to the toc and your additional file.

share|improve this answer
Thank you David. I take your comment to confirm that I haven't overlooked a natural way to do it. I use that command to do a number of things so I'll have to think about if I can modify it to this purpose. Thanks, Jim – Jim Hefferon Mar 10 '12 at 22:09
Just remember to redefine \addcontentsline before loading hyperref, because this package redefines it adding to the existent functionality. – egreg Mar 10 '12 at 22:37

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.