GNU TeXmacsUser manualReferenceAboutEnglish
The LaTeX and HTML converters

1.Introduction

The general machinery for data formats and converters (the macros define-format and converter, the converter graph, converter-search and convert, converter options stored as preferences) is described in the chapter on converters to other data formats. The present chapter goes one level deeper and explains how the two most important converters actually work: the converters between TeXmacs and LaTeX and between TeXmacs and HTML (including MathML), in both directions. The user-level description of these converters can be found in the chapters converters for LaTeX and converters for HTML.

All C++ file names below are relative to

src/src/

and all Scheme file names are relative to

src/TeXmacs/progs/

.

2.Overview of the four pipelines

Each converter is a chain of stages, each of which works on a specific data representation. In Scheme, TeXmacs documents are manipulated as strees (Scheme trees such as (frac "1" "2"), see tree->stree), HTML and XML documents as sxml expressions (such as (h:p (@ (class "x")) "text")), and LaTeX documents as LaTeX strees with special labels starting with ! (such as (!concat (textbf "a") "b")). The parsed LaTeX produced by the C++ parser is yet another representation: a C++ tree made of tuple nodes whose first child is the name of the command, such as (tuple "\frac" "1" "2").

TeXmacs \(\rightarrow\) LaTeX

Macro expansion of the unsupported markup in the editor (edit_typeset_rep::exec_latex), optional source tracking and conservative merging in C++ (

Data/Convert/Tex/conservative_totex.cpp

,

tracked_totex.cpp

), then the Scheme converter texmacs->latex (

convert/latex/tmtex.scm

) which maps the TeXmacs stree to a LaTeX stree, and finally the serializer serialize-latex (

convert/latex/texout.scm

) which also builds the preamble with the help of

convert/latex/latex-tools.scm

.

LaTeX \(\rightarrow\) TeXmacs

The C++ parser parse_latex / parse_latex_document (

Data/Convert/Tex/parsetex.cpp

) which produces parsed LaTeX, the translation parsed_latex_to_tree (

Data/Convert/Tex/fromtex.cpp

) and the post-processing driver latex_to_tree (

Data/Convert/Tex/fromtex_post.cpp

). The knowledge about LaTeX commands (types and arities) lives in Scheme tables in convert/latex/latex-*-drd.scm, which the C++ code queries through latex_type and latex_arity. Optional source tracking and conservative re-import are implemented in

tracked_fromtex.cpp

and

conservative_fromtex.cpp

.

TeXmacs \(\rightarrow\) HTML

Macro expansion in the editor (edit_typeset_rep::exec_html), the Scheme converter texmacs->html (

convert/html/tmhtml.scm

) which produces sxml, possibly using texmacs->mathml (

convert/mathml/tmmath.scm

) or the LaTeX converter (for MathJax) for formulas, and the serializer serialize-html (

convert/html/htmlout.scm

).

HTML \(\rightarrow\) TeXmacs

The C++ parser parse_html (

Data/Convert/Xml/parsehtml.cpp

and

parsexml.cpp

) which produces sxml, namespace normalization htmltm-parse (

convert/tools/xmltm.scm

), the converter html->texmacs (

convert/html/htmltm.scm

) with MathML handled by

convert/mathml/mathtm.scm

, and the C++ cleaning pass clean_html (

Data/Convert/Xml/cleanhtml.cpp

).

The formats are declared in

convert/latex/init-latex.scm

and

convert/html/init-html.scm

; these files are loaded lazily by lazy-format in

init-texmacs.scm

. The tables below summarize the converters which they declare.

From To Function
texmacs-stree latex-stree texmacs->latex
latex-stree latex-document, latex-snippet serialize-latex
texmacs-stree latex-document conservative-texmacs->latex
latex-document latex-tree parse-latex-document
latex-snippet latex-tree parse-latex
latex-tree texmacs-tree latex->texmacs
latex-document texmacs-tree latex-document->texmacs
latex-class-document texmacs-tree latex-class-document->texmacs
texmacs-stree html-stree texmacs->html
html-stree html-document, html-snippet serialize-html
html-document html-stree parse-html-document
html-snippet html-stree parse-html-snippet
html-stree texmacs-stree html->texmacs

