LaTeXMT: Machine Translation for LaTeX Documents

Calvin Hoy, Samuel Frontull, Georg MoserEMNLP 2025

TL;DR

Translating LaTeX documents is a challenge because standard machine translation tools struggle with its complex markup. This paper introduces LaTeXMT, a system that separates LaTeX structure from text, translates the text, and then intelligently reinserts the original structure and markup, enabling accurate and compilable translations.

What Problem Does This Paper Address?

Machine translation (MT) has made incredible strides, largely thanks to transformer models. However, these tools are primarily designed for plain text and falter when faced with structured documents like those written in LaTeX. LaTeX files contain not just text but also a rich set of commands, environments, and macros that define formatting, structure, and even non-textual content (like mathematical equations). Previous attempts to translate LaTeX documents either worked on already compiled output (losing source information), used pre-processors that might strip critical semantic elements, or couldn't distinguish between translatable text and structural markup. This leaves a significant gap for researchers and academics who rely heavily on LaTeX for scientific writing and need to translate their work.

Methods

LaTeXMT employs a multi-stage pipeline to tackle this problem:

  1. Parsing: The system first parses the LaTeX document into a nested node list using the pylatexenc library. Each node represents a LaTeX element, such as plain text, a macro, an environment, or a comment.
  2. Text Extraction & Masking: It then recursively traverses this node list to identify contiguous blocks of text. Crucially, non-textual elements (like inline math or specific formatting macros) are identified and replaced with placeholders (masked) to prevent them from interfering with the translation process. This masking is essential because standard MT models often struggle with or misinterpret these elements.
  3. Markup-Annotated String: The plain text portions, along with their original markup ranges, are converted into a "Markup-annotated String." This flattens the structure while preserving information about where markup should be reinserted.
  4. Translation: The extracted plain text is then translated using a chosen MT backend. LaTeXMT is designed to be backend-agnostic, supporting local transformers models (like Opus-MT) and external APIs (like DeepL and OpenAI).
  5. Markup Reinsertion: This is a key innovation. The system uses word alignments (generated by the MT model or a separate aligner) to map the positions of the original markup in the source text to the translated text. It then reconstructs the translated text by reinserting the original markup at these identified positions.
  6. Composition: Finally, the newly translated and re-marked-up text items replace the original text in the node list, and the complete, translated LaTeX document is assembled.

Datasets

The paper evaluates LaTeXMT on:

  • Synthetic LaTeX Snippets: A custom benchmark created by the authors, consisting of short LaTeX snippets designed to test various aspects of translation, including mathematical expressions, custom commands and environments, special symbols, and sequences of macros. These snippets are provided in both German and Italian.
  • Full-Document Tests: The system was also tested on complete LaTeX documents using standard conference paper templates from ACL, Springer Nature, ACM, and IEEE.

The authors also mention using pre-trained MT models like Opus-MT (specifically opus-mt-de-en for German-English) and models from Hugging Face's transformers library.

Key Insights and Ideas

  • Structure-Preserving MT is Crucial: The core insight is that simply translating the text content of a LaTeX document is insufficient; preserving its structural integrity and markup is paramount for producing usable output.
  • Masking for Robustness: Replacing non-textual elements with placeholders before translation is a surprisingly effective way to prevent MT models from corrupting them, while still allowing the surrounding text context to be used for better translation.
  • Markup Reinsertion via Word Alignments: The novel approach of using word alignments to guide the reinsertion of markup is a clever way to handle formatting without requiring specialized, markup-aware MT models.
  • Backend Agnosticism: Designing LaTeXMT to work with various translation backends (local or API-based) makes it highly flexible and adaptable to different user needs and language pairs.

Key Results

  • Full-Document Success: LaTeXMT successfully produced valid, compilable output for most of the full-document tests (ACL, Springer Nature, ACM, IEEE templates), significantly outperforming some commercial tools and other dedicated systems in terms of compilability.
  • Synthetic Snippet Performance: On synthetic snippets, LaTeXMT showed strong performance, with many examples translating correctly and compiling successfully. It was particularly noted for its ability to convert code examples into valid LaTeX and reduce hallucinations compared to some other methods.
  • Comparison to Baselines: While DeepL and GPT-40 showed promise, they often struggled with LaTeX-specific syntax or produced incomplete translations. TransLATEX, another dedicated tool, faced challenges with its token-string hiding mechanism. LaTeXMT generally offered a more reliable and robust solution.

Limitations and Future Work

  • Partial Markup: The current alignment-based markup reinsertion method cannot handle markup that spans only part of an input word. This would require more granular, token-level alignments.
  • Unknown Macros/Environments: The system relies on a predefined list of known macros and environments. Unknown ones are left untouched, which might lead to incomplete translations. Static analysis to expand macros could be a future direction.
  • Whitespace Handling: The text extraction simplifies all whitespace to a single space, losing nuances like non-breaking spaces or manual line breaks. While these can be manually restored, it's a limitation.
  • Named Entities: Named entities are translated along with the text, which might not always be desired if they are meant to remain in their original language.
  • Alignment Failures: The system's success hinges on accurate word alignments. If alignments fail to find correspondences, markup reinsertion can fail. Refining the alignment mechanism is an area for future work.

Summary updated

BibTeX
@inproceedings{hoy2025latexmt,
  author = {Calvin Hoy and Samuel Frontull and Georg Moser},
  title = {{LaTeXMT: Machine Translation for LaTeX Documents}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
}
Figure