==The database==
The bibTEX format is rather popular in the TEX community and even with its shortcomings it will stay around for a while. Many publication websites can export and many tools are available to work with this database format. It is rather simple and looks a bit like Lua tables. Unfortunately the content can be polluted with non-standardized TEX commands which complicates pre- or postprocessing outside TEX. In that sense a bibTEX database is often not coded neutrally. Some limitations, like the use of commands to encode accented characters root in the ascii world and can be bypassed by using utf instead (as handled somewhat in LATEX through extensions such as <tt style="color:rgb(0,102,102);font-size:120%;" >bibtex8</tt>).
<br/>
Unknown commands do not stall processing, but their names are then typeset in a mono- spaced font so they probably stand out for proofreading. You can access the commands with <tt style="color:rgb(0,102,102);font-size:120%;" >\btxcommand{...}</tt>, as in:
<pre style="color:rgb(102,0,102);font-size:120%">commands like \btxcommand{MySpecialCommand} are handled in an indirect way</pre>
<br/>
As this is an undefined command we get: “commands like MySpecialCommand are handled in an indirect way”.
<br/>
In this document we use some example databases, so let’s load one of them now:
<pre style="color:rgb(102,0,102);font-size:120%">\definebtxdataset[example]
\usebtxdataset[example][mkiv-publications.bib]
</pre>
<br/>
You can ask for an overview of entries in a dataset with:
<pre style="color:rgb(102,0,102);font-size:120%">\showbtxdatasetfields[example]</pre>
<br/>
this gives:
<br/>
If you want to see what publications are in the database, the easiest way is to ask for a complete list:
<pre style="color:rgb(102,0,102);font-size:120%">\definebtxrendering
[example]
[dataset=example,
[example]
[criterium=all]
</pre>
<br/>
This gives:1 Hagen, H. and Otten, T. (1996). Typesetting education documents2 Scarso, L. (2021). Designing high speed trains3 author (year). title pages p.
Citations are references to bibliographic entries that normally show up in lists someplace in the document: at the end of a chapter, in an appendix, at the end of an article, etc. We discussed the rendering of these lists in the previous chapter. A citation is normally pretty short as its main purpose is to refer uniquely to a more detailed description. But, there are several ways to refer, which is why the citation subsystem is configurable and extensible. Just look at the following commands:
<pre style="color:rgb(102,0,102);font-size:120%">\cite[author][example::demo-003]
\cite[authoryear][example::demo-003]
\cite[authoryears][example::demo-003]
\cite[authoryear][example::demo-004,demo-003]
\cite[authoryears][example::demo-004,demo-003]
</pre>
(Hans Hagen and Ton Otten)
<br/>
You can tune the way a citation shows up:
<pre style="color:rgb(102,0,102);font-size:120%">\setupbtxcitevariant[author] [sorttype=author,color=darkyellow]
\setupbtxcitevariant[authoryear] [sorttype=author,color=darkyellow]
\setupbtxcitevariant[authoryears][sorttype=author,color=darkyellow]
</pre>
\cite[author][example::demo-004,demo-003]
\cite[authoryear][example::demo-004,demo-003]
<br/>
You can loop over the entries using regular Lua code combined with MkIV helpers:
<pre style="color:rgb(102,0,102);font-size:120%">local dataset = publications.datasets.example</pre>
context.starttabulate { "|l|l|l|" }
for tag, entry in table.sortedhash(dataset.luadata) do
<br/>
Once a dataset is accessible as xml tree, you can use the regular <tt style="color:rgb(0,102,102);font-size:120%;" >\xml...</tt> commands. We start with loading a dataset, in this case from just one file.
<pre style="color:rgb(102,0,102);font-size:120%">\usebtxdataset[tugboat][tugboat.bib]</pre>
<br/>
The dataset has to be converted to xml:
<pre style="color:rgb(102,0,102);font-size:120%">\convertbtxdatasettoxml[tugboat]</pre>
<br/>
The tree is now accessible by its root reference <tt style="color:rgb(0,102,102);font-size:120%;" >btx:tugboat</tt>. If we want simple field access we can use a few setups:
<pre style="color:rgb(102,0,102);font-size:120%">\startxmlsetups btx:initialize
\xmlsetsetup{#1}{bibtex|entry|field}{btx:*}
\xmlmain{#1}
\stopxmlsetups
</pre>
\startxmlsetups btx:field
\xmlflushcontext{#1}
<br/>
The two setups are predefined in the core already, but you might want to change them. They are applied in for instance:
<pre style="color:rgb(102,0,102);font-size:120%">\starttabulate[|||]
\NC \type {tag} \NC \xmlfirst {btx:tugboat}
{/bibtex/entry[string.find(@tag,'Hagen')]/attribute('tag')}
\NC \NR
\stoptabulate
</pre>
{|
|}
<pre style="color:rgb(102,0,102);font-size:120%">\startxmlsetups btx:demo
\xmlcommand
{#1}
{/bibtex/entry[string.find(@tag,'Hagen')][1]}{btx:table}
\stopxmlsetups
</pre>
\startxmlsetups btx:table
\starttabulate[|||]
<br/>
Here is another example:
<pre style="color:rgb(102,0,102);font-size:120%">\startxmlsetups btx:row
\NC \xmlatt{#1}{tag}
\NC \xmlfirst{#1}{/field[@name='title']}
\NC \NR
\stopxmlsetups
</pre>
\startxmlsetups btx:demo
\xmlfilter {#1} {
<br/>
A more extensive example is the following. Of course this assumes that you know what xml support mechanisms and macros are available.
<pre style="color:rgb(102,0,102);font-size:120%">\startxmlsetups btx:getkeys
\xmladdsortentry{btx}{#1}{\xmlfilter{#1}{/field[@name='author']/text()}}
\xmladdsortentry{btx}{#1}{\xmlfilter{#1}{/field[@name='year' ]/text()}}
\xmladdsortentry{btx}{#1}{\xmlatt{#1}{tag}}
\stopxmlsetups
</pre>
\startxmlsetups btx:sorter
\xmlresetsorter{btx}