# Footnotes and references

> Footnotes and endnotes, bookmarks, cross-references, captions, tables of contents and figures, and fields in Birch UI Document Editor.

The **References** tab and the **Insert → Links** group hold everything that points from one place in a document to another. Each command is also available from code.

## Footnotes and endnotes

```ts
editor.exec('insertFootnote', 'See Schedule 1.'); // at the cursor; without text the note opens for typing
editor.exec('insertEndnote', { text: 'Incoterms 2020.' });
editor.getFootnotes(); // [{ id, kind: 'footnote', number: 1, label: '1', pos, content }, …]
editor.getFootnotes('endnote');
editor.exec('nextFootnote'); // 'previousFootnote' goes back; pass 'endnote' to walk endnotes only
editor.exec('noteFormat', { footnote: 'lowerRoman', endnote: 'decimal' });
editor.exec('convertNotes', 'footnotesToEndnotes'); // or 'endnotesToFootnotes', 'swap'; no value converts the selected note
editor.exec('deleteNote'); // the selected note
```

A note is an inline `footnote` node with `kind` and its own block `content`. In page layout a footnote is set at the bottom of the page that holds its reference, under a separator line, and takes room from the body text of that page; a note that does not fit continues on the next page. Double-click a note, or press <kbd>Enter</kbd> on its reference, to edit it in place. `getPageInfo(n).footnotes` tells you how many notes a page carries.

The number formats are `decimal`, `lowerRoman`, `upperRoman`, `lowerLetter`, `upperLetter` and `chicago` (\*, †, ‡ …). Set the starting formats with the `noteNumberFormat` option. **Note options** in the ribbon opens the same settings as a dialog.

The **Note options** dialog on the References tab (`noteOptionsDialog`) sets where notes go (footnotes at the bottom of the page or below the text, endnotes at the end of the section or of the document), the number format, the first number and whether numbering restarts in each section or on each page. `exec('noteSettings', { footnote, endnote })` applies the same settings from code, and they are written to `w:footnotePr` and `w:endnotePr`.

A footnote whose reference sits inside a list item or a table cell is set at the bottom of the page that holds that item or row, numbered in document order with the others. Endnotes are collected after the last paragraph; the list is paginated with the text, so it continues over as many pages as it needs. Endnotes at the end of each section are not supported: they always follow the end of the document.

## Bookmarks

```ts
editor.setSelection(120, 164);
editor.exec('insertBookmark', 'payment_terms'); // up to 40 letters, digits and underscores, not starting with a digit
editor.getBookmarks(); // [{ name, id, from, to, hidden }]
editor.goToBookmark('payment_terms');
editor.exec('deleteBookmark', 'payment_terms');
editor.exec('showBookmarks', true); // brackets around bookmarked ranges; also the showBookmarks option
```

`getBookmarks(true)` includes the hidden bookmarks that Word creates for tables of contents and cross-references (names that start with an underscore).

## Cross-references

```ts
editor.exec('insertCrossReference', { target: 'bookmark', name: 'payment_terms', display: 'page' });
editor.exec('insertCrossReference', { target: 'heading', index: 0, display: 'text', hyperlink: true });
```

`target` is `heading`, `bookmark`, `figure`, `table`, `equation` or `footnote`; `index` picks the n-th heading, caption or note and `name` a bookmark. `display` is `text`, `page`, `number` or `aboveBelow`. A cross-reference is a field (`REF` or `PAGEREF` in DOCX), so it shows the value from the last update.

## Captions and the table of figures

```ts
editor.exec('insertCaption', { label: 'Table', text: 'Fees by service', position: 'above' });
editor.exec('insertTableOfFigures', 'Table'); // lists every caption with that label
```

With the cursor in a table the label defaults to "Table", with an equation selected to "Equation" and otherwise to "Figure". Captions are numbered with a `SEQ` field per label.

## Citations and bibliography

A document keeps a list of sources. A citation names one or more of them by tag, and a bibliography lists them all. Both are written from the sources in one of three styles: APA, MLA or Chicago (author-date).

