1

I am working on a textbook that has a repeated structure for 40 chapters. I set up the TeX file to use \dorecurse to provide that structure, then it uses \input to enter in the data into each section and subsection. Here is a simplified example:

\starttext
    \dorecurse{40}{%
        \input chapter\recurselevel_metadata
        \chapter{Title}
            \section{Introduction}
                 \input chapter\recurselevel_introduction
            \section{Reading}
                 \input chapter\recurselevel_reading
            \section{Exercises}
                 \input chapter\recurselevel_exercises
    }
\stoptext

The file "chapter1_introduction.tex", for example, contains the introduction text for chapter 1.

The problem I'm having is, though some data is stored in a metadata file, and it displays correctly in the chapter, it does not appear correctly in the table of contents.

I've created a minimal working example, no longer relying on input files, which showcases this error:

\setvalue{title1}{Animals}
\setvalue{title2}{Cereals}
\setvalue{title3}{Mountains}

\starttext

    \completecontent

    \dorecurse{3}{%

        \chapter{\getvalue{title\recurselevel}}
            \input knuth
    }

\stoptext

When compiled, the chapter titles appear correctly on the pages \chapter appears on, however, the \completecontent cannot get the values. Note, I also tried simply using \define instead of \getvalue, but I see the same error.

In my actual case, the chapter1_metadata file contains \define\chaptertitleis{Animals} and then \chapter is defined with \chapter{\chaptertitleis}, and \input chapter\recurselevel_metadata is called before \chapter{\chaptertitleis} is in the \dorecurse loop.

Why aren't my table of contents rendering correctly and how can I correct this problem?

0

1 Answer 1

2

\recurselevel is a global counter, so you may have expansion issues. An easy way is using hashes instead:

\setvalue{title1}{Animals}
\setvalue{title2}{Cereals}
\setvalue{title3}{Mountains}
\starttext
\completecontent
\dorecurse{3}{\chapter{\getvalue{title#1}}\input{knuth}}
\stoptext

but nesting issues will eventually appear. A more robust way implies enabling expansion:

\setvalue{title1}{Animals}
\setvalue{title2}{Cereals}
\setvalue{title3}{Mountains}
\setuphead[chapter][expansion=yes]
\starttext
\completecontent
\dorecurse{3}{\chapter{\getvalue{title\recurselevel}}\input{knuth}}
\stoptext

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.