0

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}}

\newcommand\l@answer[2]{\@dottedtocline{1}{1.5em}{2.3em}{\exerciseref{#1}}{\pageref{#1}}}
\newcommand\addanswer[1]{\addcontentsline{ans}{answer}{#1}}
\newcommand\listofanswers{\section*{Answers}\@starttoc{ans}}

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...
\addanswer...

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?

1
  • 3
    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

2 Answers 2

1

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.

3
  • 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.
    – ElGatoGris
    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.
    – ElGatoGris
    Aug 11, 2021 at 11:39
0

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 
\newcommand\listofexamples{\section*{Comments}\@starttoc{xmp}}
\newcommand\l@example[2]{\par\noindent#1,~\textit{#2}\par} 
\makeatother

The other problem was a mistake. I misspelled a letter in the name of the pagestyle I used to create the list. So somehow LaTeX ignored the whole page, and didn't write anything to the .ans file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.