A small tool for mathematical writing
Lean 4 proofs in LaTeX.
Write Lean alongside your mathematics, and include its diagnostics and proof states in the PDF. LeanTeX connects your LaTeX document to Lean’s language server, with minted for source highlighting.
Use your existing Lean project, or check snippets remotely through Lean Web. Unchanged Lean code reuses cached results while you work on the prose.
Get startedExample PDF (earlier v2.5)Ryland Gross
Install
You need Python 3.9+ and a TeX distribution with latexmk, minted, and XeLaTeX or LuaLaTeX. The remote option needs no local Lean or Mathlib installation.
git clone https://github.com/rgrossharv/LeanTeX.git
cd LeanTeX
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[remote]"
Download the small example document, then build it:
leantex build examples/quickstart.tex --backend remote
The command makes the bundled TeX styles available for the build. Remote mode sends the extracted Lean code to the selected server; the PDF is compiled on your machine. PDF compilation enables shell escape for minted, so build TeX documents you trust.
Use
Give a Lean minted block a label, then place its output wherever it belongs. The onefile option shares declarations between snippets.
\documentclass{article}
\usepackage[onefile]{leantexv2}
\begin{document}
\begin{minted}[label=successor]{lean}
#check Nat.succ
example : 1 + 1 = 2 := by decide
\end{minted}
\leantexoutput{successor}
\end{document}
For output without a source listing:
\leantex{
#check List.map
}
For a block that LeanTeX both highlights and checks, use \begin{lean} … \end{lean}. Ordinary \input and \include files are read in document order. A lean environment must put its closing command on its own line.
Choose how much proof state to show
Configure a labeled block with \leantexconfig{successor}{infoview=lines:4}.
auto- A compact state from the end of the snippet.
lines:N- The final state, limited to N display lines.
full- States at each nonblank code line; more requests and more output.
goals- The final tactic goal, with diagnostics.
off- Diagnostics only; no proof-state requests.
Use \totalinfoview for the combined diagnostics and collected states. It reuses the snippet results. Set outsize=footnotesize or codesize=small in a block’s configuration to adjust text size.
Local or remote
Remote. Lean Web already has Lean and Mathlib available. Choose a server project with --remote-project; list the current choices with leantex projects.
leantex build paper.tex --backend remote \
--remote-project MathlibDemo
The remote adapter uses Lean Web’s WebSocket editor protocol. It retrieves text diagnostics and plain proof states; interactive widgets are not embedded in the PDF. Availability and Lean versions depend on the server. Use --refresh after a server update, and keep larger projects local. A self-hosted Lean Web server can be selected with --remote-url.
Local. The default backend uses an already installed Lean toolchain and prebuilt imports in your project. LeanTeX does not download toolchains, fetch Mathlib, or build your dependencies.
leantex build paper.tex --project-root /path/to/project
Set up and build that project with your usual Lean tools first. Select an installed binary explicitly with LEANTEX_LEAN if needed. LeanTeX reads the existing Lake manifest and compiled libraries.
While writing
# Refresh output without running LaTeX
leantex build paper.tex --backend remote --no-latexmk
# Rebuild as the document changes
leantex watch paper.tex --backend remote
# Check installed tools without starting Lean
leantex doctor
Each document has its own small .leantex/<document>/ output directory. Prose-only changes reuse Lean results, and unchanged generated files keep their timestamps so latexmk can work incrementally. Use --strict in CI to fail on Lean errors, or --timeout 180 to allow a longer check.
Using an editor’s LaTeX build command
The CLI build command works directly after installation. To use a separate TeX editor, install the styles with leantex install-tex. To also enable automatic checking through latexmk, run leantex install-tex --latexmk-hook; this adds a managed block to ~/.latexmkrc. Set LEANTEX_BACKEND=remote in the editor’s environment to select remote checking.
Existing documents and older versions
The leantexv2 API, named minted blocks, hidden snippets, and onefile option remain supported. Regenerate output once after updating. The earlier implementations are preserved on the version1 and version2 branches.