![]() |
Writing TeXmacs bibliography styles | ![]() |
TeXmacs admits support both for BibTEX and a native tool for managing bibliographies. BibTEX styles are denoted by their usual names. TeXmacs styles are prefixed by tm-. For example, the TeXmacs tm-plain style is the replacement for the BibTEX plain style. Equivalents for the following BibTEX styles have been implemented: abbrv, abstract, acm, alpha, elsart-num, ieeetr, plain, siam and unsrt. These styles can therefore be used without installation of BibTEX. When BibTEX is not installed and a document uses a BibTEX style name, then TeXmacs falls back on tm-name, or on tm-plain if no such style exists. (Without the database tool, this fallback is a fixed list in
edit_process.cpp
, which does not contain abstract: a document with the style abstract then uses tm-plain.)
New bibliography styles can be defined by the user. Each style is associated to a unique Scheme file, which should be added to the directory $TEXMACS_PATH/progs/bibtex. Style files are treated as regular Scheme programs. Since the creation of a style file from scratch is a complex task, we recommend you customize existing style files or modules. In the next sections, we will describe the creation of a new style on a simple example and give a detailed lists of available Scheme functions which facilitate the creation of new styles.
The bibliography is generated by generate_bibliography in
edit_process.cpp
, in one of two ways, depending on the preference "database tool" (Tools\(\rightarrow\)Database tool, off by default):
Without the database tool, the entries are read from the .bib file of the bibliography, together with the references of TeXmacs itself ($TEXMACS_PATH/misc/bib/texmacs.bib). For a style tm-name, the module (bibtex name) is loaded and its bib-process formats the entries; other styles are given to the bibtex program. When there is no .bib file, a .bbl file of the same name is used.
With the database tool, bib-compile in
database/bib-manage.scm
looks for each cited key in the following sources, in this order (see bib-sources): the entries of the document itself (the attachments whose names end with -biblio), the .bib file of the bibliography and the references of TeXmacs, the bibliographic database of the user ((bib-database)), the .bib files kept up to date by Zotero (zotero-managed-file?), Zotero itself, and finally the entries kept in the document (the attachments whose names end with -bibliography). For the styles of (bib-standard-styles), the entries found are formatted by the Scheme style; for the other styles, the .bib files and the other entries, converted to BibTEX, are gathered in $TEXMACS_HOME_PATH/system/bib/auto.bib, which is given to the bibtex program. Afterwards, bib-attach keeps the entries found in the attachment prefix-bibliography of the document, so that the document can be compiled without the database. The routine (bib-retrieve-entries names . sources) retrieves the entries of a list of keys from such a list of sources.
The formatting routines of the styles are described below. See the TeXmacs database and bibliographies, Zotero and the bibliography styles for the underlying machinery.
Bibliographic style files are stored in directory $TEXMACS_PATH/progs/bibtex. They have the name of the style followed with extension .scm. For example, example.scm is the file name associated to the style example, which is denoted by tm-example when it is used in a TeXmacs document.
All style files must be declared as a module as follows:
(texmacs-module (bibtex example)
(:use (bibtex bib-utils) (bibtex plain))) |
The module bib-utils contains all useful functions needed to write bibliographic styles; the module of the fall-back style (here plain, see below) should also be used, so that its formatting routines are defined.
All style files must be declared as a bibliographic style as follows:
(bib-define-style "example" "plain") |
The first argument to bib-define-style is the name of the current style. The second argument is the name of a fall-back style, plain in our case. If a function is not defined in current style, the function from the fall-back style is used instead. Hence, the following minimalistic style file behaves in an identical way as the plain style:
(texmacs-module (bibtex example)
(:use (bibtex bib-utils) (bibtex plain)))
(bib-define-style "example" "plain") |
Each formatting function defined in the default style can be overloaded in the current style. For example, the function bib-format-date is used to format the date in the plain style. It is redefinable in our example style as follows:
(tm-define (bib-format-date e)
(:mode bib-example?)
(bib-format-field e "year")) |
All exported functions must be prefixed with bib-. Overloaded functions must be followed with directive (:mode bib-example?), in which example is the name of the current style.
Our complete example file example.scm is as follows:
(texmacs-module (bibtex example)
(:use (bibtex bib-utils) (bibtex plain)))
(bib-define-style "example" "plain")
(tm-define (bib-format-date e)
(:mode bib-example?)
(bib-format-field e "year")) |
It behaves in a similar way as the plain style, except that all dates are formatted according to our custom routine.
With the database tool, TeXmacs only uses its internal styles for the names returned by (bib-standard-styles); other style names are passed to BibTEX. (Without the database tool, any style tm-name is loaded directly.) In order to make a new style available in both cases, one therefore also has to extend this list, for instance in the style file itself or in my-init-texmacs.scm:
(tm-define (bib-standard-styles)
(append (former) (list "tm-example"))) |
The style module (bibtex example) is loaded automatically when a bibliography with style tm-example is generated.
This function declares a style called name (string) with fall-back style default (string). The style is selected by choosing tm-name when adding a bibliography to a document. Whenever a formatting function is not defined in the current style, its definition in the fall-back style is used as replacement. This macro defines a TeXmacs mode whose predicate bib-name? can be used in (:mode …) clauses of tm-define.
This function applies the function fun to the arguments args as if the current style were style (string), and returns the result.
This function creates a TeXmacs tree corresponding to the field field (string) of entry entry without format. In some cases, the output is special:
If field is "author" or "editor", we return a tree with label bib-names followed by a list of author names; each author name is a tree with label bib-name containing four elements: first name, particule (von), last name and suffix (jr);
If field is "pages", then we return a tree with label bib-pages, followed by one or two page numbers (as strings) corresponding to a single page or a pages interval. If the field could not be parsed, then the single page number is "0".
This function creates a TeXmacs tree corresponding to the field field (string) of entry entry, with basic format: the first letter is put in upper case, except inside keep-case blocks. The empty string is returned if the field is empty or absent. The variants bib-format-field-preserve-case and bib-format-field-locase-first respectively preserve the case and put the first letter in lower case.
This function is similar to bib-format-field, but fields are formatted in lower case with an upper case letter at the beginning.
This function returns boolean #t if the field field (string) of entry entry is empty or absent; it returns #f in the other cases.
This function creates a TeXmacs tree consisting of a block containing TeXmacs tree tm, with its first letter in upper case and a period at the end. The variant bib-new-case-preserved-block does not change the case.
This function creates a TeXmacs tree which is the concatenation of all the elements of list ltm separated with TeXmacs tree sep.
This function is equivalent to the evaluation of (bib-new-list " " ltm).
This function creates a TeXmacs tree corresponding to a sentence containing all the elements of list ltm separated by commas. Empty elements are skipped. The variant bib-new-case-preserved-sentence does not put the first letter in upper case.
This function creates a TeXmacs tree corresponding to the abbreviation of the name contained in name TeXmacs tree: it retrieves the list of first letters of each word, followed by dot (TeXmacs tree) and separated by spc (TeXmacs tree).
This function creates a TeXmacs tree with a dot at the end of tm.
(bib-default-preserve-case tm)
These functions simplify the TeXmacs tree tm; the second one also puts its first letter in upper case (except inside keep-case blocks). They are used by the bib-format-field family of routines.
This function creates a TeXmacs tree corresponding to the italic version of tm.
This function creates a TeXmacs tree, which is equal to tm with all letters in lower case, except for those within keep-case blocks.
This function returns a string containing the first nbcar characters of tm.
This function creates a TeXmacs tree, which is equal to tm with all letters in upper case, except for those within keep-case blocks.
This function creates a TeXmacs tree, which is equal to tm with its first letter in upper case, except inside keep-case blocks. Similarly, (bib-locase-first tm) puts the first letter in lower case.
This function returns boolean #t if value v is empty (the empty string, the empty list, or a with whose body is empty); it returns #f in the other cases.
This function returns a string made of all letters of the TeXmacs tree tm.
This function returns a TeXmacs tree corresponding to the simplification of TeXmacs tree tm.
This function returns the length of TeXmacs tree tm.
This function returns the markup (localize msg), which translates the string message msg from English into the language of the document.
This function returns the list of the names (with the tm- prefix) of the styles which are handled by the Scheme implementation.