Skip to content

ni/md2latex

Repository files navigation

md2latex

A command-line tool to convert Markdown files to LaTeX.

Project Structure

md2latex/
├── src/md2latex/
│   ├── __init__.py          # Package exports
│   ├── cli.py               # Command-line interface
│   └── converter.py         # Markdown to LaTeX conversion logic
├── tests/
│   ├── conftest.py          # Shared test fixtures
│   ├── unit/                # Unit tests
│   ├── integration/         # Integration tests
│   └── system/              # End-to-end CLI tests
├── pyproject.toml           # Package configuration
├── README.md
├── CONTRIBUTING.md
└── LICENSE

Installation

# Install in development mode
pip install -e .

# Or install directly
pip install .

# Install with development dependencies (for testing)
pip install -e ".[dev]"

Usage

Basic Usage

# Convert a Markdown file and print to stdout
md2latex input.md

# Convert and save to a file
md2latex input.md -o output.tex

# Read from stdin
cat input.md | md2latex -

# Pipe to a file
md2latex input.md > output.tex

Options

  • -o, --output FILE - Write output to a file instead of stdout
  • --no-preamble - Omit the LaTeX document preamble (useful for including in larger documents)
  • -v, --version - Show version information
  • -h, --help - Show help message

Examples

Generate a complete LaTeX document:

md2latex document.md -o document.tex

Generate LaTeX content without preamble (for inclusion in another document):

md2latex chapter.md --no-preamble -o chapter.tex

Supported Markdown Features

  • Headings (h1-h6) → \section, \subsection, etc.
  • Bold (**text**) → \textbf{text}
  • Italic (*text*) → \textit{text}
  • Inline code (`code`) → \texttt{code}
  • Code blocks (with language support) → lstlisting environment
  • Links ([text](url)) → \href{url}{text}
  • Images (![alt](src)) → \includegraphics in figure environment
  • Ordered listsenumerate environment
  • Unordered listsitemize environment
  • Block quotesquote environment
  • Horizontal rules\hrulefill

LaTeX Packages

The converter automatically includes only the packages needed for your content:

  • hyperref - for links
  • graphicx - for images
  • listings - for code blocks
  • ulem - for strikethrough text

Python API

You can also use md2latex as a Python library:

from md2latex import convert_markdown_to_latex, MarkdownToLatexConverter

# Simple conversion
latex = convert_markdown_to_latex("# Hello World\n\nThis is **bold**.")

# Without preamble
latex = convert_markdown_to_latex("# Hello", include_preamble=False)

# Using the converter class
converter = MarkdownToLatexConverter(include_preamble=True)
latex = converter.convert(markdown_text)
converter.convert_file("input.md", "output.tex")

Testing

The project includes a comprehensive test suite with unit, integration, and system tests.

Running Tests

# Run all tests
pytest tests -v

# Run specific test categories
pytest tests/unit -v          # Unit tests only
pytest tests/integration -v   # Integration tests only
pytest tests/system -v        # System/CLI tests only

# Run with coverage (requires pytest-cov)
pytest tests --cov=md2latex --cov-report=term-missing

License

MIT License

About

A command line tool that converts Markdown files to LaTeX files

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors