A project to convert LaTeX to DOCX
Project description
LaTeX to Word Conversion Tool
In daily work, colleagues or supervisors unfamiliar with LaTeX may request Word documents for review and collaboration. This project provides a Python script that uses Pandoc and Pandoc-Crossref to automatically convert LaTeX files into Word documents following a specified format. Although there's no perfect method for converting LaTeX to Word, the output generated by this project meets informal review needs. However, around 5% of the content (such as author information) may require manual corrections post-conversion.
Features
- Supports formula conversion
- Automatically numbers and cross-references images, tables, formulas, and citations
- Converts multi-figure LaTeX files
- Outputs Word files in a specified format
- Supports Chinese
Examples are shown below; more results are in tests
:
Quick Start
Ensure Pandoc and Pandoc-Crossref are correctly installed (see Install Dependencies). Execute the following command in your terminal:
python ./tex2docx/tex2docx.py --input_texfile <your_texfile> --multifig_dir <dir_saving_temporary_figs> --output_docxfile <your_docxfile> --reference_docfile <your_reference_docfile> --bibfile <your_bibfile> --cslfile <your_cslfile>
Replace <...>
with appropriate file paths or folder names.
Install Dependencies
Ensure you have installed Pandoc, Pandoc-Crossref, and related Python libraries.
Pandoc
Install Pandoc as described in the official documentation. It is recommended to download the latest release from Pandoc Releases.
Pandoc-Crossref
Install Pandoc-Crossref by following the official documentation. Ensure compatibility between Pandoc and Pandoc-Crossref and configure the path correctly.
Python Libraries
Install locally:
pip install -e .
Or install from PyPI:
pip install tex2docx
Usage and Examples
This tool supports both command-line and script-based usage. Ensure the required dependencies are installed.
Command-Line Usage
Run the following command in your terminal:
convert --input-texfile <your_texfile> --multifig-dir <dir_saving_temporary_figs> --output-docxfile <your_docxfile> --reference-docfile <your_reference_docfile> --bibfile <your_bibfile> --cslfile <your_cslfile>
Use convert --help
to see details on these parameters.
For example, using tests/en
:
convert --input-texfile ./tests/en/main.tex --multifig-dir ./tests/en/multifigs --output-docxfile ./tests/en/main_cli.docx --reference-docfile ./my_temp.docx --bibfile ./tests/ref.bib --cslfile ./ieee.csl
This will generate the Word file main_cli.docx
in the tests/en
directory.
Script Usage
from tex2docx import LatexToWordConverter
config = {
'input_texfile': '<your_texfile>',
'output_docxfile': '<your_docxfile>',
'multifig_dir': '<dir_saving_temporary_figs>',
'reference_docfile': '<your_reference_docfile>',
'cslfile': '<your_cslfile>',
'bibfile': '<your_bibfile>',
'debug': False
}
converter = LatexToWordConverter(**config)
converter.convert()
For more examples, refer to tests/test_tex2docx.py
.
FAQ
-
Inconsistent Multi-Figure Layout
The relative positions of sub-figures may differ between LaTeX compilation and Word conversion, as shown below:This may result from redefined page size or parameters in the LaTeX file. To address this, adjust the
MULTIFIG_TEXFILE_TEMPLATE
variable. Below is an example for reference:import tex2docx my_multifig_texfile_template = r""" \documentclass[preview,convert,convert={outext=.png,command=\unexpanded{pdftocairo -r 600 -png \infile}}]{standalone} \usepackage{graphicx} \usepackage{subfig} \usepackage{xeCJK} \usepackage{geometry} \newgeometry{ top=25.4mm, bottom=33.3mm, left=20mm, right=20mm, headsep=10.4mm, headheight=5mm, footskip=7.9mm, } \graphicspath{{%s}} \begin{document} \thispagestyle{empty} %s \end{document} """ config = { 'input_texfile': 'tests/en/main.tex', 'output_docxfile': 'tests/en/main.docx', 'multifig_dir': 'tests/en/multifigs', 'reference_docfile': 'my_temp.docx', 'cslfile': 'ieee.csl', 'bibfile': 'tests/ref.bib', 'multifig_texfile_template': my_multifig_texfile_template, } converter = tex2docx.LatexToWordConverter(**config) converter.convert()
-
The Word Output Doesn't Meet Formatting Requirements
Use Word's style management tools to adjust the styles inmy_temp.docx
.
Implementation Details
This project relies on Pandoc and Pandoc-Crossref to convert LaTeX files to Word documents. The core command used is:
pandoc texfile -o docxfile \
--lua-filter resolve_equation_labels.lua \
--filter pandoc-crossref \
--reference-doc=temp.docx \
--number-sections \
-M autoEqnLabels \
-M tableEqns \
-M reference-section-title=Reference \
--bibliography=ref.bib \
--citeproc --csl ieee.csl \
-t docx+native_numbering
--lua-filter resolve_equation_labels.lua
handles equation numbering and cross-references, inspired by Constantin Ahlmann-Eltze's script.--filter pandoc-crossref
handles cross-references for other elements.--reference-doc=my_temp.docx
applies the styles frommy_temp.docx
to the generated Word file. Two template files are included:TIE-temp.docx
(for TIE journal submission, double-column format) andmy_temp.docx
(single-column, designed for easier annotation).--number-sections
adds numbering to section headings.-M autoEqnLabels
,-M tableEqns
enable automatic numbering of equations and tables.-M reference-section-title=Reference
adds a section title for references.--bibliography=my_ref.bib
generates the bibliography fromref.bib
.--citeproc --csl ieee.csl
formats citations and the bibliography using the IEEE citation style.-t docx+native_numbering
improves captions for images and tables.
The conversion for multi-figure LaTeX content may not be perfect. This project extracts multi-figure code from the LaTeX file and uses the convert
and pdftocairo
tools to compile the figures into a single large PNG file, replacing the original LaTeX image code and updating references to ensure smooth import into Word.
Known Issues
- Captions for figures and tables in Chinese still start with "Figure" and "Table".
- Author information is not fully converted.
Changelog
v1.2.0
- Fixed module import issues, improving stability.
- Enhanced the command-line tool for a more intuitive and efficient user experience.
- Switched to
pyproject.toml
for dependency management, replacingsetup.py
. - Released on PyPI; users can install via
pip install tex2docx
.
Miscellaneous
There are two kinds of people: those who use LaTeX and those who don't. The latter often ask the former for a Word version. Hence, the following command:
pandoc input.tex -o output.docx\
--filter pandoc-crossref \
--reference-doc=my_temp.docx \
--number-sections \
-M autoEqnLabels -M tableEqns \
-M reference-section-title=Reference \
--bibliography=my_ref.bib \
--citeproc --csl ieee.csl \
-t docx+native_numbering
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tex2docx-1.2.0.tar.gz
.
File metadata
- Download URL: tex2docx-1.2.0.tar.gz
- Upload date:
- Size: 9.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 214c57ed0d5e04c9d8efd2fcf8282333f98502240fe8cdaa5930d4e49f0e561d |
|
MD5 | 52b56133c0c31eb86962aa7caa2ed7a0 |
|
BLAKE2b-256 | bc9fad7f4787cc5e9f38f63efaa2504ad7d18a71ba210768d0438c0a1f4a45de |
File details
Details for the file tex2docx-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: tex2docx-1.2.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab2190d301bd0eb8e134506e5be96d756aabb3c2d32b71021a3fa7ec65c0fb44 |
|
MD5 | f0875d18c00da25537bc17d55009ed2b |
|
BLAKE2b-256 | 7a480cab35f92c829b12c4bec508a89336ce7a6e9706034c8353fb559ccee424 |