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}{%
\chapter{Title}
\section{Introduction}
\input chapter\recurselevel_introduction
\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.

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