| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \readbibs {Reading bib databases <2026-05-10>} % loaded on demand by \usebib |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
First, we implement the scanner of `.bib` files. Unfortunately, the format of |
| 7 |
these files isn't \TeX/ friendly, so we must to do more work. |
| 8 |
\`\readbibs` `{<bib-bases>}` reads <bib-bases> files |
| 9 |
(i.e. the Bib\TeX/ format). |
| 10 |
\_cod ----------------------------- |
| 11 |
|
| 12 |
\_newcount\_aunum |
| 13 |
\_newcount\_NameCount |
| 14 |
\_gdef\_eaddto#1#2{\_ea\_addto\_ea#1\_ea{#2}} |
| 15 |
|
| 16 |
\_gdef\_readbibs #1{% |
| 17 |
\_ifcsname _be:\_bibp*\_endcsname \_def\_citelist{}\_fi % \_citelist will be created |
| 18 |
\_begingroup |
| 19 |
\_everyeof{@{}}\_foreach#1,\_do##1,{% |
| 20 |
\_isfile{##1.bib}\_iftrue \_ea\_nextat\_input{##1.bib} |
| 21 |
\_else \_opwarning{\_string\usebib: Missing ##1.bib file} |
| 22 |
\_fi}% |
| 23 |
\_endgroup |
| 24 |
} |
| 25 |
\_public \readbibs ; |
| 26 |
|
| 27 |
\_doc ----------------------------- |
| 28 |
The \`\_nextat` macro skips the text in the `.bib` file to the next `@`, and starts |
| 29 |
the \`\_bibentry` macro which reads `@<entry type>{<data>}` from the `.bib` file. |
| 30 |
Each reference entry is converted to the \`\_entrydata` |
| 31 |
macro and then `\_glet \_be:<bib-part>/<label> = \_entrydata` is done. |
| 32 |
The \^`\_entrydata` includes key-value pairs, as described in the |
| 33 |
section~\ref[readbibs]. |
| 34 |
\_cod ----------------------------- |
| 35 |
|
| 36 |
\_long\_gdef\_nextat#1@{\_bgroup\_catcode` =9 \_ea\_egroup\_bibentry} |
| 37 |
\_gdef\_bibentry #1#{\_ifx^#1^\_else \_afterfi{\_bibentryA{#1}}\_fi} |
| 38 |
\_gdef\_bibentryA #1#2{\_lowercase{\_def\_entrytype{#1}}% |
| 39 |
\_ismacro\_entrytype{comment}\_iffalse % comment is ignored |
| 40 |
\_ismacro\_entrytype{texcode}\_iftrue % TeX code is processed |
| 41 |
\_endgroup #2\_begingroup \_everyeof{@{}}\_else |
| 42 |
\_ismacro\_entrytype{string}\_iftrue % string is reported as unsupported |
| 43 |
\_opwarning{\_string\usebib: @STRING entry isn't supported, try to use @TEXCODE}% |
| 44 |
\_else |
| 45 |
\_edef\_entrydata{[@]{\_entrytype}}% |
| 46 |
{\_bibentryB #2\_fin}% % read a "normal" bib entry |
| 47 |
\_fi\_fi\_fi |
| 48 |
\_nextat |
| 49 |
} |
| 50 |
\_gdef\_bibentryB #1#2,#3\_fin{\_def\_citekey{#1#2}\_def\_bibentryC{\_nextfield #3,\_fin}% |
| 51 |
\_ifcsname _be:\_bibp*\_endcsname |
| 52 |
\_bibentryC |
| 53 |
\_global\_addto\_citelist{\_citeI[#1#2]} |
| 54 |
\_else |
| 55 |
\_ea\_ifx \_begincsname _be:\_bibp#1#2\_endcsname \_empty |
| 56 |
\_bibentryC |
| 57 |
\_fi\_fi |
| 58 |
} |
| 59 |
\_gdef\_bibentryF {% finalize entry |
| 60 |
\_preparebibsorting |
| 61 |
\_global\_ea\_let \_csname _be:\_bibp\_citekey\_endcsname = \_entrydata |
| 62 |
} |
| 63 |
|
| 64 |
\_doc ----------------------------- |
| 65 |
\`\_nextfield` reads next field name and saves it to the \`\_fieldname` |
| 66 |
and then reads field data and saves it to the \`\_fielddata`. |
| 67 |
\_cod ----------------------------- |
| 68 |
|
| 69 |
\_gdef\_nextfield #1{\_ifx,#1\_ea\_nextfield % skip commas from previous field |
| 70 |
\_else \_ifx\_fin#1\_ea\_ea\_ea\_bibentryF % finalize bib entry |
| 71 |
\_else \_def\_fieldname{#1}\_ea\_ea\_ea\_nextfieldA % first letter of fieldname found |
| 72 |
\_fi\_fi |
| 73 |
} |
| 74 |
\_gdef\_nextfieldA #1{% next letters of field name until = is found |
| 75 |
\_ifx=#1\_afterfi{\_nospacefuturelet\_next\_nextfieldB}% |
| 76 |
\_else \_addto\_fieldname{#1}% |
| 77 |
\_ea\_nextfieldA \_fi |
| 78 |
} |
| 79 |
\_gdef\_nextfieldB {% reading field data |
| 80 |
\_casesof\_next |
| 81 |
" {\_nextfieldC}% name = "data", |
| 82 |
\_bgroup {\_nextfieldD}% name = {data}, |
| 83 |
\_finc {\_nextfieldE}% name = data, |
| 84 |
} |
| 85 |
\_gdef\_nextfieldC "#1"{\_nextfieldD{#1}} |
| 86 |
\_gdef\_nextfieldD #1{\_def\_fielddata{#1}\_nextfieldF} |
| 87 |
\_gdef\_nextfieldE #1,{\_nextfieldD{#1}} |
| 88 |
|
| 89 |
\_gdef\_nextfieldF{% finalize field |
| 90 |
\_lowercase\_ea{\_ea\_def\_ea\_fieldname\_ea{\_fieldname}}% case insensitive field name |
| 91 |
\_ifcsname _fia:\_fieldname\_endcsname \_edef\_fieldname{\_cs{_fia:\_fieldname}}\_fi |
| 92 |
\_eaddto\_entrydata{\_ea[\_fieldname]}% |
| 93 |
\_ismacro\_fieldname{author}\_iftrue \_ea\_auscan\_ea{\_fielddata}[author]\_else |
| 94 |
\_ismacro\_fieldname{editor}\_iftrue \_ea\_auscan\_ea{\_fielddata}[editor]\_else |
| 95 |
\_eaddto\_entrydata{\_ea{\_fielddata}}\_fi\_fi |
| 96 |
\_sdef{_fd:\_fieldname\_ea}\_ea{\_fielddata}% |
| 97 |
\_nextfield |
| 98 |
} |
| 99 |
|
| 100 |
\_doc ----------------------------- |
| 101 |
\`\_fieldalias``{<new-name>}{<given-name>}` |
| 102 |
defines `\_fia:<new_name>` as <given-name>. |
| 103 |
\_cod ----------------------------- |
| 104 |
|
| 105 |
\_gdef\_fieldalias#1#2{\_lowercase{\_sxdef{_fia:#1}{#2}}} |
| 106 |
|
| 107 |
\_doc ----------------------------- |
| 108 |
The \`\_auscan``{<authors/editors-names>}[<field-name>]` reads the specific Bib\TeX/ |
| 109 |
format menitoned in section~\ref[readbibs] and converts them to |
| 110 |
`{<Lastname>}{<Firstname>}{<Von>}{<Junior>}` for each author/editor. The result |
| 111 |
includes $4k$ subfields (where $k$ is number of the authors/editors) and |
| 112 |
it is saved to the `\_entrydata` and the |
| 113 |
`[authornum]{`$k$`}` or `[editornum]{`$k$`}` is added. |
| 114 |
|
| 115 |
The \`\_auscanA` macro does the loop over authors separated by `and`. |
| 116 |
Each single author has its `\_tmpb` macro with `X` and `x`. Each letter |
| 117 |
corresponds to single word of the name (`X`: begins with uppercase, `x`: |
| 118 |
begins with lowercase). For example Leonardo Piero da Vinci has `\_tmpb` |
| 119 |
macro `XXxX.`. If there are commas in after some words, then these commas |
| 120 |
are in `\_tmpb` macro too, for example da Vinci, Piero Leonardo has its |
| 121 |
`\_tmpb` macro `xX,XX.`. The number of commas is saved to `\_tmpnum`. |
| 122 |
The \`\_auscanB` macro does a slight modifications of the `\_tmpb` macro as |
| 123 |
mentioned in comments. Then the macro |
| 124 |
\`\_auscanD``<tpmb-pattern>;{<WordA>}{<WordB>}{<WordC>}...` is executed. |
| 125 |
It saves given words due to the `\_tmpb` pattern to the macros |
| 126 |
`\_Lastname`, `\_Firstname`, `\_Von`, `\_Junior` in a loop. Finally, the contents of |
| 127 |
these macros are saved to `\_fiedldata` and then to the `\_entrydata`. |
| 128 |
\_cod ----------------------------- |
| 129 |
|
| 130 |
\_gdef\_auscan#1[#2]{\_def\_auname{}\_def\_fielddata{}\_def\_tmpb{}\_aunum=0\_tmpnum=0 |
| 131 |
\_auscanA #1 and {} |
| 132 |
\_eaddto\_entrydata{\_ea{\_fielddata}[#2num]}% |
| 133 |
\_eaddto\_entrydata{\_ea{\_the\_aunum}}% |
| 134 |
} |
| 135 |
\_gdef\_auscanA #1 {% |
| 136 |
\_ifx^#1^\_else |
| 137 |
\_isequal{#1}{and}\_iftrue |
| 138 |
\_incr\_aunum |
| 139 |
\_addto\_tmpb{.}% |
| 140 |
\_auscanB |
| 141 |
\_ea\_auscanX \_auname |
| 142 |
\_def\_auname{}\_def\_tmpb{}\_tmpnum=0 |
| 143 |
\_else |
| 144 |
\_lowercase{\_isequal{#1}}{#1}\_iftrue \_addto\_tmpb{x}\_else \_addto\_tmpb{X}\_fi |
| 145 |
\_def\_tmp{#1^}\_isinlist\_tmp{,^}% |
| 146 |
\_iftrue |
| 147 |
\_ea\_auscanC\_tmp \_addto\_tmpb{,}\_incr\_tmpnum |
| 148 |
\_else |
| 149 |
\_def\_tmp{#1}% |
| 150 |
\_fi |
| 151 |
\_eaddto\_auname{\_ea{\_tmp}}% |
| 152 |
\_fi |
| 153 |
\_ea\_auscanA |
| 154 |
\_fi |
| 155 |
} |
| 156 |
\_gdef\_auscanB{% |
| 157 |
\_ifcase\_tmpnum % 0 commas: XXX. -> XX:X. ; XxxXX. -> X:xxXX. ; xXX. -> :xXX. ; First:Last |
| 158 |
\_isinlist\_tmpb{x}\_iffalse \_replstring\_tmpb{X.}{:X.}\_else |
| 159 |
\_ea\_auscanT\_tmpb;\_iffalse \_replstring\_tmpb{Xx}{X:x}\_fi\_fi |
| 160 |
\_or % 1 comma: XX,XXX -> XX,,XXX, Junior part is empty |
| 161 |
\_replstring\_tmpb{,}{,,}\_tmpnum=2 |
| 162 |
\_fi % 2 commas: XX,XX,XXX no changes, generic format: Last, Junior, First |
| 163 |
\_def\_Firstname{}\_def\_Lastname{}\_def\_Von{}\_def\_Junior{}% |
| 164 |
} |
| 165 |
\_gdef\_auscanT #1#2;\_iffalse{\_ifx#1x\_def\_tmpb{:x#2}\_else} % xXX. -> :xXX. |
| 166 |
\_gdef\_auscanX {\_ea\_auscanD\_tmpb;} |
| 167 |
\_gdef\_auscanD #1#2;{% |
| 168 |
\_def\_tmpb{#2}% |
| 169 |
\_casesof #1 |
| 170 |
. {\_auscanF} % dot is last character, do final job |
| 171 |
, {\_decr\_tmpnum \_auscanX} % Lastname->Junior or Junior->Firstname |
| 172 |
: {\_tmpnum=2 \_auscanX} % Firstname->Lastname |
| 173 |
X {\_auscanE\_Firstname\_Junior\_Lastname} % add data due to the \tmpnum value |
| 174 |
x {\_auscanE\_Firstname\_Junior\_Von} % Von instead Lastname |
| 175 |
\_finc {}% |
| 176 |
} |
| 177 |
\_gdef\_auscanE#1#2#3{\_ifcase\_tmpnum \_ea\_auscanS\_ea#1\_or \_ea\_auscanS\_ea#2\_else |
| 178 |
\_ea\_auscanS\_ea#3\_fi} |
| 179 |
\_gdef\_auscanS#1#2{% #1=\Firstname or \Lastname or etc., #2=word to be inserted |
| 180 |
\_ifx#1\_empty \_def#1{#2}\_else \_addto#1{ #2}\_fi |
| 181 |
\_auscanX |
| 182 |
} |
| 183 |
\_gdef\_auscanF{% final work of \auscanX |
| 184 |
\_eaddto\_fielddata{\_ea{\_Lastname}}\_eaddto\_fielddata{\_ea{\_Firstname}}% |
| 185 |
\_eaddto\_fielddata{\_ea{\_Von}}\_eaddto\_fielddata{\_ea{\_Junior}}% |
| 186 |
} |
| 187 |
\_gdef\_auscanC #1,^{\_def\_tmp{#1}} % removing final comma: Word,^ -> Word |
| 188 |
|
| 189 |
\_doc ----------------------------- |
| 190 |
The \^`\_citelist` includes \`\_citeI``[<label>]` |
| 191 |
commands. The \^`\usebib` macro runs this lists in order to print references. |
| 192 |
Each \^`\_citeI``[<label>]` prints single bib entry given by the <label>. |
| 193 |
It opens a group, sets macros `\_fd:<field-name>` to `<field-data>` and runs |
| 194 |
\^`\_printentry`. Finally, it closes \TeX/ group, |
| 195 |
so all macros `\_fd:<field-name>` have their initial (undefined) value.\nl |
| 196 |
The \`\_getfield``[<field-name>]\macro` does `\def\macro{<field-data>}`. |
| 197 |
If the field isn't declared then the `\macro` is empty. |
| 198 |
\_cod ----------------------------- |
| 199 |
|
| 200 |
\_gdef\_citeI[#1]{% |
| 201 |
\_begingroup |
| 202 |
\_ea\_ifx \_begincsname _be:\_bibp#1\_endcsname \_empty |
| 203 |
\_opwarning{\_string\usebib: entry [#1] isn't found in .bib}% |
| 204 |
\_global\_slet{_bes:#1}{_relax}% |
| 205 |
\_else |
| 206 |
\_ea\_ea\_ea\_foreach \_csname _be:\_bibp#1\_endcsname \_do[##1]##2{\_sdef{_fd:##1}{##2}}% |
| 207 |
\_def\_entrykey{#1}% |
| 208 |
\_printentry |
| 209 |
\_fi |
| 210 |
\_endgroup |
| 211 |
} |
| 212 |
\_gdef\_getfield[#1]#2{% |
| 213 |
\_ifcsname _fd:#1\_endcsname |
| 214 |
\_ea\_ea\_ea \_def \_ea\_ea\_ea #2\_ea\_ea\_ea {\_csname _fd:#1\_endcsname}% |
| 215 |
\_else \_def#2{}% |
| 216 |
\_fi |
| 217 |
} |
| 218 |
|
| 219 |
\_doc ----------------------------- |
| 220 |
\`\_preparebibsorting` is called repeatedly for each bib entry when its reading from |
| 221 |
`.bib` file is finished. Its main goal is to do |
| 222 |
`\gdef\_bes:<citekey> {\;<sorting-rule>^^^<citekey>}`. |
| 223 |
Note that the part of the control sequence name after `^^^` is ignored during sorting. |
| 224 |
The default \^`\_preparebibsorting` |
| 225 |
macro creates <sorting-rule> in the form: <Lastnames> <Firstnames> <Von> |
| 226 |
<Junior> of the first author followed by <year> from `year` field.\nl |
| 227 |
\`\_dobibsorting``\_citelist` sorts the \^`\_citelist` and runs it. |
| 228 |
\_cod ----------------------------- |
| 229 |
|
| 230 |
\_gdef\_preparebibsorting{% |
| 231 |
\_getfield[sortedby]\_sortedby |
| 232 |
\_ifx\_sortedby\_empty % explicitly given [sortedby] field has precedence |
| 233 |
\_def\_sortedby{}% |
| 234 |
\_getfield[author]\_tmp % sorting by author firstly |
| 235 |
\_ifx\_tmp\_empty \_else \_ea\_preparebibsortingA\_tmp \_fin \_fi |
| 236 |
\_getfield[year]\_tmp % soering by year secondly |
| 237 |
\_ifx\_tmp\_empty \_else \_eaddto\_sortedby{\_tmp}\_fi |
| 238 |
\_edef\_sortedby{\_sortedby}% we need to run macros aka \"e etc. |
| 239 |
\_edef\_sortedby{\_ea\_removeoutbraces\_sortedby {\_fin}}% remove braces |
| 240 |
\_fi |
| 241 |
\_sxdef{_bes:\_citekey\_ea}\_ea{\_csname;\_detokenize\_ea{\_sortedby}^^^\_citekey\_endcsname}% |
| 242 |
} |
| 243 |
\_gdef\_preparebibsortingA#1#2#3#4#5\_fin {% names of the first author used by sorting: |
| 244 |
\_def\_sortedby{#1 }% Lastname |
| 245 |
\_ifx^#2^\_else \_addto\_sortedby{#2 }\_fi % Firstname |
| 246 |
\_ifx^#3^\_else \_addto\_sortedby{#3 }\_fi % Von |
| 247 |
\_ifx^#4^\_else \_addto\_sortedby{#4 }\_fi % Junior |
| 248 |
} |
| 249 |
\_gdef\_dobibsorting{% |
| 250 |
{\_def\_citeI[##1]{\_ea\_citeIs\_csname _bes:##1\_endcsname{##1}}% |
| 251 |
\_edef\_citelist{\_ea}\_citelist % converting \_citelist |
| 252 |
\_dosorting\_citelist \_ea}% |
| 253 |
} |
| 254 |
\_gdef\_citeIs#1#2{\_eaddto\_citelist{#1}% |
| 255 |
\_ea\_gdef#1{\_citeI[#2]\_ea\_glet#1=\_undefined \_glet#1=\_undefined}% |
| 256 |
} |
| 257 |
|
| 258 |
\_doc ----------------------------- |
| 259 |
The \`\_thisentry` macro prints bibliographic reference entry. |
| 260 |
It prints <bibnum> or <bimark> (including hyperlinks) and they are |
| 261 |
followed by printing the entry data. The format is given by the \^`\_printbib` |
| 262 |
macro and by `\_print:<entrytype>` declared in the bib-style file. |
| 263 |
\_cod ----------------------------- |
| 264 |
|
| 265 |
\_gdef\_thisentry {\_par \_bibskip |
| 266 |
\_bibgl\_incr\_bibnum |
| 267 |
\_isdefined{_bim:\_bibp\_the\_bibnum}\_iftrue |
| 268 |
\_edef\_tmpb{\_csname _bim:\_bibp\_the\_bibnum\_endcsname}% |
| 269 |
\_bibmark=\_ea{\_tmpb}% |
| 270 |
\_else \_bibmark={}\_fi |
| 271 |
\_edef\_tmpb{\_entrykey}% |
| 272 |
\_noindent \_dbib\_entrykey |
| 273 |
\_printbib |
| 274 |
{% |
| 275 |
\_getfield[@]\_entrytype |
| 276 |
\_csname _print:BEGIN\_endcsname |
| 277 |
\_isdefined{_print:\_entrytype}\_iftrue |
| 278 |
\_csname _print:\_entrytype\_endcsname |
| 279 |
\_else |
| 280 |
\_ifx\_entrytype\_empty \_else |
| 281 |
\_opwarning{Entrytype @\_entrytype\_space from [\_entrykey] undefined}% |
| 282 |
\_csname _print:misc\_endcsname |
| 283 |
\_fi\_fi |
| 284 |
\_csname _print:END\_endcsname |
| 285 |
\_wbib \_entrykey {\_the\_bibnum}{\_the\_bibmark}% |
| 286 |
}\_par |
| 287 |
} |
| 288 |
|
| 289 |
\_doc ----------------------------- |
| 290 |
\`\_printentry` is equivalent to the \^`\_thisentry` macro by default, it |
| 291 |
means that no filter is applied and all cited entries are printed. We |
| 292 |
can re-define \^`\_printentry` in order to implement a filter which prints |
| 293 |
only selected entries. It can be usable if we want to divide the |
| 294 |
bibliography list to more sections. For example, suppose that a `test.bib` |
| 295 |
file includes several entries with `entrysubtype` field and it can have a |
| 296 |
value `primary` We want to print these entries in a special subsection and |
| 297 |
then to print others entries. We can do following: |
| 298 |
\begtt |
| 299 |
\def\isfield[#1]#2{\_getfield[#1]\tmp \_ismacro\tmp{#2}} |
| 300 |
|
| 301 |
\nonum\secc Primary Sources |
| 302 |
\def\_printentry{\isfield[entrysubtype]{primary}\iftrue \_thisentry \_fi} |
| 303 |
\usebib/s (simple) test % only entrysubtype=primary entries are printed |
| 304 |
|
| 305 |
\nonum\secc Secondary Sources |
| 306 |
\def\_printentry{\isfield[entrysubtype]{primary}\iffalse \_thisentry \_fi} |
| 307 |
\usebib/s (simple) test % other cited entries are printed |
| 308 |
\endtt |
| 309 |
\_cod ----------------------------- |
| 310 |
|
| 311 |
\_doc ----------------------------- |
| 312 |
The \`\_bprinta`, \`\_bprintb`, \`\_bprintc`, \`\_bprintv` |
| 313 |
commands used in the style files: |
| 314 |
\_cod ----------------------------- |
| 315 |
|
| 316 |
\_gdef\_bprinta {\_bprintb*} |
| 317 |
\_gdef\_bprintb #1[#2#3]{% |
| 318 |
\_def\_bibfieldname{#2#3}% |
| 319 |
\_if!#2\_relax |
| 320 |
\_def\_bibfieldname{#3}% |
| 321 |
\_getfield[#3]\_bibfield |
| 322 |
\_getfield[#3num]\_namecount % total persons in the author/editor fields |
| 323 |
\_ifx\_bibfield\_empty\_else |
| 324 |
\_def\_bibfield{\_loopauthors{#3}}% read author/editor field in a loop |
| 325 |
\_fi |
| 326 |
\_else |
| 327 |
\_getfield[#2#3]\_bibfield |
| 328 |
\_fi |
| 329 |
\_if^#1^% |
| 330 |
\_ifx\_bibfield\_empty \_ea\_ea\_ea \_doemptyfield |
| 331 |
\_else \_ea\_ea\_ea \_dofullfield \_fi |
| 332 |
\_else \_ea \_bprintaA |
| 333 |
\_fi |
| 334 |
} |
| 335 |
\_gdef\_dofullfield#1#2{\_def\_dofield##1{#1}\_ea\_dofield\_ea{\_bibfield}} |
| 336 |
\_gdef\_doemptyfield#1#2{\_def\_dofield##1{#2}\_ea\_dofield\_ea{\_bibfield}} |
| 337 |
\_gdef\_bprintaA #1#2{\_ifx\_bibfield\_empty #2\_else\_bprintaB #1**\_fin\_fi} |
| 338 |
\_gdef\_bprintaB #1*#2*#3\_fin{\_ifx^#3^#1\_else\_ea\_bprintaC\_ea{\_bibfield}{#1}{#2}\_fi} |
| 339 |
\_gdef\_bprintaC #1#2#3{#2#1#3} |
| 340 |
\_gdef\_bprintc#1#2{\_bprintcA#1#2**\_relax} |
| 341 |
\_gdef\_bprintcA#1#2*#3*#4\_relax{\_ifx#1\_empty \_else \_if^#4^#2\_else#2#1#3\_fi\_fi} |
| 342 |
\_gdef\_bprintv [#1]#2#3{\_def\_tmpa{#2}\_def\_tmpb{#3}\_bprintvA #1,,} |
| 343 |
\_gdef\_bprintvA #1,{% |
| 344 |
\_if^#1^\_tmpb\_else |
| 345 |
\_getfield[#1]\_tmp |
| 346 |
\_ifx \_tmp\_empty |
| 347 |
\_else \_tmpa \_def\_tmpb{}\_def\_tmpa{}% |
| 348 |
\_fi |
| 349 |
\_ea \_bprintvA |
| 350 |
\_fi |
| 351 |
} |
| 352 |
|
| 353 |
\_doc ----------------------------- |
| 354 |
\`\_loopauthors``{<field-name>}` does a loop over all authors/editors in |
| 355 |
the `author` or `editor` field. The \`\_namecount` |
| 356 |
(total number of authors/editors) was defined in |
| 357 |
\^`\_bprintb`. Then for each author/edtor it do: |
| 358 |
\begitems |
| 359 |
* Set \`\_NameCount` to the position number of the currently processed author/editor. |
| 360 |
* Define \`\_Lastname`, \`\_Firstname`, \`\_Junior`, \`\_Von`, \`\_After` macros. |
| 361 |
* Run \^`\_authorname` or \^`\_editorname` macro (defined in the bib style file). |
| 362 |
\enditems |
| 363 |
\_cod ----------------------------- |
| 364 |
|
| 365 |
\_gdef\_loopauthors #1{% |
| 366 |
\_NameCount=0 |
| 367 |
\_ea\_ea\_ea\_foreach\_csname _fd:#1\_endcsname \_do ##1##2##3##4{% |
| 368 |
\_advance\_NameCount by1 |
| 369 |
\_def\_Lastname{##1}\_def\_Firstname{##2}\_def\_Von{##3}\_def\_Junior{##4}% |
| 370 |
\_csname _#1ini\_endcsname \_csname _#1name\_endcsname |
| 371 |
}} |
| 372 |
\_gdef\_authorini{} % ready for \_AbbreviateFirstname or similar... |
| 373 |
\_gdef\_editorini{} |
| 374 |
|
| 375 |
\_doc ----------------------------- |
| 376 |
\`\_bibwarning` can be used if the manatory field is missing. |
| 377 |
Note that \`\_nobibwarnlist` is used here, it is set by \^`\nobibwarning` macro. |
| 378 |
\_cod ----------------------------- |
| 379 |
|
| 380 |
\_gdef\_bibwarning{% |
| 381 |
\_ea\_isinlist \_ea\_nobibwarnlist\_ea{\_ea,\_entrykey,}\_iffalse |
| 382 |
\_opwarning{Missing field "\_bibfieldname" in [\_entrykey]}\_fi} |
| 383 |
|
| 384 |
\_doc ----------------------------- |
| 385 |
\`\_AbbreviateFirstname`, \`\_RetrieveFieldIn`, \`\_RetrieveField` are here only for |
| 386 |
backward compatibility with previous macros based on the librarian package. |
| 387 |
The \`\_CreateField`, \`\_SortingOrder`, and \`\_SpecialSort` are dummy |
| 388 |
macros because the sorting is implemented by a slightly different way |
| 389 |
than in librarian package. |
| 390 |
\_cod ----------------------------- |
| 391 |
|
| 392 |
\_gdef\_AbbreviateFirstname{\_addto\_authorini{\_abbrevnames\_Firstname}} |
| 393 |
\_gdef\_abbrevnames#1{% Karolina Pondelickova-Maslova -> K. P.-M. |
| 394 |
\_edef#1{\_ea\_foreach #1 \_do ##1##2 {##1.% |
| 395 |
\_foreach ##2-{}\_do ####1-####2{\_ifx^####2^\_else-####2.\_fi} }^% |
| 396 |
}\_replstring#1{ ^}{}% |
| 397 |
} |
| 398 |
\_gdef\_RetrieveFieldIn#1{\_getfield[#1]} |
| 399 |
\_gdef\_RetrieveField#1{\_trycs{_fd:#1}{}} |
| 400 |
\_gdef\_CreateField#1{} |
| 401 |
\_gdef\_SortingOrder#1#2{} |
| 402 |
\_gdef\_SpecialSort#1{} |
| 403 |
|
| 404 |
\_doc ----------------------------- |
| 405 |
The \^`\usebib` command is defined as `\input{usebib.opm}\_usebib` in the |
| 406 |
format. So, the command is re-defined here and it is run again with the new |
| 407 |
meaning.\nl |
| 408 |
The \^`\usebib` macro defined here reads `\_ctlst:<bibpart>/` and `\_ctlstB:<bibpart>/` |
| 409 |
(they include a list of `\_citeI[<label>]`) and merges them to a single \`\_citelist`. |
| 410 |
The `\_be:<bibpart>/<label>` is set to empty for each member of the `\_citelist`. |
| 411 |
Then the style file is read in a group, |
| 412 |
the \^`\readbibs` macro reads given `.bib` files and resulting `\_citelist` |
| 413 |
is processed: i.e.\ the macros \^`\_citeI` print desired entries. |
| 414 |
\_cod ----------------------------- |
| 415 |
|
| 416 |
\_gdef\_usebib/#1 (#2) #3 {% |
| 417 |
\_ifcsname _ctlst:\_bibp\_endcsname |
| 418 |
\_slet{_citelist}{_ctlst:\_bibp}\_else \_def\_citelist{}\_fi |
| 419 |
\_ea \_foreach\_citelist \_do ##1[##2]{\_sdef{_be:\_bibp##2}{}}% |
| 420 |
\_ifcsname _ctlstB:\_bibp\_endcsname |
| 421 |
\_ea\_ea\_ea\_foreach \_csname _ctlstB:\_bibp\_endcsname \_do ##1[##2]{% |
| 422 |
\_ifcsname _be:\_bibp##2\_endcsname |
| 423 |
\_else \_addto\_citelist{\_citeI[##2]}\_sdef{_be:\_bibp##2}{}% |
| 424 |
\_fi |
| 425 |
}% |
| 426 |
\_fi |
| 427 |
\_sxdef {_ctwrite:\_bibp}{}% |
| 428 |
\_ifx\_printentry\_undefined \_let\_printentry=\_thisentry \_fi |
| 429 |
\_ifx\_citelist\_empty |
| 430 |
\_opwarning{No cited items. \_noexpand\usebib ignored}% |
| 431 |
\_else |
| 432 |
\_bgroup |
| 433 |
\_par |
| 434 |
\_emergencystretch=.3\_hsize |
| 435 |
\_def\_optexbibstyle{#2}% |
| 436 |
\_setctable\_optexcatcodes |
| 437 |
\_input bib-#2.opm |
| 438 |
\_the \_bibtexhook |
| 439 |
\_ifcsname _mt:bib.and:\_cs{_lan:\_the\_language}\_endcsname \_else |
| 440 |
\_opwarning{\_string\usebib: No phrases for language |
| 441 |
"\_cs{_lan:\_the\_language}" (using "en")}% |
| 442 |
\_language=0 \_chardef\_documentlanguage=0 |
| 443 |
\_fi |
| 444 |
\_ifx#1c\_def\_preparebibsorting{}\_def\_dobibsorting{}\_fi |
| 445 |
\_readbibs {#3}% |
| 446 |
\_dobibsorting\_citelist |
| 447 |
\_restorectable |
| 448 |
\_egroup |
| 449 |
\_fi |
| 450 |
} |
| 451 |
|
| 452 |
\_endcode |
| 453 |
|
| 454 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 455 |
|
| 456 |
The file `usebib.opm` implements the command |
| 457 |
\~`\usebib/<sorttype> (<style>) <bibfiles>` |
| 458 |
where `<sorttype>` is one letter `c` (references ordered by citation order in |
| 459 |
the text) or `s` (references sorted usually by authors and years), |
| 460 |
`<style>` is the part of the name `bib-<style>.opm` of the style file |
| 461 |
and `<bibfiles>` are one or more `.bib` file names without suffix separated by |
| 462 |
comma without space. Example: |
| 463 |
|
| 464 |
\begtt |
| 465 |
\usebib/s (simple) mybase,yourbase |
| 466 |
\endtt |
| 467 |
This command reads the `<bibfiles>` directly and creates the list of |
| 468 |
bibliographic references (only those declared by \~`\cite[]` or |
| 469 |
\~`\nocite[]` in the document). The formatting of such references is defined in |
| 470 |
the style~file. |
| 471 |
|
| 472 |
The principle \"first entry wins" is used. Suppose `\usebib/s (simple) local,global`. |
| 473 |
If an entry with the same label is declared in `local.bib` and in |
| 474 |
`global.bib` too then the first wins. So, you can set exceptions in your |
| 475 |
`local.bib` file for your document. |
| 476 |
|
| 477 |
The `bib-<style>.opm` file declares entry types (like `@BOOK`, `@ARTICLE`) and declares |
| 478 |
their mandatory and optional fields (like `author`, `title`). When |
| 479 |
a mandatory field is missing in an entry in the `.bib` file then a warning is printed on the |
| 480 |
terminal about it. You can suppress such warnings by command |
| 481 |
\^`\nobibwarning`~`[<bib-labels>]`, where <bib-labels> is a comma-separated list |
| 482 |
of labels (without spaces) where missing mandatory fields will be no warned. |
| 483 |
|
| 484 |
Users may redefine declarations and the formatting rules given by the macros |
| 485 |
from the style~file. Such a re-definition have to be included in the |
| 486 |
\^`\bibtexhook` token list, because the \^`\usebib` macro opens group, reads |
| 487 |
the macros from the style~file, then executes \^`\bibtexhook` (it is empty |
| 488 |
by default), then reads data from the `.bib` files, then prints the desired |
| 489 |
records and finally, it closes the group. |
| 490 |
|
| 491 |
For example, \^`\bibtexhook={`\^`\oldaccents}` can be set if |
| 492 |
your old `.bib` files use an obscure notation for accents like `{\"o}`. |
| 493 |
Recommendation: converting such old `.bib` files to Unicode encoding is much |
| 494 |
more conceptual solution of this problem. |
| 495 |
|
| 496 |
\secc Notes for bib-style writers |
| 497 |
|
| 498 |
The `.bib` files include records in the format: |
| 499 |
|
| 500 |
\begtt \catcode`\<=13 |
| 501 |
@<entry-type>{<label>, |
| 502 |
<field-name> = "<field-data>", |
| 503 |
<field-name> = "<field-data>", |
| 504 |
...etc |
| 505 |
} |
| 506 |
\endtt |
| 507 |
% |
| 508 |
see the file `demo/op-biblist.bib` for a real example. The <entry-types> and |
| 509 |
<field-names> are case insensitive. More field-names can behave equally if the |
| 510 |
\^`\_fieldalias``{<new-field-name>}{<given-field-name>}` is used in a style |
| 511 |
file. If a <new-field-name> is declared by this command and it is used in the `.bib` |
| 512 |
file then the effect is the same as if it was used the <given-field-name>. |
| 513 |
|
| 514 |
Ancient Bib\TeX/ has read such files and has generated files appropriate for |
| 515 |
reading by \LaTeX. It has worked with a set of <entry-types>, see the www |
| 516 |
page \url{http://en.wikipedia.org/wiki/BibTeX}. The set of entry types |
| 517 |
listed on this www page is de facto the Bib\TeX/ standard. The \OpTeX/ bib |
| 518 |
style writer must \"declare" all such entry types and more non-standard |
| 519 |
entry types can be declared too if there is a good reason for doing it. The |
| 520 |
word \"declare" used in the previous sentence means that a bib-style writer must |
| 521 |
define the printing rules for each <entry-type>. |
| 522 |
The printing rules for <entry-type> include: which fields will be printed, in |
| 523 |
what order, by what format they will be printed on (italic, caps, etc.), |
| 524 |
which fields are mandatory, which are optional, and which are ignored in |
| 525 |
`.bib` records. |
| 526 |
|
| 527 |
The style writer can be inspired by two styles already done: `bib-simple.opm` |
| 528 |
and `bib-iso690.opm`. The second one is documented in detail in |
| 529 |
section~\ref[isobib]. |
| 530 |
|
| 531 |
The printing rules for each <entry-type> must be declared by |
| 532 |
`\_sdef{_print:<entry-type>}` in `bib-<style>.opm` file. |
| 533 |
The `<entry-type>` has to be lowercase here. \OpTeX/ supports following |
| 534 |
macros for a more comfortable setting of printing rules: |
| 535 |
|
| 536 |
\begitems |
| 537 |
* \^`\_bprinta` `[<field-name>] {<if defined>} {<if not defined>}`. The part <if defined> |
| 538 |
is executed if <field-name> is declared in .bib file for the entry which is |
| 539 |
currently processed. Else the part <if not defined> is processed. |
| 540 |
The part <if defined> can include the `*` parameter which is replaced |
| 541 |
by the value of the <field-name>. |
| 542 |
* The part <if not defined> can include |
| 543 |
the \^`\_bibwarning` command if the <field-name> is mandatory. |
| 544 |
* \^`\_bprintb` `[<field-name>] {<if defined>} {<if not defined>}`. The same as |
| 545 |
\^`\_bprinta`, but the `##1` parameter is used instead `*`. Differences: `##1` |
| 546 |
parameter can be used more than once and can be enclosed in nested |
| 547 |
braces. The `*` parameter can be used at most once and cannot be enclosed |
| 548 |
in braces. Warning: if the \^`\_bprintb` commands are nested (`\_bprintb` in |
| 549 |
\^`\_bprintb`), then you need to write the `####1` parameter for internal `\_bprintb`. |
| 550 |
But if \^`\_bprinta` commands are nested then the parameter is not duplicated. |
| 551 |
* \^`\_bprintc` `\macro {<if non-empty>}`. The <if non-empty> part is executed if |
| 552 |
`\macro` is non-empty. The `*` parameter can be used, it is replaced by |
| 553 |
the `\macro`. |
| 554 |
* \^`\_bprintv` `[<field1>,<field2>,...] {<if defined>} {<if not defined>}`. |
| 555 |
The part <if defined> is executed if <field1> or <filed2> or ... |
| 556 |
is defined, else the second part <if not defined> is executed. |
| 557 |
There is one filed name or the list field names separated by commas. |
| 558 |
The parts cannot include any parameters. |
| 559 |
\enditems |
| 560 |
|
| 561 |
There are two special field-names: `!author` and `!editor`. The processed list of |
| 562 |
authors or editors are printed here instead of raw data, see the commands |
| 563 |
\~`\_authorname` and \~`\_editorname` below. |
| 564 |
|
| 565 |
The bib-style writer can define `_print:BEGIN` and/or `_print:END`. |
| 566 |
They are executed at the beginning or |
| 567 |
end of each `<entry-type>`. The formatting does not solve the numbering and |
| 568 |
paragraph indentation of the entry. This is processed by \^`\_printbib` macro |
| 569 |
used in \OpTeX/ (and may be redefined by the author or document designer). |
| 570 |
|
| 571 |
The \^`\bibmark={something}` can be declared, for instance in the `_print:END` macro. |
| 572 |
Such \"bibmark" is saved to the `.ref` file and used in next \TeX/ run as |
| 573 |
`\cite` marks when `\nonumcitations` is set. |
| 574 |
|
| 575 |
Moreover, the bib-style writer must declare the format of |
| 576 |
special fields `author` and `editor`. These fields include a |
| 577 |
list of names, each name is processed individually in a loop. The |
| 578 |
\^`\_authorname` or \^`\_editorname` is called for each name on the list. |
| 579 |
The bib-style writer must define |
| 580 |
the \`\_authorname` and \`\_editorname` commands |
| 581 |
in order to declare the format of printing each individual name. |
| 582 |
The following control sequences can be used in these macros: |
| 583 |
|
| 584 |
\begitems |
| 585 |
* \^`\_NameCount`: the number of the currently processed author in the list |
| 586 |
* \^`\_namecount`: the total number of the authors in the list |
| 587 |
* \^`\_Lastname`, \^`\_Firstname`, \^`\_Von`, \^`\_Junior`: the parts of the name. |
| 588 |
\enditems |
| 589 |
|
| 590 |
The whole style file is read in the group during the \^`\usebib` command is executed |
| 591 |
before typesetting the reference list. Each definition or setting is local |
| 592 |
here. |
| 593 |
|
| 594 |
The auto-generated phrases (dependent on current language) can be used in |
| 595 |
bib-style files by `\_mtext{bib.<identifier>}`, where <identifier> is an identifier of |
| 596 |
the phrase and the phrase itself is defined by |
| 597 |
`\_sdef{_mt:bib.<identifier>:<language>}{<phrase>}`. See section~\ref[langphrases] |
| 598 |
for more detail. Phrases for <identifiers>: |
| 599 |
and, etal, edition, citedate, volume, number, prepages, postpages, editor, |
| 600 |
editors, available, availablealso, bachthesis, masthesis, phdthesis |
| 601 |
are defined already, see the end of section~\ref[langphrases]. |
| 602 |
|
| 603 |
The `sortedby` field is declared by \~`\readbibs` as a special field where |
| 604 |
sorting phrase can be specified. If it is present then it has precedence |
| 605 |
before default sorting phrase generated by \~`\_preparebibsorting` from the |
| 606 |
lastname, firstnames of the first author and from the year. |
| 607 |
Suppose that the .bib file includes: |
| 608 |
\begtt |
| 609 |
author = "Jan Chadima", |
| 610 |
sortedby = "Hzzadima Jan", |
| 611 |
\endtt |
| 612 |
Now, this author is sorted between H and I, because the Ch digraph in this |
| 613 |
name has to be sorted by this rule. |
| 614 |
|
| 615 |
If you need (for example) to place the auto-citations before other citations, |
| 616 |
then you can mark your entries in `.bib` file by `sortedby = "@"`, because this |
| 617 |
character is sorted before `A`. |
| 618 |
|
| 619 |
If you want to declare a different sorting rule, you can re-define the |
| 620 |
\^`\_preparebibsorting` macro. The example is in the |
| 621 |
\ulink[http://petr.olsak.net/optex/optex-tricks.html\#reverseyear]{\OpTeX/ trick 0113}. |
| 622 |
|
| 623 |
\secc[readbibs] Direct reading of `.bib` files |
| 624 |
|
| 625 |
\^`\readbibs` `{<bib-bases>}` is internally used (by \^`\usebib`) |
| 626 |
for reading `.bib` databases in Bib\TeX/ format. The <bib-bases> is |
| 627 |
comma separated list of file names (without `.bib` extension, without |
| 628 |
spaces). These files are read and \^`\readbibs` defines macros |
| 629 |
`\_be:<bibpart>/<label>`, where <label> is the label of the reference record. |
| 630 |
These macros include key-value pairs `[<field name>]{<field data>}`. |
| 631 |
The first pair is `[@]{<entry type>}`. |
| 632 |
For example, if we have in the `.bib` file: |
| 633 |
\begtt |
| 634 |
@Book { tbn, |
| 635 |
author = "Petr Olšák", |
| 636 |
TITle = {\TeX{}book naruby}, |
| 637 |
publisher = "Konvoj", |
| 638 |
year = 2001, |
| 639 |
} |
| 640 |
\endtt |
| 641 |
and the \^`\bibpart` is empty (default value) |
| 642 |
then the `\_be:/tbn` macro is defined with the content: |
| 643 |
\begtt |
| 644 |
[@]{BOOK}[author]{{Olšák}{Petr}{}{}}[authornum]{1}[title]{\TeX{}book naruby}% |
| 645 |
[publisher]{Konvoj}[year]{2001} |
| 646 |
\endtt |
| 647 |
If you do `\slet{tmp}{_be:/tbn}` then you can print the data (for example) by: |
| 648 |
\begtt |
| 649 |
\ea\foreach \tmp \do [#1]#2{\wterm{field-name: "#1", data: "#2"}} |
| 650 |
\endtt |
| 651 |
or you can do `\ea\foreach \tmp \do [#1]#2{\sdef{bib-field:#1}{#2}}` to enable |
| 652 |
direct access to the scanned data. |
| 653 |
|
| 654 |
Note that entry type and field names are converted lower-case by the `\readbibs` macro. |
| 655 |
|
| 656 |
There are two special entry types: `@COMMENT{<ignored text>}` and |
| 657 |
`@TEXCODE{<processed text>}`. The <ignored text> is ignored, the |
| 658 |
<processed text> is executed by \TeX/. The definitions of macros used in other |
| 659 |
entries in data of fields can be here. If the `\usebib` is used then |
| 660 |
the <processed text> is executed inside a |
| 661 |
\TeX/ group, so the assignment is locally valid only during creating the |
| 662 |
reference list. The BiB\TeX/'s `@STRING{}` isn't supported. All others entry |
| 663 |
types are interpreted as a {\em reference entry} and they are interpreted as |
| 664 |
described above. An optional balanced text between entries in `.bib` files |
| 665 |
is ignored. |
| 666 |
|
| 667 |
If the macro `\_be:<bibpart>/*` is defined then the \^`\readbibs` macro reads |
| 668 |
all entries from `.bib` files and creates \~`\_citelist`. If the |
| 669 |
`\_be:<bibpart>/*` is undefined then |
| 670 |
the \^`\readbibs` macro reads only entries with <label> where |
| 671 |
`\_be:<bibpart>/<label>` is set to the empty macro. After reading, the |
| 672 |
macros `\_be:<bibpart>/<label>` are globally re-defined as described above. |
| 673 |
|
| 674 |
The \^`\readbibs` macro doesn't convert fields data, but there are two exceptions: |
| 675 |
author and editor fields. These fields have very specific format with |
| 676 |
various alternatives, see \url{https://nwalsh.com/tex/texhelp/bibtx-23.html}. |
| 677 |
Shortly speaking, more authors are divided by the `and` keyword and names of a single |
| 678 |
author must be separated to four subfields: <Lastnames>, <Firstnames>, <Von>, |
| 679 |
<Junior>. Only the <Lastnames> subfield must be nonempty. The input can look like |
| 680 |
\begtt |
| 681 |
Leonardo Piero da Vinci |
| 682 |
or |
| 683 |
da Vinci, Leonardo Piero |
| 684 |
\endtt |
| 685 |
and both these variants are converted to `{Vinci}{Leonardo Piero}{da}{}`. |
| 686 |
The <Von> part is rekognized as a word with only lowercase letters. In |
| 687 |
general, the name can be written without commas: <Firstnames> <Von> |
| 688 |
<Lastnames> or with single comma: <Von> <Lastnames>, <Firstnames> or with two |
| 689 |
commas: <Von> <Lastnames>, <Junior>, <Firstnames> and all these variants are |
| 690 |
converted to the quaternion `{<Lastnames>}{<Firstnames>}{<Von>}{<Junior>}` |
| 691 |
by the \^`\readbibs` macro. |
| 692 |
If there are more than single author, then each author is saved in four |
| 693 |
subfields side by side, so you have 4 or 8 or 12 etc.\ subfildeds in the |
| 694 |
author/editor data field. You can read them by |
| 695 |
`\foreach <author-data>\do #1#2#3#4{...}`. |
| 696 |
|
| 697 |
\_endinput |
| 698 |
|
| 699 |
History: |
| 700 |
-------- |
| 701 |
2026-05-10 \let\printentry=\thisentry moved to keep the global/local logic |
| 702 |
2026-05-09 \usebib sets its macros globaly |
| 703 |
2026-05-06 \_thisentry / \_printentry pair introduced for filtering |
| 704 |
2024-02-18: missing .bib files checked, doc improved |
| 705 |
2023-06-25: \_fieldalias introduced |
| 706 |
2023-06-07: bug fix (\_namecount declaration) |
| 707 |
2023-06-06: \_slet{_bes:#1}{_relax}: bugfix when label not found and sorting |
| 708 |
2023-06-05: @TEXCODE introduced + more consistent doc |
| 709 |
2023-06-02: macros re-implemeted, librarian package no more needed. |
| 710 |
2018 - 2023-06-01 usebib.opm based on librarian package, like in OPmac. |