| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \fnote {Footnotes, marginal notes OpTeX <2026-03-19>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
\`\_gfnotenum` is a counter which counts footnotes globally in the whole document.\nl |
| 7 |
\`\_lfnotenum` is a counter which counts footnotes at each chapter from one. |
| 8 |
It is used for local page footnote counters too.\nl |
| 9 |
\`\_ifpgfnote` says that footnote numbers are counted on each page from |
| 10 |
one. We need to run `\openref` in this case.\nl |
| 11 |
\`\fnotenum` is a macro that expands to footnote number counted in declared part.\nl |
| 12 |
\`\fnotenumchapters` declares footnotes numbered in each chapter from one |
| 13 |
(default), \`\fnotenumglobal` declares footnotes numbered in whole |
| 14 |
document from one and \`\fnotenumpages` declares footnotes numbered |
| 15 |
at each page from one. |
| 16 |
\_cod ----------------------------- |
| 17 |
|
| 18 |
\_newcount\_gfnotenum \_gfnotenum=0 |
| 19 |
\_newcount\_lfnotenum |
| 20 |
|
| 21 |
\_newifi \_ifpgfnote |
| 22 |
\_def \_fnotenumglobal {\_def\_fnotenum{\_the\_gfnotenum}\_pgfnotefalse} |
| 23 |
\_def \_fnotenumchapters {\_def\_fnotenum{\_the\_lfnotenum}\_pgfnotefalse} |
| 24 |
\_def \_fnotenumpages {\_def\_fnotenum{\_trycs{_fn:\_the\_gfnotenum}{?}}\_pgfnotetrue} |
| 25 |
\_fnotenumchapters % default are footnotes counted from one in each chapter |
| 26 |
\_def \fnotenum{\_fnotenum} |
| 27 |
\_public \fnotenumglobal \fnotenumchapters \fnotenumpages ; |
| 28 |
\_let \runningfnotes = \fnotenumglobal % for backward compatibility |
| 29 |
|
| 30 |
\_doc ----------------------------- |
| 31 |
The \`\_printfnotemark` prints the footnote mark. You can re-define this |
| 32 |
macro if you want another design of footnotes. For example |
| 33 |
\begtt |
| 34 |
\fnotenumpages |
| 35 |
\def \_printfnotemark {\ifcase 0\fnotenum\or |
| 36 |
*\or**\or***\or$^\mathbox{†}$\or$^\mathbox{‡}$\or$^\mathbox{††}$\fi} |
| 37 |
\endtt |
| 38 |
This code gives footnotes* and ** and*** and$^\mathbox{†}$ etc.\ |
| 39 |
and it supposes that there are no more than 6 footnotes at one page. |
| 40 |
|
| 41 |
If you want to distinguish between footnote marks in the text and in the front of |
| 42 |
the footnote itself, then you can define \`\_printfnotemarkA` and \`\_printfnotemarkB`. |
| 43 |
|
| 44 |
The \`\fnotelinks``<colorA><colorB>` implements the hyperlinked footnotes |
| 45 |
(from text to footnote and backward). |
| 46 |
\_cod ----------------------------- |
| 47 |
|
| 48 |
\_def \_printfnotemark {\_quitvmode\_hbox{$^{\_fnotenum}$}} % default footnote mark |
| 49 |
\_def \_printfnotemarkA {\_printfnotemark} % footnote marks used in text |
| 50 |
\_def \_printfnotemarkB {\_printfnotemark} % footnote marks used in front of footnotes |
| 51 |
|
| 52 |
\_def \_fnotelinks#1#2{% <inText color> <inFootnote color> |
| 53 |
\_def\_printfnotemarkA{\_link[fnt:\_the\_gfnotenum]{#1}{\_printfnotemark}% |
| 54 |
\_dest[fnf:\_the\_gfnotenum]}% |
| 55 |
\_def\_printfnotemarkB{\_link[fnf:\_the\_gfnotenum]{#2}{\_printfnotemark}% |
| 56 |
\_dest[fnt:\_the\_gfnotenum]}% |
| 57 |
} |
| 58 |
\public \fnotelinks ; |
| 59 |
|
| 60 |
\_doc ---------------------------- |
| 61 |
Each footnote saves the \`\_Xfnote` (without parameter) to the `.ref` |
| 62 |
file (if `\openref`). We can create the mapping from `<gfnotenum>` to`<pgfnotenum>` |
| 63 |
in the macro `\_fn:<fnotenum>`. Each \^`\_Xpage` macro sets the |
| 64 |
`\_lfnotenum` to zero. |
| 65 |
\_cod ---------------------------- |
| 66 |
|
| 67 |
\_def \_Xfnote {\_incr\_lfnotenum \_incr\_gfnotenum |
| 68 |
\_sxdef{_fn:\_the\_gfnotenum}{\_the\_lfnotenum}} |
| 69 |
|
| 70 |
\_doc ---------------------------- |
| 71 |
The \`\fnote` `{<text>}` macro is simple, \`\fnotemark` and \^`\fnotetext` |
| 72 |
does the real work. |
| 73 |
\_cod ---------------------------- |
| 74 |
|
| 75 |
\_protected\_def\_fnote{\_fnotemark1\_fnotetext} |
| 76 |
\_protected\_def\_fnotemark#1{{\_advance\_gfnotenum by#1\_advance\_lfnotenum by#1\_relax |
| 77 |
\_printfnotemarkA}} |
| 78 |
|
| 79 |
\_doc ---------------------------- |
| 80 |
The \`\fnotetext` calls \^`\_opfootnote` which is equivalent to plain \TeX/ |
| 81 |
\^`\vfootnote`. It creates new data to Insert \^`\footins`. The only |
| 82 |
difference is that we can propagate a macro parameter into the Insert group |
| 83 |
before the text is printed (see section \ref[output]). |
| 84 |
This propagated macro is \`\_fnset` which sets smaller fonts. |
| 85 |
|
| 86 |
Note that \^`\vfootnote` and \^`\_opfootnote` don't read the text as a |
| 87 |
parameter but during the normal horizontal mode. This is the reason why catcode |
| 88 |
changes (for example in-line verbatim) can be used here. |
| 89 |
\_cod ---------------------------- |
| 90 |
|
| 91 |
\_def\_fnotetext{\_incr\_gfnotenum \_incr\_lfnotenum % global increment |
| 92 |
\_ifpgfnote \_openref \_fi |
| 93 |
\_wref \_Xfnote{}% |
| 94 |
\_ifpgfnote \_ifcsname _fn:\_the\_gfnotenum \_endcsname \_else |
| 95 |
\_opwarning{unknown \_noexpand\fnote mark. TeX me again}% |
| 96 |
\_incr\_unresolvedrefs |
| 97 |
\_fi\_fi |
| 98 |
\_opfootnote\_fnset\_printfnotemarkB |
| 99 |
} |
| 100 |
\_def\_fnset{\_everypar={}\_scalemain \_typoscale[800/800]} |
| 101 |
|
| 102 |
\_public \fnote \fnotemark \fnotetext ; |
| 103 |
|
| 104 |
\_doc ----------------------------- |
| 105 |
By default \`\mnote``{<text>}` are in right margin at odd pages and they are in left |
| 106 |
margin at even pages. The `\mnote` macro saves its position to `.ref` |
| 107 |
file as \`\_Xmnote` without parameter. We define `\_mn:<mnotenum>` as |
| 108 |
`\right` or `\left` when the `.ref` file is read. |
| 109 |
The `\ifnum 0`$\le$`0#2` trick returns true if `<pageno>` has a numeric type and false |
| 110 |
if it is a non-numeric type (Roman numeral, for example). We prefer to use |
| 111 |
`<pageno>`, but only if it has the numeric type. We use `<gpageno>` in other cases. |
| 112 |
\_cod ----------------------------- |
| 113 |
|
| 114 |
\_newcount\_mnotenum \_mnotenum=0 % global counter of mnotes |
| 115 |
\_def \_Xmnote {\_incr\_mnotenum \_ea \_XmnoteA \_currpage} |
| 116 |
\_def \_XmnoteA #1#2{% #1=<gpageno> #2=<pageno> |
| 117 |
\_sxdef{_mn:\_the\_mnotenum}{\_ifodd\_numtype{#2}{#1} \_right \_else \_left \_fi}} |
| 118 |
\_def \_numtype #1#2{\_ifnum 0<0#1 #1\_else #2\_fi} |
| 119 |
|
| 120 |
\_doc ----------------------------- |
| 121 |
User can declare \`\fixmnotes``\left` or \^`\fixmnotes``\right`. It defines |
| 122 |
\`\_mnotesfixed` as `\_left` or `\_right` which declares the placement |
| 123 |
of all marginal notes and such declaration has a precedence. |
| 124 |
\_cod ----------------------------- |
| 125 |
|
| 126 |
\_def \_fixmnotes #1{\_edef\_mnotesfixed{\_cs{_\_csstring #1}}} |
| 127 |
\_public \fixmnotes ; |
| 128 |
|
| 129 |
\_doc ----------------------------- |
| 130 |
The \`\_mnoteD``{<text>}` macro sets the position of the marginal note. |
| 131 |
The outer box of marginal note has zero width and zero depth and it is appended |
| 132 |
after current line using `\vadjust` primitive or it is inverted to vertical mode |
| 133 |
as a box shifted down by `\parskip` and with `\vskip-\baselineskip` followed. |
| 134 |
\_cod ----------------------------- |
| 135 |
|
| 136 |
\_def\_mnote #1#{\_ifx^#1^\_else \_mnoteC#1\_fin \_fi \_mnoteD} |
| 137 |
\_def\_mnoteC up#1\_fin{\_mnoteskip=#1\_relax} % \mnote up<dimen> {<text>} syntax |
| 138 |
\_long\_def\_mnoteD#1{% |
| 139 |
\_ifvmode \_vskip\_parskip{\_mnoteA{#1}}\_nobreak\_vskip-\_baselineskip\_vskip-\_parskip \_else |
| 140 |
\_lower\_dp\_strutbox\_hbox{}\_vadjust{\_kern-\_dp\_strutbox \_mnoteA{#1}\_kern\_dp\_strutbox}% |
| 141 |
\_fi |
| 142 |
} |
| 143 |
\_public \mnote ; |
| 144 |
|
| 145 |
\_doc ----------------------------- |
| 146 |
The \`\mnoteskip` is a dimen value that denotes the vertical shift of marginal |
| 147 |
note from its normal position. A positive value means shift up, negative |
| 148 |
down. The `\mnoteskip` register is set to zero |
| 149 |
after the marginal note is printed. The new syntax `\mnote up<dimen>{<text>}` |
| 150 |
is possible too, but public `\mnoteskip` is kept for backward compatibility. |
| 151 |
\_cod ----------------------------- |
| 152 |
|
| 153 |
\_newdimen\_mnoteskip |
| 154 |
\_public \mnoteskip ; |
| 155 |
|
| 156 |
\_doc ----------------------------- |
| 157 |
The \`\_mnoteA` macro does the real work. The \`\_lrmnote``{<left>}{<right>}` |
| 158 |
uses only first or only second parameter depending on the left or right |
| 159 |
marginal note. The \`\_mninvert` macro can be redefined for special purposes, see |
| 160 |
\ulink[http://petr.olsak.net/optex/optex-tricks.html\#mninvert]{\OpTeX/ trick 0175}. |
| 161 |
\_cod ----------------------------- |
| 162 |
|
| 163 |
\_long\_def\_mnoteA #1{\_incr\_mnotenum |
| 164 |
\_ifx\_mnotesfixed\_undefined |
| 165 |
\_ifcsname _mn:\_the\_mnotenum \_endcsname |
| 166 |
\_edef\_mnotesfixed{\_mninvert\_cs{_mn:\_the\_mnotenum}}% |
| 167 |
\_else |
| 168 |
\_opwarning{unknown \_noexpand\mnote side. TeX me again}\_openref |
| 169 |
\_incr\_unresolvedrefs |
| 170 |
\_def\_mnotesfixed{\_right}% |
| 171 |
\_fi\_fi |
| 172 |
\_hbox to0pt{\_wref\_Xmnote{}\_everypar={}\_resetattrs |
| 173 |
\_lrmnote{\_kern-\_mnotesize \_kern-\_mnoteindent}{\_kern\_hsize \_kern\_mnoteindent}% |
| 174 |
\_vbox to0pt{\_vss \_setbox0=\_vtop{\_hsize=\_mnotesize |
| 175 |
\_lrmnote{\_leftskip=0pt plus 1fill \_rightskip=0pt} |
| 176 |
{\_rightskip=0pt plus 1fil \_leftskip=0pt}% |
| 177 |
{\_the\_everymnote\_noindent#1\_endgraf}}% |
| 178 |
\_dp0=0pt \_box0 \_kern\_mnoteskip \_global\_mnoteskip=0pt}\_hss}% |
| 179 |
} |
| 180 |
\_def \_lrmnote#1#2{\_ea\_ifx\_mnotesfixed\_left #1\_else #2\_fi} |
| 181 |
\_def\_mninvert{} % should be redefined, see OpTeX trick 0175 |
| 182 |
|
| 183 |
\_doc ----------------------------- |
| 184 |
We don't want to process `\fnote`, `\fnotemark`, `\mnote` in TOC, headlines |
| 185 |
nor outlines. |
| 186 |
\_cod ----------------------------- |
| 187 |
|
| 188 |
\_regmacro {\_def\fnote#1{}} {\_def\fnote#1{}} {\_def\fnote#1{}} |
| 189 |
\_regmacro {\_def\fnotemark#1{}} {\_def\fnotemark#1{}} {\_def\fnotemark#1{}} |
| 190 |
\_regmacro {\_def\mnote#1{}} {\_def\mnote#1{}} {\_def\mnote#1{}} |
| 191 |
|
| 192 |
\_endcode % ------------------------------------- |
| 193 |
|
| 194 |
2026-03-19 \_mninvert introduced |
| 195 |
2026-02-15 \_resetattrs added to \mnote |
| 196 |
2025-12-15 \fnote, \fnotenum defined as \protected. |
| 197 |
2023-04-15 \fnote in math mode allowed |
| 198 |
2020-05-26 \mnote up<dimen> implemented |
| 199 |
2020-03-20 released |