| 1 |
% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \cite {Cite, Bibliography <2026-05-06>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
Registers used by `\cite`, `\bib` macros are declared here. |
| 7 |
The \`\bibnum` counts the bibliography items from one. |
| 8 |
The \`\bibmark` is used when `\nonumcitations` is set. |
| 9 |
\_cod ----------------------------- |
| 10 |
|
| 11 |
\_newcount\_bibnum % the bibitem counter |
| 12 |
\_newtoks\_bibmark % the bibmark used if \nonumcitations |
| 13 |
\_newcount\_lastcitenum \_lastcitenum=0 % for \shortcitations |
| 14 |
\_public \bibnum \bibmark ; |
| 15 |
|
| 16 |
\_doc ----------------------------- |
| 17 |
\`\_bibp` expands to \^`\bibpart``/`. |
| 18 |
By default, \^`\bibpart` is empty, so internal links are in the form |
| 19 |
`cite:/<number>`. If \^`\bibpart` is set to `<bibpart>`, then internal |
| 20 |
links are `cite:<bibpart>/<number>`. |
| 21 |
\_cod ----------------------------- |
| 22 |
|
| 23 |
\_def\_bibp{\_the\_bibpart/} % unique name for each bibliography list |
| 24 |
|
| 25 |
\_doc ----------------------------- |
| 26 |
\`\cite` `[<label>,<label>,...,<label>]` manages <labes> using `\_citeA` |
| 27 |
and prints `[<bib-marks>]` using `\_printsavedcites`. |
| 28 |
\nl |
| 29 |
\`\nocite` `[<label>,<label>,...,<label>]` only manages <labels> but prints nothing. |
| 30 |
\nl |
| 31 |
\`\rcite` `[<label>,<label>,...,<label>]` behaves like `\cite` but prints |
| 32 |
<bib-marks> without brackets. |
| 33 |
\nl |
| 34 |
\`\ecite` `[<label>]{<text>}` behaves like `\rcite [<label>]` but prints |
| 35 |
<text> instead <bib-mark>. The <text> is hyperlinked like <bib-marks> |
| 36 |
when `\cite` or `\rcite` is used. |
| 37 |
The empty internal macro \`\_savedcites` will include the `<bib-marks>` list to |
| 38 |
be printed. This list is set by `\_citeA` inside a group and it is used by |
| 39 |
`\_printsavedcites` in the same group. Each `\cite`/`\rcite`/`\ecite` macro |
| 40 |
starts from empty list of <bib-marks> because new group is opened. |
| 41 |
\_cod ----------------------------- |
| 42 |
|
| 43 |
\_def\_cite[#1]{{\_citeA#1,,,[\_printsavedcites]}} |
| 44 |
\_def\_nocite[#1]{{\_citeA#1,,,}} |
| 45 |
\_def\_rcite[#1]{{\_citeA#1,,,\_printsavedcites}} |
| 46 |
\_def\_ecite[#1]{\_bgroup\_citeA#1,,,\_ea\_eciteB\_savedcites;} |
| 47 |
\_def\_eciteB#1,#2;#3{\_if?#1\_relax #3\_else \_ilink[cite:\_bibp#1]{#3}\_fi\_egroup} |
| 48 |
\_def\_savedcites{} |
| 49 |
|
| 50 |
\_public \cite \nocite \rcite \ecite ; |
| 51 |
|
| 52 |
\_doc ----------------------------- |
| 53 |
`<bib-marks>` may be numbers or a special text related to cited bib-entry. |
| 54 |
It depends on \`\nonumcitations` and on used bib-style. The mapping from |
| 55 |
<label> to <bib-mark> is done when `\bib` or `\usebib` is processed. |
| 56 |
These macros store the information to |
| 57 |
\`\_Xbib``{<bibpart>}{<label>}{<number>}{<nonumber>}` |
| 58 |
where <number> and <nonumber> are |
| 59 |
two variants of <bib-mark> (numbered or text-like). This information is |
| 60 |
read from `.ref` file and it is saved to macros |
| 61 |
`\_bib:<bibpart>/<label>` and `\_bim:<bibpart>/<number>`. |
| 62 |
First one includes <number> and second one includes <nonumber>. |
| 63 |
The `\_lastbn:<bibpart>` macro includes last number of |
| 64 |
bib-entry used in the document with given <bibpart>. |
| 65 |
A designer can use it to set appropriate |
| 66 |
indentation when printing the list of all bib-entries. |
| 67 |
\_cod ----------------------------- |
| 68 |
|
| 69 |
\_def\_Xbib#1#2#3#4{\_sxdef{_bib:#1/#2}{\_bibnn{#3}&}% |
| 70 |
\_if^#4^\_else\_sxdef{_bim:#1/#3}{#4}\_fi\_sxdef{_lastbn:#1}{#3}} |
| 71 |
|
| 72 |
\_doc ----------------------------- |
| 73 |
\`\_citeA` `<label>,` processes one label from the list of labels given in the |
| 74 |
parameter of `\cite`, `\nocite`, `\rcite` or `\ecite` macros. It adds the |
| 75 |
<label> to a global list `\_ctlst:<bibpart>/` which will be used by `\usebib` (it |
| 76 |
must know what <labels> are used in the document to pick-up |
| 77 |
only relevant bib-entries from the database. Because we want to save |
| 78 |
space and to avoid duplications of <label> in the `\_ctlst:<bibpart>/`, we |
| 79 |
distinguish four cases: |
| 80 |
\begitems |
| 81 |
* `<label>` was not declared by \^`\_Xbib` before and it is first such a <label> in the |
| 82 |
document: Then `\_bib:<bibpart>/<label>` is undefined and we save label using |
| 83 |
\^`\_addcitelist`, write warning on the terminal and define |
| 84 |
`\_bib:<bibpart>/<label>` as empty. |
| 85 |
* `<label>` was not declared by \^`\_Xbib` before but it was used previously in the |
| 86 |
document: Then `\_bib:<bibpart>/<label>` is empty and we do nothing (only data to |
| 87 |
`\_savedcites` are saved). |
| 88 |
* `<label>` was declared by \^`\_Xbib` before and it is first such <label> |
| 89 |
used in the document: Then `\_bib:<bibpart>/<label>` includes \`\_bibnn``{<number>}&` and we |
| 90 |
test this case by the command `\if &\_bibnn{<number>}&`. This is true when `\_bibnn{<number>}` |
| 91 |
expands to empty. The <label> is saved by \^`\_addcitelist` and |
| 92 |
`\_bib:<bibpart>/<label>` is re-defined directly as <number>. |
| 93 |
* `<label>` was declared by \^`\_Xbib` and it was used previously in the |
| 94 |
document. Then we do nothing (only data to `\_savedcites` are saved. |
| 95 |
\enditems |
| 96 |
The `\_citeA` macro runs repeatedly over the whole list of <labels>. |
| 97 |
\_cod ----------------------------- |
| 98 |
|
| 99 |
\_def\_citeA #1#2,{\_if#1,\_else |
| 100 |
\_if *#1\_addcitelist{*}\_sxdef{_bib:\_bibp*}{}\_ea\_skiptorelax \_fi |
| 101 |
\_ifcsname _bib:\_bibp#1#2\_endcsname \_else |
| 102 |
\_addcitelist{#1#2}% |
| 103 |
\_opwarning{{\_the\_bibpart} \_noexpand\cite [#1#2] unknown. Try to TeX me again}\_openref |
| 104 |
\_incr\_unresolvedrefs |
| 105 |
\_addto\_savedcites{?,}\_def\_sortcitesA{}\_lastcitenum=0 |
| 106 |
\_ea\_gdef \_csname _bib:\_bibp#1#2\_endcsname {}% |
| 107 |
\_ea\_skiptorelax \_fi |
| 108 |
\_ea\_ifx \_csname _bib:\_bibp#1#2\_endcsname \_empty |
| 109 |
\_addto\_savedcites{?,}\_def\_sortcitesA{}\_lastcitenum=0 |
| 110 |
\_ea\_skiptorelax \_fi |
| 111 |
\_def\_bibnn##1{}% |
| 112 |
\_if &\_csname _bib:\_bibp#1#2\_endcsname |
| 113 |
\_def\_bibnn##1##2{##1}% |
| 114 |
\_addcitelist{#1#2}% |
| 115 |
\_sxdef{_bib:\_bibp#1#2}{\_csname _bib:\_bibp#1#2\_endcsname}% |
| 116 |
\_fi |
| 117 |
\_edef\_savedcites{\_savedcites \_csname _bib:\_bibp#1#2\_endcsname,}% |
| 118 |
\_relax |
| 119 |
\_ea\_citeA\_fi |
| 120 |
} |
| 121 |
\_let\_bibnn=\_relax |
| 122 |
|
| 123 |
\_doc ---------------------------- |
| 124 |
Because we implement possibility of more independent bibliography lists |
| 125 |
distinguished by <bibpart>, the \`\_addcitelist``{<label>}` macro |
| 126 |
must add the <label> to given `\_ctlst:<bibpart>/`.\nl |
| 127 |
When `\_addcitelist` is processed before \^`\usebib`, |
| 128 |
then \^`\_citeI``[<label>]` is added. \^`\usebib` will use this list for selecting |
| 129 |
right records from `.bib` file. Then \^`\usebib` sets `\_ctwrite:<bibpart>/`. |
| 130 |
If `\_addcitelist` is processed after \^`\usebib`, then |
| 131 |
\`\_Xcite``{<bibpart>/}{<label>}` is saved to the `.ref` |
| 132 |
file. The `\_Xcite` creates `\_ctlstB:<bibpart>/` as a list of saved |
| 133 |
`\_citeI[<label>]`. Finally, \^`\usebib` concats both lists `\_ctlst:<bibpart>/` |
| 134 |
and `\_ctlstB:<bibpart>/` in the second \TeX/ run. |
| 135 |
\_cod ---------------------------- |
| 136 |
|
| 137 |
\_def\_addcitelist#1{% |
| 138 |
\_unless \_ifcsname _ctlst:\_bibp\_endcsname \_sxdef{_ctlst:\_bibp}{}\_fi |
| 139 |
\_ifcsname _ctwrite:\_bibp \_endcsname \_openref \_immediate\_wref\_Xcite{{\_bibp}{#1}}% |
| 140 |
\_else \_global \_ea\_addto \_csname _ctlst:\_bibp\_endcsname {\_citeI[#1]}\_fi |
| 141 |
} |
| 142 |
\_def\_Xcite#1#2{% |
| 143 |
\_unless \_ifcsname _ctlstB:#1\_endcsname \_sxdef{_ctlstB:#1}{}\_fi |
| 144 |
\_global \_ea\_addto \_csname _ctlstB:#1\_endcsname {\_citeI[#2]}% |
| 145 |
} |
| 146 |
|
| 147 |
\_doc ----------------------------- |
| 148 |
The `<bib-marks>` (in numeric or text form) are saved in \^`\_savedcites` |
| 149 |
macro separated by commas. |
| 150 |
The \`\_printsavedcites` prints them by normal order or sorted if |
| 151 |
\`\sortcitations` is specified or condensed if \`\shortcitations` is |
| 152 |
specified.\nl |
| 153 |
The \^`\sortcitations` appends the dummy number 300000 and we suppose that normal |
| 154 |
numbers of bib-entries are less than this constant. |
| 155 |
This constant is removed after the sorting algorithm. |
| 156 |
The \^`\shortcitations` sets simply `\_lastcitenum=1`. |
| 157 |
The macros for <bib-marks> printing follows (sorry, without detail |
| 158 |
documentation). They are documented in `opmac-d.pdf` (but only in Czech). |
| 159 |
\_cod ----------------------------- |
| 160 |
|
| 161 |
\_def\_printsavedcites{\_sortcitesA |
| 162 |
\_chardef\_tmpb=0 \_ea\_citeB\_savedcites,% |
| 163 |
\_ifnum\_tmpb>0 \_printdashcite{\_the\_tmpb}\_fi |
| 164 |
} |
| 165 |
\_def\_sortcitesA{} |
| 166 |
\_def\_sortcitations{% |
| 167 |
\_def\_sortcitesA{\_edef\_savedcites{300000,\_ea}\_ea\_sortcitesB\_savedcites,% |
| 168 |
\_def\_tmpa####1300000,{\_def\_savedcites{####1}}\_ea\_tmpa\_savedcites}% |
| 169 |
} |
| 170 |
\_def\_sortcitesB #1,{\_if $#1$% |
| 171 |
\_else |
| 172 |
\_mathchardef\_tmpa=#1 |
| 173 |
\_edef\_savedcites{\_ea}\_ea\_sortcitesC \_savedcites\_end |
| 174 |
\_ea\_sortcitesB |
| 175 |
\_fi |
| 176 |
} |
| 177 |
\_def\_sortcitesC#1,{\_ifnum\_tmpa<#1\_edef\_tmpa{\_the\_tmpa,#1}\_ea\_sortcitesD |
| 178 |
\_else\_edef\_savedcites{\_savedcites#1,}\_ea\_sortcitesC\_fi} |
| 179 |
\_def\_sortcitesD#1\_end{\_edef\_savedcites{\_savedcites\_tmpa,#1}} |
| 180 |
|
| 181 |
\_def\_citeB#1,{\_if$#1$\_else |
| 182 |
\_if?#1\_relax??% |
| 183 |
\_else |
| 184 |
\_ifnum\_lastcitenum=0 % only comma separated list |
| 185 |
\_printcite{#1}% |
| 186 |
\_else |
| 187 |
\_ifx\_citesep\_empty % first cite item |
| 188 |
\_lastcitenum=#1\_relax |
| 189 |
\_printcite{#1}% |
| 190 |
\_else % next cite item |
| 191 |
\_advance\_lastcitenum by1 |
| 192 |
\_ifnum\_lastcitenum=#1\_relax % cosecutive cite item |
| 193 |
\_mathchardef\_tmpb=\_lastcitenum |
| 194 |
\_else % there is a gap between cite items |
| 195 |
\_lastcitenum=#1\_relax |
| 196 |
\_ifnum\_tmpb=0 % previous items were printed |
| 197 |
\_printcite{#1}% |
| 198 |
\_else |
| 199 |
\_printdashcite{\_the\_tmpb}\_printcite{#1}\_chardef\_tmpb=0 |
| 200 |
\_fi\_fi\_fi\_fi\_fi |
| 201 |
\_ea\_citeB\_fi |
| 202 |
} |
| 203 |
\_def\_shortcitations{\_lastcitenum=1 } |
| 204 |
|
| 205 |
\_def\_printcite#1{\_citesep |
| 206 |
\_ilink[cite:\_bibp#1]{\_citelinkA{#1}}\_def\_citesep{,\_hskip.2em\_relax}} |
| 207 |
\_def\_printdashcite#1{\_ifmmode-\_else\_hbox{--}\_fi\_ilink[cite:\_bibp#1]{\_citelinkA{#1}}} |
| 208 |
\_def\_citesep{} |
| 209 |
|
| 210 |
\_def\_nonumcitations{\_lastcitenum=0\_def\_sortcitesA{}\_def\_etalchar##1{$^{##1}$}% |
| 211 |
\_def\_citelinkA##1{\_trycs{_bim:\_bibp##1} |
| 212 |
{##1\_opwarning{\_noexpand\nonumcitations + empty bibmark. Maybe bad bib-style}}}% |
| 213 |
} |
| 214 |
\_def\_citelinkA{} |
| 215 |
|
| 216 |
\_public \nonumcitations \sortcitations \shortcitations ; |
| 217 |
|
| 218 |
\_doc ----------------------------- |
| 219 |
The \`\bib` `[<label>]` or `\bib [<label>] ={<bib-mark>}` prints one bib-entry |
| 220 |
without reading any database. The bib-entry follows after this command. |
| 221 |
This command counts the used `\bib`s from one by `\bibnum` counter and |
| 222 |
saves \^`\_Xbib``{<bibpart>}{<label>}{<number>}{<nonumber>}` into `.ref` file |
| 223 |
immediately using \`\_wbib``{<label>}{<number>}{<nonumber>}`. |
| 224 |
This is the core of creation of mapping from `<labels>` to `<number>` and |
| 225 |
`<nonumber>`.\nl |
| 226 |
\`\_bibA` and \`\_bibB` implement the scanner of the optional argument |
| 227 |
with the \^`\bibmark`.\nl |
| 228 |
\`\_bibgl` is `\relax` by default but \^`\slides` do `\let\_bibgl=\_global`.\nl |
| 229 |
\`\_dbib``{<label>}` creates destination for hyperlinks. |
| 230 |
\_cod ----------------------------- |
| 231 |
|
| 232 |
\_def\_bib[#1]{\_def\_tmp{\_isnextchar={\_bibA[#1]}{\_bibmark={}\_bibB[#1]}}% |
| 233 |
\_nospaceafter\_tmp} % ignore optional space |
| 234 |
\_def\_bibA[#1]=#2{\_bibmark={#2}\_bibB[#1]} |
| 235 |
\_def\_bibB[#1]{\_par \_bibskip |
| 236 |
\_bibgl\_advance\_bibnum by1 |
| 237 |
\_noindent \_def\_tmpb{#1}\_dbib{#1}\_wbib{#1}{\_the\_bibnum}{\_the\_bibmark}% |
| 238 |
\_printbib \_ignorespaces |
| 239 |
} |
| 240 |
\_def\_dbib#1{\_dest[cite:\_bibp\_the\_bibnum]\_printlabel{#1}} |
| 241 |
\_def\_wbib#1#2#3{% |
| 242 |
\_ifx\_wref\_wrefrelax\_else \_immediate\_wref\_Xbib{{\_the\_bibpart}{#1}{#2}{#3}}\_fi |
| 243 |
\_unless \_ifcsname bib:\_bibp#1\_endcsname \_Xbib{\_the\_bibpart}{#1}{#2}{#3}\_fi |
| 244 |
} |
| 245 |
\_let\_bibgl=\_relax |
| 246 |
|
| 247 |
\_public \bib ; |
| 248 |
|
| 249 |
\_doc ----------------------------- |
| 250 |
The \`\_printbib` prints the bib-entry itself. You can re-define it if you |
| 251 |
want a different design. The `\_pritbib` starts in horizontal mode after |
| 252 |
`\noindent` and after the eventual hyperlink destination is inserted. |
| 253 |
By default, the `\_printbib` sets the indentation |
| 254 |
by `\hangindent` and prints numeric <bib-marks> by |
| 255 |
`\llap{[\the\bibnum]}` If `\nonumcitations` then the `\_citelinkA` is not |
| 256 |
empty and <bib-marks> (`\the\bibnum` nor `\the\bibmark`) are not printed. |
| 257 |
The text of bib-entry follows. User can create this text manually |
| 258 |
using `\bib` command or it is generated automatically from a `.bib` database |
| 259 |
by `\usebib` command. |
| 260 |
|
| 261 |
The vertical space between bib-entries is controlled by \`\_bibskip` macro. |
| 262 |
\_cod ----------------------------- |
| 263 |
|
| 264 |
\_def \_printbib {\_hangindent=\_iindent |
| 265 |
\_ifx\_citelinkA\_empty \_hskip\_iindent \_llap{[\_the\_bibnum] }\_fi |
| 266 |
} |
| 267 |
\_def \_bibskip {\_ifnum\_bibnum>0 \_smallskip \_fi} |
| 268 |
|
| 269 |
\_doc ----------------------------- |
| 270 |
The \`\usebib` command is implemented in `usebib.opm` file which is loaded |
| 271 |
when the `\usebib` command is used first. |
| 272 |
The `usebib.opm` file loads the `librarian.tex` for scanning the `.bib` |
| 273 |
files. See the section~\ref[usebib], where the file `usebib.opm` is |
| 274 |
documented. |
| 275 |
\_cod ----------------------------- |
| 276 |
|
| 277 |
\_def\_usebib{\_par \_opinput {usebib.opm} \_usebib} |
| 278 |
\_def\usebib{\_usebib} |
| 279 |
|
| 280 |
\_doc ------------------------------ |
| 281 |
\_fw\`\nobibwarning` `[<list of bib-labels>]` |
| 282 |
declares a list of bib labels |
| 283 |
which are not fully declared in `.bib` file but we want to suppress the |
| 284 |
warning about it. List of bib labels are comma-separated case sensitive |
| 285 |
list without spaces. |
| 286 |
\_cod ------------------------------ |
| 287 |
|
| 288 |
\_def\_nobibwarnlist{,} |
| 289 |
\_def\_nobibwarning[#1]{\_global\_addto\_nobibwarnlist{#1,}} |
| 290 |
\_public \nobibwarning ; |
| 291 |
|
| 292 |
\_endcode % ------------------------------------- |
| 293 |
|
| 294 |
See end of the `usebib.opm` file for documentation about creating style |
| 295 |
files `bib-<style>.opm`. |
| 296 |
|
| 297 |
See end of the `bib-iso690.opm` file for large documentation about `.bib` files |
| 298 |
structure and about the usage of `iso690` bib-style. |
| 299 |
|
| 300 |
History: |
| 301 |
2026-05-06 ... \_addcitelist: \_ctwrite used for setting write flag |
| 302 |
2021-04-13 ... \bibpart implemented |
| 303 |
2021-04-13 ... \_bibgl introduced (used in \slides) |
| 304 |
2020-03-09 ... released |