![]() |
Labels, references and the reference table | ![]() |
Labels and references combine two mechanisms: loci, for navigation and printed hyperlinks, and the reference table of the buffer, for the numbers and page numbers which are displayed.
The macros label, reference, pageref, hlink and action are defined in C++ (
Typeset/Env/env_default.cpp
), so they exist in every style:
A locus with identifier (hard-id Id) and a link (link "anchor" (id "#Id")), whose body is a flag and (set-binding Id (value "the-label")).
A locus with identifier (hard-id Id) and a link (link "hyperlink" (id hard-id) (url "#Id")), whose body is (get-binding Id). pageref is the same with (get-binding Id 1).
A locus with identifier (hard-id body) and a hyperlink to (url destination).
A locus with an action link to a script.
hard-id (edit_env_rep::exec_hard_id) builds an identifier starting with % from the address of the environment and of the expanded argument (and a hash of the argument if it is not accessible). Such identifiers are stable while a document is open but not across sessions, which is fine for these loci: they are only used to make the reference clickable. The style package preview-ref (
packages/utilities/preview-ref.ts
) redefines reference and pageref with an additional mouse-over link to the script preview-reference, which shows the labelled content in a tooltip.
exec_set_binding (
Typeset/Env/env_exec.cpp
) stores the value of a label in the table env->local_ref as a tuple (value page), keeping the previous page number, and appends a third component when the label comes from an included file or a part of a project (file#part). The page numbers are filled in after page breaking by typesetter_rep::determine_page_references, which collects the tag boxes created for each set-binding. exec_get_binding reads local_ref, falling back to global_ref, and records missing keys.
The editor typesets its document repeatedly (edit_typeset_rep::typeset) until no key is missing or redefined, or until no progress is made, after which the remaining missing and redefined keys are reported. Unused entries are removed (clean_unused).
The tables of the environment are references to tables of the buffer data, bound once when the editor is created (edit_typeset_rep::edit_typeset_rep):
the fields ref, aux and att of buf->data, which are shared by all views and saved with the document (see the buffer classes);
the same tables of the project buffer if the buffer belongs to a project, so that a chapter sees the labels of the whole book; otherwise a private table grefs for references and the buffer's own tables for the others.
The table is accessible from Scheme through the editor glue: get-reference, set-reference, reset-reference, list-references (with a starred variant using the project table), find-references (the keys whose value is a given string), and similarly get-auxiliary, set-auxiliary, list-auxiliaries for the auxiliary table (tables of contents, bibliographies, indices).
edit_cursor_rep::search_label (s, local) (label->path) looks for the label in the document tree, then in the boxes (find_tag, for labels generated by macros), and, unless local is set, uses the third component of the reference table entry, or the project, to find the file which contains the label; that file is then searched in a passive view. go_to_label (go-to-label) moves the cursor there, loading the file and showing the right part of a project if needed. This is what the #Id targets of references lead to.
ref-edit.scm)
Searching: search-labels, search-references, search-citations, search-duplicate-labels, search-broken-references (references to unknown labels, taking the project into account) and search-broken-citations, with the corresponding navigation commands (go-to-duplicate-label, go-to-broken-reference, …).
Inferred references: when a reference is typed with a number instead of a key (for instance thm:3 or 3 after the word “theorem”), number->label looks the number up in the reference table (find-references) and picks the label whose environment matches; the key is substituted when the reference is validated with return.
Previews: ref-preview builds the tooltip shown by preview-reference, and a keyboard-press extension shows it while a reference is being typed.
ref-markup.scm
implements the “smart” references of the style packages (ext-smart-ref, ext-typed-ref), which group several keys by type and render them as, for instance, “Theorems 1 and 2”.