Table 1. Converters declared in

init-latex.scm

and

init-html.scm

.

Since every edge has the same default penalty, the converter graph prefers the direct edges: exporting a document to latex-document uses conservative-texmacs->latex (one step) rather than texmacs->latex followed by serialize-latex (two steps), and importing a latex-document uses latex-document->texmacs. Snippets (used for copy and paste) go through latex-stree resp. latex-tree.

3.Interaction with the editor

Exporting a buffer (export-buffer-main in

texmacs/texmacs/tm-files.scm

) calls the C++ routine buffer_export in

Texmacs/Data/new_buffer.cpp

. For the HTML format, this routine first replaces the body by exec_html of the body, so that all macros which HTML does not know are expanded using the environment of the document (the style, the packages and the user's macros). For LaTeX, it only attaches a view attribute to the document: the expansion is done later, inside the conversion itself, by latex_expand which retrieves the view and calls exec_latex. The resulting document is passed to export_tree, which calls tree_to_generic and hence the Scheme converters. Image file names produced during the export are derived from current-save-target: buffer_export and export_tree set this variable and current-save-source for the time of the conversion only, and restore them afterwards.

Importing goes through generic_to_tree with the format latex-document or html-document. The same converters are used for copying and pasting in the corresponding formats (with the snippet variants).

4.Source map

convert/latex/init-latex.scm

Format declarations, converters and their options for LaTeX.

convert/latex/tmtex.scm

The TeXmacs to LaTeX stree converter.

convert/latex/tmtex-*.scm

Style-specific variants for journal styles (

tmtex-acm.scm

,

tmtex-ams.scm

,

tmtex-beamer.scm

,

tmtex-elsevier.scm

,

tmtex-ieee.scm

,

tmtex-revtex.scm

,

tmtex-springer.scm

) and the debugging widget

tmtex-widgets.scm

.

convert/latex/texout.scm

Serialization of LaTeX strees.

convert/latex/latex-tools.scm

Preamble construction: package management, macro definitions, catcodes, page size, colors.

convert/latex/latex-command-drd.scm
,
latex-symbol-drd.scm
,
latex-texmacs-drd.scm
,
latex-drd.scm

Logical tables describing LaTeX commands, symbols, the extra LaTeX macros introduced by TeXmacs, package dependencies and paper sizes.

convert/latex/latex-define.scm
,
latex-overload.scm

Definitions (as LaTeX strees) of the extra macros and environments which TeXmacs puts in the preamble.

Data/Convert/Tex/

The LaTeX parser (

parsetex.cpp

), the importer (

fromtex.cpp

,

fromtex_post.cpp

), the importer for class files (

fromcls.cpp

), the bridge to the Scheme tables (

inittex.cpp

), metadata import (metadata*.cpp), tools on LaTeX sources (

latex_tools.cpp

), error recovery and pdflatex runs (

latex_recover.cpp

), source tracking and conservative conversion (tracked_*.cpp, conservative_*.cpp).

Plugins/LaTeX_Preview/

Rendering of LaTeX fragments as pictures during the import.

convert/html/init-html.scm

Format declaration and converters for HTML.

convert/html/tmhtml.scm
,
tmhtml-expand.scm
,
htmlout.scm

The export to HTML.

convert/html/htmltm.scm

The import from HTML.

convert/mathml/

Export (

tmmath.scm

) and import (

mathtm.scm

,

mathml-drd.scm

) of MathML.

convert/tools/

Shared tools: sxml accessors (

sxml.scm

,

sxhtml.scm

), XML import helpers (

xmltm.scm

), construction of TeXmacs strees (

stm.scm

,

tmconcat.scm

), lengths, colors and tables (

tmlength.scm

,

tmcolor.scm

,

tmtable.scm

,

old-tmtable.scm

), CSS (

css.scm

), the output buffer used by the serializers (

output.scm

) and a pre-processor for the LaTeX export (

tmpre.scm

).

Data/Convert/Xml/

The XML/HTML parser (

parsexml.cpp

,

parsehtml.cpp

) and the cleaning of imported HTML (

cleanhtml.cpp

).

doc/tmweb.scm

Conversion of whole directories into a web site.

5.Contents of this chapter

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