2

I setup a macro that very simply creates heading titles numbed "Puzzle 1", "Puzzle 2", etc.

\newcount\a\newcount\counter
\advance\a by 1

\define\puzzle{\chapter{Puzzle~\the\a}\advance\a by 1}

\starttext

    \completecontent
    \puzzle
    \puzzle
    \puzzle
    \puzzle
    \puzzle
    \puzzle

\stoptext

The numbering appears correctly within the document, but in the table of contents, it just shows the starting value of the counter.

How can I have the table of contents display the incrementing number correctly?

And additionally, what if I need to add an additional kind of chapter title, such as "New Vocabulary 1", "New Vocabulary 2" (also at the chapter level)? I tried adding this too, but the code for "Puzzle 1" and "New Vocabulary 1" caused weird effects, resulting in two chapter titles appearing at different parts of the document to appear smashed together, overlapping.

0

2 Answers 2

3
+450

Forget my former solution (it doesn't work if you need to use it with numbered heads anyway). Since you need something a bit different (it's nice to give a clearer example of what one needs right from the start ;)), the following can be done:

\definenumber[vocabularycounter][way=bytext,prefix=no]
\definenumber[puzzlecounter][way=bytext,prefix=no]
\setuphead[chapter][expansion=yes]
\setnumber[puzzlecounter][1]
\setnumber[vocabularycounter][1]
\defineexpandable\puzzle%
{\chapter{Puzzle \rawcountervalue[puzzlecounter]}\incrementnumber[puzzlecounter]}
\defineexpandable\vocabulary%
{\chapter{Vocabulary \rawcountervalue[vocabularycounter]}\incrementnumber[vocabularycounter]}
\starttext
\completecontent
\puzzle
\vocabulary
\puzzle
\vocabulary
\puzzle
\puzzle
\puzzle
\vocabulary
\puzzle
\stoptext

And you'll get the following in the TOC: enter image description here

Does it meet your requirements?

10
  • 1
    You can drop the conversion when you set a labeltext for \puzzle with \setuplabeltext[en][puzzle=Puzzle\nobreakspace]. You can apply the label now to the heading with \setuphead[puzzle][bodypartlabel=puzzle], to get the label in the table of content you need also \setuplist[puzzle][label=puzzle]. Jan 20, 2021 at 21:50
  • @WolfgangSchuster i missed the second part (how to apply a label to the TOC). Thank you very much!
    – user226564
    Jan 20, 2021 at 22:04
  • @JairoA.delRio This worked perfectly, but then when I discovered I need to use another kind in the document, with different name from puzzle, it won't show up. And regular \chapter{} doesn't show up either.
    – Village
    Jan 22, 2021 at 17:17
  • I've tried this, but it shows Puzzle + words from other Chapter smashed together in both the TOC and where the heading appears.
    – Village
    Jan 22, 2021 at 18:44
  • Updated. Sorry I didn't realize when making the document until later that I've need more than one of the custom titles.
    – Village
    Jan 22, 2021 at 19:11
5

Jaira has provided an alternative solution which is more elegant, but let me explain what was going wrong with your solution and how to fix it. If you open the .tuc file, you will see

utilitydata.structures.lists.collected={
 {
  ...
  ["titledata"]={
   ["label"]="chapter",
   ["title"]="Puzzle~\\the \\a ",
  },
 },
 },
}

As you can see, ConTeXt is literally storing Puzzle~\\the\\a as the title of the chapter. So, when you place the TOC using \completecontent, the same title gets copied into the TOC. When the TOC is typeset, the value of \a is equal to 1, so you see Puzzle 1 in the TOC.

Now that we understand what is happening, the fix is easy. Simply add

\setuphead[chapter][expansion=yes]

which instructs ConTeXt to expand the contents of the title before storing them in the .tuc file. In this case, the .tuc file contains:

utilitydata.structures.lists.collected={
 {
 ...
  ["titledata"]={
   ["label"]="chapter",
   ["title"]="Puzzle~1",
  },
 },
 ... 
  ["titledata"]={
   ["label"]="chapter",
   ["title"]="Puzzle~2",
  },
 },
}

And we get the correct TOC!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .