I try to make a custom list of answers as instructed by section 2.3 of The LaTeX Companion, 2nd edition.

I write this code to define my custom list:

\newcommand{\exerciseref}[1]{\hyperref[exercise#1]{Exercise #1}}
\newcommand{\declareexercise}[1]{\paragraph{Exercise #1} \label{exercise#1}}



The command \declareexercise introduces a label referring to an answer of an exercise, and the command \exerciseref uses \hyperref to refer to a label created by '\declareexercise'.

According to the companion, I firstly define the command \l@answer, which uses \@dottedtocline to define the look of an answer appearing in the list. Then, I define the command \addanswer, which instructs LaTeX to write a contents line to the file \jobname.ans. Finally, I define the command \listofanswers, which starts the list by calling \@starttoc.

Then inside my file, I introduce the list somewhere at the beginning of the document, and I add the answers to the list at the end of the document, like this:

\listofanswers
some texts and some declaration of answers using \declareexercise...


To my surprise, the result list only had a title introduced by the \section*{Answers}. And when I checked the .ans file, I found it empty. I am sure that I ran LaTeX more than twice, and the table of contents was correct.

What was wrong?

• show a complete example that can be tested. Nobody can know from your snippets what you are doing wrong. Aug 11, 2021 at 7:30

Everything works fine.

Have you checked content of .aux file? It should contain line like

\@writefile{ans}{\contentsline ...

Have you checked that your .ans file is not locked for writing?

Also read log file, may be there some notes.

• Thank you. I found that the file was not written for a mistake I made. See my answer. Aug 11, 2021 at 11:21
• I'v got no enough score to comment you amswer, so I reply here. File .ans was epmty due to second problem. Aug 11, 2021 at 11:30
• Continue. "Level" is work because uoy have defined l@answer, other macros which uses snwer are set to \relax. That is why there is no errer message. Aug 11, 2021 at 11:39

I found two problems.

One problem: The level used in the command \addcontentsline should not be "answer", since I do not define it. Otherwise, LaTeX would write section*2 to the command \contentsline. However, I don't quite understand why the undefined level "example" works fine in the companion's example:

\newcommand\ecaption[1]{\addcontentsline{xmp}{example}{#1}}
\makeatletter