On the **References** tab, **Insert citation** lists the sources of the document and ends with **Add new source…**, **Manage sources** opens the list with New, Edit, Delete and Insert citation, **Citation style** switches the style of every citation and bibliography, and **Bibliography** inserts a heading and the list below the current paragraph.

```ts
const tag = editor.addSource({
  type: 'JournalArticle',
  authors: [{ last: 'Brandt', first: 'Daniel' }],
  title: 'Slot booking and yard congestion',
  journalName: 'Journal of Freight Operations',
  year: '2021',
  volume: '14',
  issue: '3',
  pages: '201-219',
}); // 'Bra21'

if (tag) {
  editor.insertCitation(tag); // (Brandt, 2021)
  editor.insertCitation({ tags: tag, pages: '204' }); // (Brandt, 2021, p. 204)
}
editor.insertBibliography();
editor.setCitationStyle('chicago'); // rewrites every citation and the bibliography
```

| Style | Citation in the text | Entry |
| --- | --- | --- |
| `apa` | (Brandt, 2021, p. 204) | Brandt, D. (2021). Slot booking and yard congestion. *Journal of Freight Operations, 14*(3), 201-219. |
| `mla` | (Brandt 204) | Brandt, Daniel. “Slot booking and yard congestion.” *Journal of Freight Operations*, vol. 14, no. 3, 2021, pp. 201-219. |
| `chicago` | (Brandt 2021, 204) | Brandt, Daniel. 2021. “Slot booking and yard congestion.” *Journal of Freight Operations* 14 (3): 201-219. |

Adding, editing or removing a source and changing the style rewrite the citations and bibliographies at once. <kbd>F9</kbd> and `updateFields()` do the same, which matters after an undo: the list of sources is not part of the undo history, the text is. `formatCitation(request, sources, style)` and `formatBibliographyEntry(source, style)` are exported for use without an editor.

A citation is a `CITATION` field inside a citation content control and a bibliography is a `BIBLIOGRAPHY` field inside a bibliography content control, as Word writes them, so Word can edit and update both. The sources are written to the sources part of the DOCX file (`customXml/item1.xml` in a new file). The sources of a Word file are read from that part, the style selected in Word is kept, and elements of a source that the editor does not model (for example `b:LCID`, translators and comments) are written back unchanged. The source types are `Book`, `BookSection`, `JournalArticle`, `ArticleInAPeriodical`, `ConferenceProceedings`, `Report`, `InternetSite`, `DocumentFromInternetSite` and `Misc`; other types of a Word file are kept and formatted like `Misc`.

## Table of contents

```ts
editor.exec('insertTableOfContents');
editor.exec('tableOfContentsOptions', { maxLevel: 2, showPages: true, hyperlinks: true, leader: 'dot' });
editor.exec('updateTableOfContents');
```

The table lists the headings down to `maxLevel` with their page numbers. It does not follow edits by itself: update it from its menu, with <kbd>F9</kbd>, or with `updateFields()`.

## Fields

```ts
editor.insertField('client.name'); // a merge field; see Mail merge
editor.exec('insertPageNumber'); // PAGE, for headers and footers
editor.exec('insertPageCount'); // NUMPAGES
editor.exec('insertDateField', 'd MMMM yyyy'); // DATE with a Word date picture
editor.exec('toggleFieldCodes'); // show { PAGEREF payment_terms } instead of results
const changed = editor.updateFields(); // recomputes dates, sequence numbers, references, formulas and tables of contents
editor.on('fieldsUpdate', ({ count }) => {});
```

`PAGE` and `NUMPAGES` are filled in on every page of a header or footer. A `PAGE` field placed in body text is not resolved. Fields the editor does not compute keep their instruction and their last result, and are written back to DOCX unchanged.

The DOM-free `updateFields(data, context)`, `parseFieldInstruction(text)` and `formatDatePicture(date, picture)` functions do the same work on document JSON; see [DOCX import and export](https://docs.birchui.com/document/docx.md).
