# Bibliography

• Support for bibliographies is integrated in mkiv and LMTX's core (for the curious, the files are named bibl-tra. Two other important sources of information are: For older ConTeXt mkii, support for bibliographies was added by Taco. The Bibliography mkii page explains a few basic facts and points you to the manual of the module.
• TeX offers excellent ways to handle bibliographic references. bibTeX allows you to use a database to manage your references and to insert citations and a list of references into your document. If you're completely new to this subject, you should probably begin to read some basic hints about bibTeX databases. You will find a good selection of resources (most of them are free and available on-line) at the section "Bibliographies and Citations" of the [TeX faq]. A good place for beginners is Nicolas Markey’s [Tame the Beast]. If you have TeXLive installed, it may already be on your hard disk.

# Quickstart

From the manual:

\usebtxdataset[mkiv-publications.bib]
\starttext

\startbodymatter
a citation: \cite[article,book]
\stopbodymatter

\startbackmatter
\startchapter[title=Bibliography]
\placelistofpublications
\stopchapter
\stopbackmatter

\stoptext


# Database Formats

ConTeXt MkIV Bibliography can read several different formats of bibliographic databases:

## BibTeX .bib format

ConTeXt is independent from the BibTeX program, but can read .bib databases, if it’s not polluted with non-standarddized (LaTeX) commands. This format is the oldest, but supported by most bibliography management programs. The other supported formats use the same structure of records, just ported to different “languages”.

A BibTeX database is a text file, consisting of entries like:

@Article {Hagen2010,
author = "One or more authors",
title = "Some interesting title",
journal = "Publisher’s Weekly",
volume = "27",
number = "2",
pages = "21--30",
month = may,
year = "2020",
ISSN = "1234-5678"
}


@Article is an example for a common category, like @Book, @unpublished etc. Letter case doesn’t matter. The first argument reference must not contain spaces and is ended with a comma. Spacing within an entry doesn’t matter. If values consist of only one word, you don’t need quotes, otherwise you can also use brackets.

ConTeXt understands UTF-8 encoded bib files as well as old fashioned TeX accent combinations.

Fields
Some known fields of @Article are (see manual p.19):
• Title (also Title:en, Title:de etc.), Subtitle
• Author (see manual for possible formats)
• Journal, Type, Number, Volume, Pages, Review
• Year, Month, Day
• Warning, Comment, Note
• XCrossref
• Doi
• Keywords
• Language
• Url
• author, editor: fields are parsed as "Firstname(s) Lastname", multiple entries separated by 'and' (see manual p.6/14)
• keywords: separated by semicolons
There’s a (complete?) overview of categories and their fields in the manual on page 18 (Table 3.2).
Commands in entries
you can define unknown commands like this:
\definebtxcommand\TUB {TUGboat}
\definebtxcommand\sltt{\tt}
\definebtxcommand\<#1>{\type{#1}}

btx commands use a separate namespace.

## MkII .bbl format

This intermediate format is still supported, but obsolete. Just an example to let you know what it means:

\startpublication[k=Hagen:Second,t=article,a={Hans Hagen},y=2013,s=HH01]
\artauthor[]{Hans}[H.]{}{Hagen}
\arttitle{Who knows more?}
\journal{MyJournal}
\pubyear{2013}
\month{8}
\volume{1}
\issue{3}
\issn{1234-5678}
\pages{123--126}
\stoppublication


## Lua tables

Since internally everything anyway are Lua tables, you can use them directly as a database format:

   return { ["Hagen:First"] = {
author = "Hans Hagen",
category = "article",
issn = "1234-5678",
issue = "3",
journal = "MyJournal",
month = "8",
pages = "123--126",
tag = "Hagen:First",
title = "Who knows nothing?",
volume = "1",
year = "2013", },
}


## XML

    <?xml version="2.0" standalone="yes" ?>
