| 1 |
% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \maketoc {Macros for maketoc <2021-07-18>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ------------------------------------ |
| 6 |
\`\_Xtoc` `{<level>}{<type>}{<number>}{<o-title>}<title>` (in `.ref` file) reads |
| 7 |
given data and appends them to the \`\_toclist` as |
| 8 |
\^`\_tocline``{<level>}{<type>}{<number>}{<o-title>}{<title>}{<gpageno>}{<pageno>}` |
| 9 |
where: |
| 10 |
\begitems |
| 11 |
* `<level>`: 0 reserved, 1: chapter, 2: section, 3: subsection |
| 12 |
* `<type>`: the type of the level, i.e. chap, sec, secc |
| 13 |
* `<number>`: the number of the chapter/section/subsection in the format 1.2.3 |
| 14 |
* `<o-title>`: outlines title, if differs from `<title>`. |
| 15 |
* `<title>`: the title text |
| 16 |
* `<gpageno>`: the page number numbered from 1 independently of pagination |
| 17 |
* `<pageno>`: the page number used in the pagination |
| 18 |
\enditems |
| 19 |
The last two parameters are restored from previous |
| 20 |
\^`\_Xpage``{<pageno>}{<gpageno>}`, data were saved in the \^`\_currpage` macro. |
| 21 |
|
| 22 |
We read the <title> parameter by \^`\scantoeol` from `.ref` file |
| 23 |
because the <title> can include something like \code{`\{`}. |
| 24 |
\_cod ------------------------------------ |
| 25 |
|
| 26 |
\_def\_toclist{} |
| 27 |
\_newifi \_ifischap \_ischapfalse |
| 28 |
|
| 29 |
\_def\_Xtoc#1#2#3#4{\_ifnum#1=0 \_ischaptrue\_fi |
| 30 |
\_addto\_toclist{\_tocline{#1}{#2}{#3}{#4}}\_scantoeol\_XtocA} |
| 31 |
\_def\_XtocA#1{\_addto\_toclist{{#1}}\_ea\_addto\_ea\_toclist\_ea{\_currpage}} |
| 32 |
|
| 33 |
\_doc ------------------------------------ |
| 34 |
\`\_tocline``{<level>}{<type>}{<number>}{<o-title>}{<title>}{<gpageno>}{<pageno>}` |
| 35 |
prints the record to the table of contents. It opens group, reduces `\_leftskip`, |
| 36 |
`\_rightskip`, runs the \^`\everytocline` (user can customise the design of TOC |
| 37 |
here) and runs `\_tocl:<level> {<number>}{<title>}{<pageno>}` macro. |
| 38 |
This macro starts with vertical mode, |
| 39 |
inserts one record with given `<level>` and it should end by \^`\_tocpar` |
| 40 |
which returns to horizontal mode. The `\_tocpar` |
| 41 |
appends `\_nobreak \_hskip-2\_iindent\_null \_par`. This causes |
| 42 |
that the last line of the record is shifted outside the margin given by |
| 43 |
`\_rightskip`. A typical record (with long `<title>`) looks like this: |
| 44 |
\begtt \catcode`\<=13 |
| 45 |
| | |
| 46 |
\llap{<number>} text text text text text |
| 47 |
text text text text text |
| 48 |
text text .................... <pageno> |
| 49 |
\endtt |
| 50 |
Margins given by `\leftskip` and `\rightskip` are denoted by `|` in the |
| 51 |
example above. |
| 52 |
\nl |
| 53 |
\`\tocrefnum` is the global counter of all TOC records (used by hyperlinks). |
| 54 |
\_cod ------------------------------------ |
| 55 |
|
| 56 |
\_newcount \_tocrefnum |
| 57 |
\_def\_tocline#1#2#3#4#5#6#7{% |
| 58 |
\_advance\_tocrefnum by1 |
| 59 |
\_bgroup |
| 60 |
\_leftskip=\_iindent \_rightskip=2\_iindent |
| 61 |
\_ifischap \_advance\_leftskip by \_iindent \_fi |
| 62 |
\_def\_pgn##1{\_ilink[pg:#6]{\_numprint{##1}}}% |
| 63 |
\_the\_everytocline |
| 64 |
\_ifcsname _tocl:#1\_endcsname |
| 65 |
\_cs{_tocl:#1}{#3}{\_scantextokens{#5}}{#7}\_par |
| 66 |
\_fi |
| 67 |
\_egroup |
| 68 |
} |
| 69 |
\_public \tocrefnum ; |
| 70 |
|
| 71 |
\_doc ----------------------------------- |
| 72 |
You can re-define default macros for each level of tocline if you want.\nl |
| 73 |
Parameters are `{<number>}{<title>}{<pageno>}`. |
| 74 |
\_cod ----------------------------------- |
| 75 |
|
| 76 |
\_sdef{_tocl:1}#1#2#3{\_nofirst\_bigskip |
| 77 |
\_bf\_llaptoclink{#1}{#2}\_nobreak\_hfill \_pgn{#3}\_tocpar} |
| 78 |
\_sdef{_tocl:2}#1#2#3{\_llaptoclink{#1}{#2}\_tocdotfill \_pgn{#3}\_tocpar} |
| 79 |
\_sdef{_tocl:3}#1#2#3{\_advance\_leftskip by\_iindent \_cs{_tocl:2}{#1}{#2}{#3}} |
| 80 |
|
| 81 |
\_doc ----------------------------------- |
| 82 |
The auxiliary macros are: |
| 83 |
\begitems |
| 84 |
* \`\_llaptoclink``<text>` does `\_noindent\_llap{<linked text>}`. |
| 85 |
* \`\_tocdotfill` creates dots in the TOC. |
| 86 |
* \`\_nofirst``\macro` applies the `\macro` only if we don't print the first |
| 87 |
record of the TOC. |
| 88 |
* \`\_tocpar` finalizes one TOC record with rlapped `<pageno>`. |
| 89 |
* \`\_pgn``{<pageno>}` creates <pageno> as link to real `<gpage>` saved in `#6` |
| 90 |
of \^`\_tocline`. This is temporarily defined in the \^`\_tocline`. |
| 91 |
\enditems |
| 92 |
\_cod ---------------------------------- |
| 93 |
|
| 94 |
\_def\_llaptoclink#1{\_noindent |
| 95 |
\_llap{\_ilink[toc:\_the\_tocrefnum]{\_enspace\_numprint{#1}\_kern.4em}\_kern.1em}} |
| 96 |
\_def\_tocdotfill{\_nobreak\_leaders\_hbox to.8em{\_hss.\_hss}\_hskip 1em plus1fill\_relax} |
| 97 |
\_def\_nofirst #1{\_ifnum \_lastpenalty=11333 \_else #1\_fi} |
| 98 |
\_def\_tocpar{\_nobreak \_hskip-2\_iindent\_null \_par} |
| 99 |
|
| 100 |
\_doc ----------------------------------- |
| 101 |
If you want a special formatting of TOC with adding more |
| 102 |
special lines (no generated as titles from `\chap`, `\sec`, `\secc`), you |
| 103 |
can define `\addtotoc{<level>}{<type>}{<number>}{<o-title>}{<title>}` macro: |
| 104 |
\begtt |
| 105 |
\def\addtotoc#1#2#3#4#5{% |
| 106 |
\incr\_tocrefnum |
| 107 |
\_dest[toc:\_the\_tocrefnum]% |
| 108 |
\_ewref\_Xtoc{{#1}{#2}{#3}{#4}#5}% |
| 109 |
} |
| 110 |
\endtt |
| 111 |
and you can declare special lines (or something else) |
| 112 |
as an unused level (10 in the following example): |
| 113 |
\begtt |
| 114 |
\sdef{_tocl:10}#1#2#3{\medskip\hbox{\Blue #2}\medskip} |
| 115 |
\endtt |
| 116 |
Now, users can add a blue line into TOC by |
| 117 |
\begtt \catcode`\<=13 |
| 118 |
\addtotoc{10}{blue-line}{}{\relax}{<blue text to be added in the TOC>} |
| 119 |
\endtt |
| 120 |
anywhere in the document. Note that `\relax` in the fourth parameter |
| 121 |
means that outline will be not generated. And second parameter |
| 122 |
`blue-line` is only a comment (unused in macros). |
| 123 |
|
| 124 |
\`\maketoc` prints warning if TOC data is empty, else it creates TOC by |
| 125 |
running \^`\_toclist` |
| 126 |
\_cod ---------------------------------- |
| 127 |
|
| 128 |
\_def\_maketoc{\_par \_ifx\_toclist\_empty |
| 129 |
\_opwarning{\_noexpand\maketoc -- data unavailable, TeX me again}\_openref |
| 130 |
\_incr\_unresolvedrefs |
| 131 |
\_else \_begingroup |
| 132 |
\_tocrefnum=0 \_penalty11333 |
| 133 |
\_the\_regtoc \_toclist |
| 134 |
\_endgroup \_fi |
| 135 |
} |
| 136 |
|
| 137 |
\_doc ----------------------------------- |
| 138 |
\`\regmacro` appends its parameters to \`\_regtoc`, \`\_regmark` and |
| 139 |
\`\_regoul`. These token lists are used in \^`\maketoc`, \^`\_begoutput` and |
| 140 |
\^`\pdfunidef`. |
| 141 |
\_cod \_fin ----------------------------- |
| 142 |
|
| 143 |
\_newtoks \_regtoc \_newtoks \_regmark \_newtoks \_regoul |
| 144 |
|
| 145 |
\_def\_regmacro #1#2#3{% |
| 146 |
\_toksapp\_regtoc{#1}\_toksapp\_regmark{#2}\_toksapp\_regoul{#3}% |
| 147 |
} |
| 148 |
\_public \maketoc \regmacro ; |
| 149 |
|
| 150 |
\_endcode |
| 151 |
|
| 152 |
2021-07-18 _tocl:1, \nobreak added, bug fixed |
| 153 |
2021-02-09 \thisoutline implemented |
| 154 |
2020-04-23 \_tocpar introduced (incompatible change) |
| 155 |
2020-04-22 \_pg -> \_pgn (incompatible change) |
| 156 |
2020-03-12 introduced |