A small, no-frills Markdown → LaTeX/PDF converter written in Python. One command takes a .md file and produces .tex and .pdf with sensible defaults.
- One-step pipeline:
.md → .tex → .pdf(runs a LaTeX engine for you) - Cross‑platform engine detection:
pdflatex,xelatex, orlualatex(Windows/Linux/macOS) - Engine‑flexible LaTeX preamble (via
iftex) so the same.texcompiles on Overleaf and locally with pdfLaTeX/LaTeX or XeLaTeX/LuaLaTeX - Unicode‑safe behavior:
- Preserves raw Unicode exactly inside fenced code blocks (```/~~~)
- Uses a Unicode‑capable engine automatically when needed
- Emoji/sticker removal by Unicode ranges (no per‑emoji lists)
- Inline formatting:
**bold**,[links](url), and`inline code` - Math: inline
$...$, display$$...$$, and bracketed display blocks using lines with[and] - Auto‑wraps common math used in text (e.g.,
\alpha,\int,\vec{x},x_1,x^2) into$...$ - Tables: GitHub‑style pipe tables scaled to page width
- Lists: ordered/unordered, nested by indentation (2 spaces per level)
- Headings:
#→\section,##→\subsection,###→\subsubsection,####→\paragraph- Leading numeric prefixes like
1. Titleare stripped from heading text - Subsubsections numbered as
1, 2, 3(no0.0.1)
- Leading numeric prefixes like
- Horizontal rules:
---,***, or___ - Newlines: a single newline in Markdown becomes a visible line break in LaTeX (
\newline) - Cleanup: removes LaTeX aux files after a successful build (keeps only
.md,.tex,.pdf,.py)
- Python 3.8+
- A LaTeX distribution:
- Windows: MiKTeX or TeX Live
- Linux/macOS: TeX Live
The script auto‑detects engines via PATH and known install locations and prefers xelatex/lualatex when it detects non‑ASCII inside fenced code blocks; otherwise it uses pdflatex.
The generated .tex includes an engine‑aware preamble: pdfTeX uses inputenc + T1 + lmodern, while Xe/LuaLaTeX use fontspec.
- Windows (PowerShell):
python md2tex.py your_file.md- Linux/macOS:
python3 md2tex.py your_file.mdOutputs:
your_file.tex— generated LaTeXyour_file.pdf— compiled PDF (if a LaTeX engine is installed)
Tip: If you run the script without a file, or pass "/" or ".", it defaults to README.md in the current directory. For example:
- Windows (PowerShell):
python md2tex.py
python md2tex.py /- Linux/macOS:
python3 md2tex.py
python3 md2tex.py /- Paragraphs/newlines
- Single newline →
\\newline(forced line break) - Blank line → paragraph break
- Single newline →
- Code
- Fenced code blocks (```/~~~) are emitted as
verbatimwith Unicode preserved - Inline code uses
\texttt{...}with safe escaping
- Fenced code blocks (```/~~~) are emitted as
- Math
- Inline:
$...$ - Display:
$$...$$or bracketed block between lines[and] - Literal
$$...$$text is preserved (escaped) in regular paragraphs - Auto‑math wrapping: if you accidentally use math commands in text (e.g.,
\alpha,\int,x_1,x^2,\vec{x}), they are wrapped into$...$automatically
- Inline:
- Tables
- Pipe tables with a header and a separator line are supported and auto‑scaled to
\textwidth
- Pipe tables with a header and a separator line are supported and auto‑scaled to
- Lists
-,*unordered;1.ordered- Nesting by 2‑space indentation per level
- Headings
#,##,###,####→ LaTeX sectioning commands- Leading numbering like
1. Titlein source is removed from the title text
- Emojis/stickers
- Removed globally by Unicode ranges (flags, pictographs, emoticons, dingbats, skin tones, VS‑16/ZWJ)
- “No LaTeX engine found”
- Install MiKTeX (Windows) or TeX Live (Linux/macOS) and ensure binaries are on PATH
- “PDF compilation failed”
- Check the generated
.texnext to your.md - Make sure packages like
amsmath,hyperref,adjustboxare available in your LaTeX install
- Check the generated
- “fontspec only works with Xe/LuaLaTeX”
- The output
.texavoids loadingfontspecon pdfLaTeX/LaTeX viaiftex. If you manually edit the preamble, keepfontspecunder the Xe/Lua branch only.
- The output
- “Unicode in code block breaks with pdflatex”
- The script prefers
xelatex/lualatexwhen it detects non‑ASCII in code fences; install one of them if missing
- The script prefers
- Overleaf notes
- You can compile the same
.texwith pdfLaTeX, XeLaTeX, or LuaLaTeX. If you hit Unicode issues, switch the Overleaf compiler to XeLaTeX or LuaLaTeX.
- You can compile the same
- Images, blockquotes, and task lists are not implemented (kept intentionally simple)
- This is not a full Markdown parser; it covers the most common patterns used in notes/technical docs
This README doubles as the end-to-end test document. You can run the converter directly on it to produce a PDF:
- Windows (PowerShell):
python md2tex.py README.md- Linux/macOS:
python3 md2tex.py README.mdBelow is the full “hard cases” sample previously in test.md.
This document tests all markdown features including special characters, equations, tables, and more.
The quadratic formula is
The area of a circle:
Partial Differential Equation (Heat Equation):
Navier-Stokes Equation:
Integral Example:
Double Integral:
Matrix Example:
Matrix Multiplication:
Square Root and Fractions:
Summation and Product:
Limit Example:
Taylor Series:
α (alpha), β (beta), γ (gamma), δ (delta), ε (epsilon), ζ (zeta), η (eta), θ (theta), λ (lambda), μ (mu), π (pi), σ (sigma), τ (tau), φ (phi), ω (omega)
Uppercase: Γ (Gamma), Δ (Delta), Θ (Theta), Λ (Lambda), Ξ (Xi), Π (Pi), Σ (Sigma), Φ (Phi), Ψ (Psi), Ω (Omega)
± ∓ × ÷ ∙ √ ∛ ∜ ∞ ∝ ≈ ≠ ≡ ≤ ≥ ⊂ ⊃ ⊆ ⊇ ∩ ∪ ∫ ∮ ∂ ∇ ∆ ∏ ∑
© ® ™ € £ ¥ ¢ § ¶ † ‡ • ‰ ′ ″ ‴ → ← ↑ ↓ ↔ ⇒ ⇐ ⇔
| Symbol | Name | LaTeX | Unicode |
|---|---|---|---|
| α | Alpha | \alpha |
U+03B1 |
| β | Beta | \beta |
U+03B2 |
| ∫ | Integral | \int |
U+222B |
| ∑ | Sum | \sum |
U+2211 |
| √ | Square Root | \sqrt{} |
U+221A |
| ∞ | Infinity | \infty |
U+221E |
| ≈ | Approximately | \approx |
U+2248 |
| ≠ | Not Equal | \neq |
U+2260 |
| Constant | Symbol | Approximate Value | Formula |
|---|---|---|---|
| Pi | π | 3.14159265359 | |
| Euler's Number | e | 2.71828182846 | |
| Golden Ratio | φ | 1.61803398875 | |
| Planck's Constant | h | 6.62607015 × 10⁻³⁴ J⋅s |
| Language | Addition | Multiplication | Division | Modulo | Power |
|---|---|---|---|---|---|
| Python | a + b |
a * b |
a / b |
a % b |
a ** b |
| C++ | a + b |
a * b |
a / b |
a % b |
pow(a, b) |
| JavaScript | a + b |
a * b |
a / b |
a % b |
a ** b |
| Java | a + b |
a * b |
a / b |
a % b |
Math.pow(a, b) |
import numpy as np
import matplotlib.pyplot as plt
# Calculate π using Monte Carlo method
def estimate_pi(n_samples=1000000):
"""Estimate π using random points in a square"""
x = np.random.uniform(-1, 1, n_samples)
y = np.random.uniform(-1, 1, n_samples)
inside_circle = (x**2 + y**2) <= 1
pi_estimate = 4 * np.sum(inside_circle) / n_samples
return pi_estimate
# Test with special operators: +, -, *, /, %, **, //, &, |, ^, ~, <<, >>
result = (2 ** 3) * (10 // 3) + (15 % 4) - (100 / 7)
print(f"Result: {result:.4f}")
# Unicode in strings
symbols = "α β γ δ ε ζ η θ λ μ π σ τ φ ω"
operators = "± × ÷ √ ∞ ≈ ≠ ≤ ≥ ∫ ∑"\begin{equation}
\nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}
\end{equation}
\begin{align}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\epsilon_0} \\
\nabla \cdot \mathbf{B} &= 0 \\
\nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} &= \mu_0 \mathbf{J} + \mu_0 \epsilon_0 \frac{\partial \mathbf{E}}{\partial t}
\end{align}- Item with α (alpha) and β (beta)
- Mathematical operators: ∫ ∑ ∏ √
- Comparison: ≈ ≠ ≤ ≥ ∞
- Arrows: → ← ↑ ↓ ↔
- Symbols: © ® ™ € £ ¥
- First: Calculate
$\int_0^1 x^2 dx = \frac{1}{3}$ - Second: Evaluate
$\sum_{i=1}^{10} i = 55$ - Third: Solve
$\frac{dy}{dx} = 2x$ to get$y = x^2 + C$ - Fourth: Matrix multiplication
$\mathbf{A} \times \mathbf{B}$ - Fifth: Compute
$\lim_{x \to 0} \frac{\sin x}{x} = 1$
- Top level with π ≈ 3.14159
- Nested with
$e^{i\pi} + 1 = 0$ - Another nested:
$\sqrt{-1} = i$
- Nested with
- Another top: ∞ (infinity)
- Sub-item:
$\lim_{n \to \infty}$
- Sub-item:
Bold text with italic text and inline code with special chars: α_β^γ
Regular text with bold, italic, and bold italic combined.
Text with special characters: @ # $ % ^ & * ( ) _ + = { } [ ] | \ : ; " ' < > , . ? /
Escaped characters test: _underscore_ *asterisk* #hash#
Visit Python Official for documentation.
Check out NumPy for numerical computing.
Mathematical reference: Wolfram MathWorld
This document contains:
- Multiple heading levels (# ## ### ####)
- Tables with special characters (α β γ π ∑ ∫)
- Mathematical equations (
$inline$ and$$block$$ ) - Code blocks with various languages
- Lists (ordered, unordered, nested)
- Special symbols (©®™€£¥)
- Links and references
- Text formatting (bold, italic,
code) - Greek letters (α β γ δ ε ζ η θ λ μ π σ τ φ ω Γ Δ Θ Λ Ξ Π Σ Φ Ψ Ω)
- Mathematical operators (± × ÷ √ ∞ ≈ ≠ ≤ ≥ ∫ ∑ ∏ ∂ ∇)
- Complex LaTeX equations with matrices, integrals, partial derivatives
- Author: SDNT8810