<bibtex>
<entry tag="Hagen:First" category="article">
<field name="author">Hans Hagen</field>
<field name="category">article</field>
<field name="issn">1234-5678</field>
<field name="issue">3</field>
<field name="journal">MyJournal</field>
<field name="month">8</field>
<field name="pages">123--126</field>
<field name="tag">Hagen:First</field>
<field name="title">Who knows nothing?</field>
<field name="volume">1</field>
<field name="year">2013</field>
</entry>
</bibtex>


# Setup

\usebtxdataset[default][mybooks.bib]
\setupbtx[dataset=default]
\usebtxdefinitions[apa] % see p.15
\placelistofpublications

• The mentioned database file formats are recognized by their extension: .bib, .bbl., .lua and .xml
• default is the name for this database. If you load several files into the same name space, they get merged.
• \definebtxrendering, \setupbtxrendering
\definebtxrendering[example][apa][%  setup "example" inherits from "apa"
specification=apa,
sorttype=authoryear,
numbering=no
]


## sorttypes

• default, none, cite, list: don’t sort, use original order
• dataset, index: sort after dataset index
• reference: sort by reference tag
• key: sort by key (traditionally used in BibTeX as sortkey)
• short: sort by short
• authoryear: sort by author(s) and publication year

## bib list rendering

See manual, p.24ff.

\setupbtxlist[apa][
alternative=paragraph,
width=fit,
distance=.5em,
margin=3em % hanging list
]


Predefined renderings include:

• apa (by American Psychological Assoc.)
• mla
• chicago
• harvard
• ieee,
• aps (used in physical sciences)
• vancouver (used in biological sciences)

Note: Only apa and aps styles are currently provided as a showcase.

## Language

Bibliography lists and citations are rendered in the language of the document (\mainlanguage). If an entry includes a language field, this can be used, depending on the rendering specification.

You can force the main language on all entries with \setupdelimitedtext[language=global].

You can define and use translated titles with language modifiers, e.g. title:de or booktitle:fr.

## Complex example

To see how the concepts play together, here a relatively complete example:

\definebtxdataset[main] % choose a name for a dataset to use
\usebtxdataset[main][testbib.bib] % fill the dataset with bib entries
\setupbtx[dataset=main] % make \cite use our dataset by default
\setupbtx[default:cite][alternative=authoryear] % make \cite[ref] work like \cite[authoryear][ref]
\usebtxdefinitions[apa] % activate apa style for \cite rendering (also *load* apa style for bib entry rendering)
\setupbtx[apa:cite][otherstext={ et al.}] % modify the apa \cite rendering style (remove comma after sole author name)
\definebtxrendering[mainlist][apa][dataset=main] % define a bib entry rendering to use with \placebtxrendering

% in the document:
\placebtxrendering[mainlist][method=global]


# Citations

See p.31ff. of the manual. E.g.

\cite[num][reference] % [77]
\cite[authoryear][reference] % (Hagen 1977)
\cite[short][reference] % [HHH+77b]
and even
\cite[num][ref1,ref2,ref3] % [2,15,77] see p.35f.


The first argument defaults to the default citation rendering, as defined by \setupbtx[default:cite][alternative=num].

# Custom bibliographical styles

Users who wish to develop their own custom styles could start with the files in the distribution, copy them to different names and then modify them (e.g., the files publ-imp-apa.lua and publ-imp-apa.mkvi). However, these files contain a number of advanced code to deal with the numerous exceptions and rules. Hence, in some cases, it may be easier to write your own custom style. Here, I will provide you with an example what a minimal setup will be to get you started. Once you have a style in place and understand what's needed, it is easy to experiment further.

## Files

For every style, you will need two files: one with the TeX setups and one with the Lua definitions. They have to follow the naming conventions that the bibliographic subsystem expects so they can be found. If your style is called mycustom, these two files should be publ-imp-mycustom.mkiv and publ-imp-mycustom.lua.

## The Lua code

A minimal Lua file can be very brief. It has to contain a little bit of boiler plate with the required tables and the "categories" (i.e. publication types) that your bibliography will process. A minimal example will look like this:

