2022.2

Table Of Contents
Notethattheinfo()functioncanalsobeusedtogetanelement'ssheetnumber,thesectionitislocated
in,andthetotalpagecountandsheetcountofthatsection(see"PaginationInfo"onpage843and"info
()"onpage847).Inthiscaseonlythepagenumberisused.
Thenthecallbackfunctionaddsanentrytothevariablethatholdsthetableofcontents,usingthe
retrievedinfo.
toc += '<p class="toc-entry ' + level + '">';
toc += '<span class="text">' + text + '</span>';
toc += '<span class="dots"></span>';
toc += '<span class="number">' + pageNo + '</span></p>';
TheHTMLtagnameisaddedasaclass.ThiscanbeusedinaCSSfiletostyletheentriesinthetable
ofcontentsaccordingtotheirlevel.(See"Step4:Stylingthetableofcontents"onpage415.)
Theemptyspanbetweentheheading'stextandpagenumberhastheclassdots.Thisisusedtoput
dotsbetweenheadingandpagenumber.
Thenumberclass(forthepagenumber)isnotonlyusedinCSS,butalsolateroninthescript.
Thetableofcontentsisinsertedinthesectionwith:results.html(toc);(see"html()"onpage801).
Thetableofcontentsmaygettoolongforasinglepageandaffectthepagenumbersinothersections.
Inthatcaseitisnecessarytore-paginatethecontent;merge.section.paginate();doesthetrick.
Note: Whetherpagenumberingrestartsineachsection,dependsonthesettingsforpagenum-
bering;see"Configuringpagenumbers"onpage137.Bydefault,pagenumberingstartswith
page1foreachsection.
Ifthepaginationprocesshaschangedthepagenumbers,theTOCneedstobeupdatedaswell.
Todothat,thescriptfirsthastocollectthepagenumbersfromthetableofcontents.Thisiswherethe
numberclasscomesinhandy:var$numbers=query('.number');.
Notethatthisquery()function,asopposedtomerge.context.query(),onlysearchesthecurrentsection
(see"query()"onpage742).
Then,thelevel1and2headingsarecollectedfromallPrintsectionsagainusingmerge.context.query
("h1,h2").
Thecallbackfunctionineach()retrievestheheading'snewpagenumber.Itthenusestheindexnum-
beroftheheadingintheresultsettogetthecorrespondingentryintheTOC:varentry=$numbers.get(
index);(see"get(index)"onpage766),andreplacesitwiththenewpagenumber.
Excluding headings
Oftentherearecertainheadingsthatyoudon'twanttoappearinthetableofcontents.Thetitleofthe
tableofcontentsitself,forexample.
Toexcludetheseheadingsfromthetableofcontents,dothefollowing:
Page 413