![]() |
Source tracking and conservative conversion | ![]() |
A user who collaborates with LaTeX users may want to import a LaTeX file, edit it in TeXmacs and export it again, or export a TeXmacs document, let a colleague edit the LaTeX file and import it back. With the plain converters, each conversion rewrites the whole document: the formatting of the source, the macros of the author and many small details are lost. TeXmacs therefore implements two complementary mechanisms, written in C++ in
Data/Convert/Tex
:
During a conversion, markers are inserted into the source document. They survive the conversion and indicate which part of the result comes from which part of the source. The correspondence is stored together with the result.
When a document which was obtained by a tracked conversion is converted back, the parts which were not modified are replaced by the corresponding parts of the original source, and only the modified parts are really converted.
Both mechanisms are controlled by preferences (menu: preferences dialog, tab Convert, LaTeX, section “Conservative conversion options”):
|
||||||||||||||
The dialog sets the import and export variants of each preference together. The C++ code reads them with get_preference.
Defined in
tracked_fromtex.cpp
. If "latex->texmacs:source-tracking" is off, this is just latex_document_to_tree. Otherwise:
latex_mark inserts the markers {\blx{i}} and {\elx{i}} around the paragraphs and environments of the source, where i is a character position in the source. Regions in which markers may not be inserted (verbatim environments, …) are protected beforehand by latex_protect.
The marked source is imported with latex_document_to_tree. Since \blx and \elx are unknown commands, they become tags blx and elx with one argument.
texmacs_group_markers transforms each concatenation which starts with blx and ends with elx into a tag (mlx "b:e" body), which records that body comes from the characters b to e of the source; texmacs_correct_markers and texmacs_clean_markers remove inconsistent and remaining markers.
If "latex->texmacs:transparent-source-tracking" is on, the document is also imported without markers, and the results are compared (texmacs_unmark). Markers which changed the result are declared invalid (texmacs_declare_transparent, texmacs_check_transparency) and the process is repeated without them.
The result is the unmarked document, with two attachments: the original source (latex-source) and the marked document (latex-target).
The attachments are saved with the TeXmacs document, so that the correspondence remains available in later sessions.
Defined in
conservative_totex.cpp
. If "texmacs->latex:conservative" is off, or if the document has no latex-source attachment, the function simply calls tracked_texmacs_to_latex. Otherwise:
If the document is identical to the unmarked latex-target, the original source is returned unchanged.
texmacs_invarianted looks for subtrees of the document which also occur in the old target inside an mlx marker, choosing the best match when a subtree occurs several times (texmacs_best_match, which uses the neighbors of the subtree). These subtrees are replaced by (ilx "b:e"), adjacent invariant paragraphs are merged (texmacs_invarianted_merge), and finally texmacs_invarianted_replace substitutes the corresponding source text: (!ilx source).
The resulting document is exported normally; the LaTeX converter turns !ilx into (!invariant source) (tmtex-ilx), which texout writes verbatim. During this export, latex-set-virtual-packages declares the packages used by the original source, so that the converter does not add them again.
The metadata, the abstract and the preamble are merged with those of the original source (latex_merge_metadata, latex_merge_abstract, latex_merge_preamble), so that the document class, the packages and the declarations of the user are preserved.
Defined in
tracked_totex.cpp
. After the macro expansion by latex_expand, the document is exported by tree_to_latex_document if "texmacs->latex:source-tracking" is off. Otherwise tracked_tree_to_latex_document is called:
texmacs_mark wraps each paragraph of the body into (mtm "path" paragraph), where the path is encoded as a comma-separated list of integers (encode_as_string).
The LaTeX converter translates mtm into the markers {\btm{path}} and {\etm{path}} (tmtex-mtm and texout-marker).
latex_unmark removes the markers from the generated LaTeX and records the positions at which they occurred.
If "texmacs->latex:transparent-source-tracking" is on, the unmarked result is compared with an export without markers; markers which change the output are invalidated (latex_declare_transparent, latex_check_transparency) and the export is repeated.
tracked_tree_to_latex_document returns false on success. In this case, tracked_texmacs_to_latex appends to the LaTeX output a comment block delimited by %%%%%%%%%% Begin TeXmacs source and %%%%%%%%%% End TeXmacs source, which contains, encoded in base64, the TeXmacs document (in Scheme syntax, without the private attributes, see purify) followed by the marked LaTeX output. If the markers cannot be made transparent, the plain output is returned without this block.
Defined in
conservative_fromtex.cpp
. If "latex->texmacs:conservative" is off, or if s does not end with a block Begin TeXmacs source (get_texmacs_attachments), this is tracked_latex_to_texmacs. Otherwise the block is decoded into the original TeXmacs document and the marked LaTeX which was generated from it, and:
latex_correspondence computes the positions of the paragraphs of the original document in the generated LaTeX. If the LaTeX file was not modified, the original document is returned.
latex_invarianted searches the modified LaTeX for the unmodified pieces of the generated LaTeX and replaces them by {\itm{path}}.
The result is imported with tracked_latex_to_texmacs, and latex_invarianted_replace replaces the resulting itm tags by the corresponding subtrees of the original document.
If the style, the preamble, the metadata or the abstract were not changed, they are restored from the original document (texmacs_recover_style, texmacs_recover_preamble, texmacs_recover_metadata, texmacs_recover_abstract); otherwise the preambles are merged (texmacs_merge_preamble).
Since the encoded document is stored with paths into the body, upgrade_texmacs_attachments also upgrades documents written with older versions of TeXmacs and adapts the paths.
The markers are also used by try_latex_export (
latex_recover.cpp
), called by the Tools\(\rightarrow\)LaTeX\(\rightarrow\)Run command. It exports the buffer with markers, runs pdflatex, parses the log file (get_latex_errors) and, for each error, finds the position in the LaTeX source (latex_error_find) and the corresponding path in the TeXmacs document (texmacs_error_find). The widget of
convert/latex/tmtex-widgets.scm
uses this information to show the error next to the offending markup.
The whole mechanism works at the granularity of paragraphs and environments; a small change inside a long paragraph causes the whole paragraph to be converted again.
The transparency checks multiply the cost of a conversion, since the document is converted several times.
The preference "texmacs->latex:attach-tracking-info", shown in the dialog as “Store tracking information in LaTeX files”, is not consulted by tracked_texmacs_to_latex in this version: the information is appended whenever source tracking is on and succeeds.
A similar conservative mechanism exists for BibTeX (
Data/Convert/BibTeX/conservative_bib.cpp
, preference "texmacs->bibtex:conservative").