[NTG-context] Custom Table of Contents

Hans Hagen j.hagen at xs4all.nl
Thu Jan 20 17:39:16 CET 2022


On 1/20/2022 3:45 PM, andrei--- via ntg-context wrote:
> Hi! I’m new to ConTeXt.
> 
> I need to make a custom table of contents that looks like this:
> 
> https://imgur.com/a/Ca3By6X <https://imgur.com/a/Ca3By6X>
> 
> Each row in the table corresponds to an item that has following data to 
> print in the table of contents: page, total number of pages which the 
> item spans, name of the item and a custom text metadata field which is 
> defined by the item. Each item will have its own regular table of 
> content and own structure. So we can think of this TOC as kind of a mega 
> table of content one level above.
> 
> It needs to be fully automatic.
> 
> The actual table is going to be anxtable(which I was blown away by after 
> using LaTeX godawful tables for years, it’s soo good).
> 
> After digging into the documentation (again, wow, it’s soo detailed and 
> approachable after LaTeX😂), I found out that\completecontentdoesn't 
> provide such extreme level of customization. Therefore I need 
> reimplement at least some parts of the default content forming 
> mechanism. After digging in the source a bit, I found something 
> relevant: 
> https://source.contextgarden.net/tex/context/base/mkxl/strc-lst.mklx 
> <https://source.contextgarden.net/tex/context/base/mkxl/strc-lst.mklx>.

there are some examples in the test suite ... if you look into the 
stc-lst files you'll see that one has access to fields and can use setups

> What I need is some guidance on what would be the best course of action, 
> in particular, which API should I use and roughly how should I approach it🙏
> 
> Should I create my own counters for each item and somehow reference 
> those manually in the TOC?
> 
> How would I save additional text metadata field and the item name to 
> show it in the TOC?
> 
> The source code mentions a concept of list renderers. This sounds like 
> the correct API to plug in, but it’s hard for me to grasp how exactly it 
> works.

The problem with tables is that they are multipass and store 
(unexpanded) data so it has not enough info. Attached is an example of 
how to circumvent this (the helpers can be found in the lua file).

(You can then try to add it to the wiki so that it gets documented. The 
lua-context interface wrt TABLE can do the same from the lua end as in 
tex. There are examples of that.)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
-------------- next part --------------
\definelistalternative
  [whatever]
  [renderingsetup=whatever]

\setuplist
  [chapter]
  [alternative=whatever]

\startsetups whatever
    \framed [offset=overlay,frame=off] {
        \framed [width=25mm,align=flushleft,frame=off,offset=0pt] {
            number: \currentlistentrynumber
        }
        \framed [width=4cm,align=normal,frame=off,offset=0pt] {
            title: \currentlistentrytitle
        }
        \framed [width=2cm,align=flushleft,frame=off,offset=0pt] {
            page: \currentlistentrypagenumber
        }
        \framed [width=4cm,align=normal,frame=off,offset=0pt] {
            author: \structurelistuservariable{more}
        }
    }
\stopsetups

\starttext

\starttitle[title={Tabulated Table of Contents}]
    \placelist[chapter]
    \blank[2*big]
    \determinelistcharacteristics[chapter]
    \startluacode
        local result = structures.lists.result
        context.bTABLE()
            for i=1,#result do
                local entry = result[i]
                inspect(entry)
                context.bTR()
                    context.bTD()
                        structures.lists.number(entry.metadata.name,i)
                    context.eTD()
                    context.bTD()
                     -- context(entry.titledata.title)
                        structures.lists.title(entry.metadata.name,i)
                    context.eTD()
                    context.bTD()
                        context(entry.userdata.more)
                    context.eTD()
                context.eTR()
            end
        context.eTABLE()
    \stopluacode
\stoptitle

\startchapter[title=One]  [more=ONE]   \samplefile{ward} \stopchapter
\startchapter[title=Two]  [more=TWO]   \samplefile{ward} \stopchapter
\startchapter[title=Three][more=THREE] \samplefile{ward} \stopchapter

\stoptext


More information about the ntg-context mailing list