local specification = {
name      = "mycustom",
categories = { },
}

local categories = specification.categories

categories.book = {
required = { "title" },
optional = {"author", "editor",
"year", "editor",
"edition", "series", "volume", "number",
},
}
return specification


For every type publicationtype you want processed, you need an entry categories.publicationtype. This entry needs to have a list of fields that will be processed, divided into "required" fields (that every entry needs to have) and "optional" fields (that they may or may not have). Every book needs to have a title, but not necessarily an author. Remember: only types that have a category will be processed. Only fields that are listed in the category will be processed.

## The TeX code

The TeX file is where the formatting is done. Again, you need to start with a few lines that make the publ-imp-mycustom.mkiv file work:

\startbtxrenderingdefinitions[mycustom]

\definebtx
[mycustom]
[default=default,
specification=mycustom]

\definebtxrendering
[mycustom]
[specification=mycustom,
numbering=yes]


This will make ConTeXt use your definitions. Further on, you include formatting directives for every type of publication you have included in your Lua file. If you want to modify the way the cite command will be displayed, these directives will be in the form \startsetups btx:mycustom:cite. For formatting the bibliographic list, they will be in the form \startsetups btx:mycustom:list:categroy. In our example, the only category is "book," so that's what our minimal definition will look like:

\startsetups btx:mycustom:list:book
\btxdoif {author} {
\color[blue]{\btxflush{author}}
\btxcomma
}
\btxdoifelse {title} {
\color[red]{\btxflush{title}}
} {
No Title
}
\removeunwantedspaces
\stopsetups


The most important command here is the btxflush: it will typeset the corresponding field of your bibliographic entry. You include it within formatting directives. As you can see, in our example, the author name will be typeset in blue; the title will be typeset in red. In the first section, btxdoif {author} checks whether there is a field "author"; if it exists, it will be typeset; if it doesn't exist, nothing happens. In the second example, the btxdoifelse {title} checks whether the field "title" exists. If it exists, the code within the first pair of braces will be executed, i.e., the title will be typeset in red. If it doesn't exist, the second pair of braces will be executed, and ConTeXt will typeset "No Title" in your document.

This should get you started with your custom style and let you experiment to your heart's content. The fields will be typeset in the order in which you include them in the setups. If you want to know more about these fields and about the commands that are available, check Chapter 6 of mkiv-publications.pdf.

# Simple manual bibliography

If you don’t need bibliographical databases, but just references within one document, this approach might be enough for you:

\def\dostartbibitem[#1][#2]%
{\doifsomethingelse{#2}%
{\startBibItem[reference={#1},title={#2}]}%
{\startBibItem[reference={#1},title={#1}]}%
}
\def\startbibitem{\dodoubleempty\dostartbibitem}
\def\stopbibitem{\stopBibItem}

\definedescription[BibItem][
margin=4em,
indenting={no},
indentnext=no,
alternative=hanging,
hang=1,
align=flushleft
]

\def\bibitemrule{\noindentation\vrule height0.5pt width4em depth0pt\thinspace}

\definereferenceformat [cite]  [type=title,left={},right={}]
\definereferenceformat [bibpage]   [type=page]

\starttext

\section{Content}

While we wait for the perfect book about ConTeXt\footnote{Maybe \cite[Hagen 2050]}, we can at least read a nice novel (\cite[Hagen 2014]).

\section{Bibliography}

\startbibitem[Hagen 2014]
Hagen, Hans: \emph{The Cow in the Ridderstraat.} Hasselt 2014.
\stopbibitem

\startbibitem[Hagen 2050]
\bibitemrule \emph{ConTeXt complete compendium.} Hasselt 2050.
\stopbibitem

\startbibitem[Hagen 2060][The Wizard 1899] % First parameter is reference, second gets displayed
\bibitemrule \emph{Optional differing reference title.} Hasselt 2060
\stopbibitem

\stoptext