# Write LaTeX section title to an additional file

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}
\begin{ans}{One.I.1.17}
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.

\renewcommand{\subsection}{\@startsection
{subsection}{2}{0em}{-10ex plus1ex minus1ex}{1em}%
{\raggedright\usefont{T1}{fvs}{b}{n}\large\SetSectioningName}}

\def\SetSectioningName#1{\gdef\sectioningname{#1}%
\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?

-

Probably the easiest thing would be to redefine \addcontentsline as all the sectioning commands pretty much do something like
\addcontentsline{toc}{chapter}{#1}

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