GNU TeXmacsUser manualReferenceAboutEnglish
Source tracking and conservative conversion

1.Purpose

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

:

Source tracking

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.

Conservative conversion

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”):

Preference Default
latex->texmacs:source-tracking off
latex->texmacs:conservative off
latex->texmacs:transparent-source-tracking off
texmacs->latex:source-tracking off
texmacs->latex:conservative on
texmacs->latex:transparent-source-tracking on

Table 1. Preferences for source tracking (defaults from

init-latex.scm

).

The dialog sets the import and export variants of each preference together. The C++ code reads them with get_preference.

2.Tracked import

tree tracked_latex_to_texmacs (string s, bool as_pic)
(import with source tracking)

Defined in

tracked_fromtex.cpp

. If "latex->texmacs:source-tracking" is off, this is just latex_document_to_tree. Otherwise:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

3.Conservative export

string conservative_texmacs_to_latex (tree doc, object opts)
(export reusing the original source)

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:

  1. If the document is identical to the unmarked latex-target, the original source is returned unchanged.

  2. 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).

  3. 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.

  4. 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.

4.Tracked export

string tracked_texmacs_to_latex (tree doc, object opts)
(export with source tracking)

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:

  1. 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).

  2. The LaTeX converter translates mtm into the markers {\btm{path}} and {\etm{path}} (tmtex-mtm and texout-marker).

  3. latex_unmark removes the markers from the generated LaTeX and records the positions at which they occurred.

  4. 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.

5.Conservative import

tree conservative_latex_to_texmacs (string s, bool as_pic)
(import reusing the original document)

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:

  1. 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.

  2. latex_invarianted searches the modified LaTeX for the unmodified pieces of the generated LaTeX and replaces them by {\itm{path}}.

  3. 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.

  4. 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.

6.Error localization

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.

7.Remarks

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".