![]() |
Automatic content: tables of contents, indexes and glossaries | ![]() |
Tables of contents, indexes, glossaries, lists of figures and tables, and bibliographies are produced in two steps:
While the document is typeset, macros such as toc-entry, index, glossary or cite record entries with the write primitive. These entries are collected in the auxiliary data of the buffer.
On request (Document\(\rightarrow\)Update), the editor walks through the document, empties the body of every automatic section (table-of-contents, the-index, …) and fills it with content computed from the auxiliary data.
The generated content is ordinary document content: it is typeset like everything else, and is saved with the document. A second typesetting pass is needed afterwards, since the page numbers in the generated content are references.
The auxiliary data are kept in the hash tables aux and ref of the new_data_rep of the buffer (see buffers), which are shared by all views on the buffer and are saved in the auxiliary and references parts of the file (unless the save-aux variable of the document is false, get_save_aux).
Maps the name of a list (toc, idx, gly, bib, …; the names are given by variables like toc-prefix or index-prefix of
std-automatic.ts
) to a document of entries. It is filled by concater_rep::typeset_write (
Typeset/Concat/concat_active.cpp
), which evaluates the second argument of write, removes its labels and appends it to the list named by the first argument, but only when the typesetting is complete, that is, when the whole document is being typeset. At the start of such a pass, the typesetter replaces the table by a fresh one (
Typeset/Bridge/typesetter.cpp
), so that it always reflects the current document.
Maps labels to their value and page number; it is filled by label during typesetting and used to resolve reference and pageref.
The typesetting environment holds references to these tables (local_aux, local_ref) and, for documents which belong to a project, to the tables of the project buffer (global_aux, global_ref), so that a chapter can refer to labels of other chapters. The generators described below also read the project tables when the buffer has a project (buf->prj), so that a table of contents in the master document lists the entries of all chapters.
From Scheme, the tables are accessed with get-auxiliary, set-auxiliary, list-auxiliaries, get-reference, set-reference, list-references and find-references (
build-glue-editor.scm
); the variants with a star take a flag which selects the tables of the project.
Edit/Process/edit_process.cpp
) is exported as generate-all-aux (without argument) and generate-aux. It calls generate_aux_recursively on the body of the buffer, which looks for the automatic tags recognized by is_aux: bibliography and bibliography*, table-of-contents and table-of-contents*, the-index and the-index*, the-glossary and the-glossary*, list-of-figures and list-of-tables (with their exact arities). For each of them it assigns an empty document to the last argument, puts the cursor inside, and, if which is empty or equal to the name of the tag, calls the generator for the list named by the first argument; the generator inserts its result at the cursor with insert_tree. The traversal does not look inside automatic tags. At the end, init_update refreshes the document information of the editor.
generic/document-edit.scm
. This schedules, as many times as the preference document update times says (at most 5), a delayed command which for "all" regenerates all automatic content, empties the caches of included documents and pictures and retypesets the buffer; for "bibliography" does the same without the cache cleaning; for "buffer" only retypesets; and otherwise calls (generate-aux what). The command is run inside cursor-after, which restores the cursor afterwards. Since regeneration uses the auxiliary data of the previous typesetting pass, and the generated content changes page numbers, several updates may be needed before a document is stable.
std-automatic.ts
are (key ref) for an ordinary entry with a page reference, (key "" text) for an entry with a fixed text instead of a page, and (key how range entry ref) for the complex form, where how may be strong and range opens or closes a page range. The key is itself a tuple of one to several levels (entry, subentry, …). generate_index (idx)
computes a sort key for every entry (index_name): the letters, digits and spaces of each level, in lower case, levels separated by tabs, with a marker * inserted after the level which contains the first capital letter, so that capitalized variants are kept apart from the lower case ones; entries whose keys only differ by other characters are distinguished by a number (the table followup);
sorts the keys with the collation of the document language (std::locale through get_std_locale, or CompareStringEx on Windows);
groups the entries with the same key, and inserts missing parent entries for subentries (insert_recursively);
builds one line per key (make_entry), collecting the page references separated by commas, turning runs of consecutive pages into ranges, and joining explicit range starts and ends. The lines are calls of index-1, index-2, … (the number is the level), or of index+1, index+2, … which repeat all levels when the document variable index-break-style is recall; a starred variant is used for entries without pages.
Document\(\rightarrow\)Update\(\rightarrow\)Index and Document\(\rightarrow\)Update\(\rightarrow\)Glossary do not work. The menu (generic/document-menu.scm:124-125) calls (update-document "index") and (update-document "glossary"), which pass the strings "index" and "glossary" to generate_aux, while generate_aux_recursively compares them with the tag names "the-index" and "the-glossary" (Edit/Process/edit_process.cpp:613-618). Combined with the next item, the effect is that all automatic content of the document is emptied and none is regenerated.
generate_aux_recursively empties every automatic section (
edit_process.cpp:594
) before testing whether it matches which. A restricted update such as Document\(\rightarrow\)Update\(\rightarrow\)Table of contents (or (generate-aux "bibliography")) therefore regenerates the requested section but leaves all the other ones empty.
The auxiliary data are only recorded during a complete typesetting of the document. If the document has never been typeset entirely in the current session (for instance because only some parts are shown, see show-part), or if the auxiliary data were not saved (save-aux), the generated content is empty until the document has been typeset once more.
The generators insert their result at the cursor and leave the cursor in the last regenerated section; callers which care should save and restore the cursor, as update-document does with cursor-after.