GNU TeXmacsUser manualReferenceAboutEnglish·Français
Converters to other data formats

TeXmacs comes with converters between its own format and LaTeX, HTML (including MathML), XML (e.g. the TeXmacs XML format tmml), BibTeX, Scheme (the stm format), plain text, several programming languages and Coq, as well as export to PDF, PostScript and various image formats. Most converters are imperfect, and there is always room for improvement. This chapter briefly describes how the conversion machinery is organized and gives some recommendations based on our experience with the implementation of the existing converters. For practical instructions on how to add a new format, we refer to the chapter Adding new data formats and converters.

1.Organization of the converters

1.1.Formats and the converter graph

Data formats and converters are declared in Scheme, using the macros define-format and converter from

progs/kernel/texmacs/tm-convert.scm

. For instance,

progs/convert/latex/init-latex.scm

contains declarations such as

(define-format latex
  (:name "LaTeX")
  (:suffix "tex")
  (:recognize latex-recognizes?))

(converter latex-document latex-tree
  (:function parse-latex-document))

(converter latex-tree texmacs-tree
  (:function latex->texmacs))

Each format fm gives rise to several “variants”: the file fm-file, the document fm-document and the snippet fm-snippet (as strings), and the parsed forms fm-tree (a C++ tree) or fm-stree (a Scheme tree). The macro define-format automatically adds converters between fm-file and fm-document. The converters form a directed graph with weighted edges (the weight can be set with the :penalty option); the function converter-search finds the cheapest path between two formats, and convert applies the successive conversions along this path. Options for converters are declared with :option and are stored as preferences, such as "texmacs->latex:expand-macros". A converter may also be implemented by an external program (option :shell), in which case it is only enabled when the program is found in the path. The routines generic_to_tree and tree_to_generic in

Data/Convert/Generic/generic.cpp

allow C++ code to call the converters from Scheme.

1.2.Where to find the converters

The parsers and the more performance critical parts of the converters are written in C++ and can be found in the directory

src/src/Data/Convert

:

Texmacs

Parsing and printing the native TeXmacs format (

fromtm.cpp

,

totm.cpp

) and the upgrade of documents written with older versions of TeXmacs (

upgradetm.cpp

).

Scheme

Conversion between trees and Scheme expressions.

Tex

The LaTeX parser (

parsetex.cpp

), the conversion of the parsed LaTeX into TeXmacs (

fromtex.cpp

,

fromtex_post.cpp

), the importation of metadata for various journal styles (metadata*.cpp) and the “conservative” and “tracked” converters, which attach source tracking information to the exported LaTeX so that it can be re-imported with minimal changes (conservative_*.cpp, tracked_*.cpp).

Xml

Parsers for XML and HTML (

parsexml.cpp

,

parsehtml.cpp

) and some cleaning routines.

BibTeX

The BibTeX parser and the conservative import and export of bibliographies.

Verbatim

Conversion from and to plain text.

Coq

A parser for the Coq vernacular.

Generic

Glue with the Scheme converters, indexing and postprocessing.

The remaining parts of the converters are written in Scheme and can be found in

src/TeXmacs/progs/convert

. For instance,

convert/latex/tmtex.scm

implements the conversion from TeXmacs to LaTeX and

convert/latex/texout.scm

the serialization of LaTeX;

convert/html/htmltm.scm

and

convert/html/tmhtml.scm

implement the conversions from resp. to HTML, and

convert/mathml

the conversions from and to MathML. Plug-ins may define additional formats (see for instance the files *-format.scm in the plug-ins for programming languages).

2.Parsing extern data formats

In order to write a converter from LaTeX, HTML, XML, etc. to TeXmacs, a good first step is to write a parser for the extern data format. For HTML, XML, etc. this should be rather easy, but for LaTeX, you will probably need to be a real LaTeX guru. We recommend the result of the parsing step to be a Scheme expression (as is the case for the HTML and XML parsers, which produce html-stree resp. xml-stree expressions), because this language is very well adapted for the implementation of the actual converter.

This first step should be able to process any correct file having the extern data format; possible incompatibilities should only come into play during the actual conversion. In the case of LaTeX, one should not expand the macros and keep all macro definitions, because TeXmacs will be able to take advantage out of this.

3.The actual converter

We recommend the actual converter to proceed in several steps. Often it is convenient to start with a rough, structural, conversion step, which is “polished” by a certain number of additional steps. These additional steps may take care of some very particular layout issues which can not be treated conveniently at the main step. For instance, the C++ function latex_to_tree in

Data/Convert/Tex/fromtex_post.cpp

successively filters the preamble, converts the parsed LaTeX into a rough TeXmacs tree, finalizes the document structure (paragraphs, sections, algorithms, matrices), handles the preamble and matching environments, upgrades the result to the current TeXmacs conventions and finally corrects the tree with respect to the DRD.

Actually, the main difficulties usually come from exceptional text, like verbatim, and layout issues which are handled differently in the extern data format and TeXmacs. A good example of such a difference between LaTeX and TeXmacs is the way equations or lists are handled. Consider for instance the following paragraph:

Text before.

\(\displaystyle a^2 + b^2 = c^2 .\)

Text after.

In LaTeX, the equation is really seen as a part of the paragraph. Indeed, there will not be any blank line between “Text before” and the equation. However, for efficiency reasons, it is better to see the paragraph as three paragraphs in TeXmacs, because the lines can be typeset independently. Nevertheless, the equation environment will disable the indentation of “Text after”.

As a result of this anomaly, converted texts have to be postprocessed, so as to insert paragraph breaks at strategic places (in the LaTeX importer, this is done by finalize_layout and make_paragraphs in

fromtex_post.cpp

). It should be noticed that this step may be independent from the format which is actually being converted and that a similar reverse step may be implemented for backward conversions. We also notice that one needs an exhaustive list of all similar exceptional environments for this postprocessing step. From a semantical point of view, one should also be able to detect that the above example logically forms only one and not three paragraphs.

4.Backward conversions

Conversions from TeXmacs to an extern data format are usually easier to implement, because the TeXmacs data format is semantically rich. However, conversions to an extern data format without a TeX-like macro facility give rise to the problem of macro expansion of non supported TeXmacs functions or environments. The existing converters address this problem in several ways: the LaTeX exporter can expand macros or keep them as LaTeX definitions (see the options "texmacs->latex:expand-macros" and "texmacs->latex:expand-user-macros"), while the HTML exporter expands the document before the conversion (see

convert/html/tmhtml-expand.scm

). Graphics and other constructs without a counterpart in the target format may also be exported as images (see for instance the option "texmacs->html:images").

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