Prettier Plugin for LaTeX
raw JSON → 2.0.1 verified Sat Apr 25 auth: no javascript
Prettier plugin for formatting LaTeX source files. Version 2.0.1 supports Prettier v3 (ESM) and requires Node >= 18. Uses the @unified-latex/unified-latex-prettier library for parsing LaTeX into an AST, enabling smart formatting of environments, math mode, and macros. Notable caveats: Prettier v3 dropped automatic plugin resolution (must specify --plugin), and the plugin cannot parse arbitrary TeX — it assumes standard LaTeX. Online playground available.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/prettier-plugin-latex/dist/index.js from /path/to/project not supported. ↓
cause Using require() with an ESM-only package.
fix
Change to import or use dynamic import. Ensure your project is ESM (type: module) or use .mjs files.
error Cannot find module 'prettier-plugin-latex' ↓
cause Prettier v3 does not automatically resolve plugins; missing --plugin flag or plugins array in config.
fix
Add '--plugin=prettier-plugin-latex' or '--plugin=./node_modules/prettier-plugin-latex/dist/index.js' to the prettier CLI command.
error TypeError: prettier.format is not a function ↓
cause Using require() of default export from ESM module.
fix
Use dynamic import or switch to ESM: import prettier from 'prettier';
error SyntaxError: Invalid or unexpected token (prettier-plugin-latex fails to parse file) ↓
cause The plugin expects standard LaTeX; non-standard syntax may cause parse failure.
fix
Avoid using custom macros or non-standard LaTeX constructs. Simplify the document to match standard LaTeX.
Warnings
breaking Prettier v3 removed automatic plugin resolution. You must explicitly pass --plugin=... or include plugins: [...] in config. ↓
fix Add --plugin=node_modules/prettier-plugin-latex/dist/index.js when running prettier on the command line, or include 'prettier-plugin-latex' in the plugins array of your config.
breaking Prettier v3 dropped CommonJS support (CJS). This plugin is now ESM-only. ↓
fix Switch to ESM imports or use dynamic import(). For scripts, change 'type': 'module' in package.json or use .mjs extension.
gotcha The plugin cannot parse arbitrary TeX; it assumes standard LaTeX. Non-standard macros or unexpanded conditionals may cause formatting errors or no output. ↓
fix Ensure your document follows typical LaTeX syntax. For complex macros, consider preprocessing or using a different formatter.
deprecated printPrettier from standalone may become unstable; prefer programmatic usage via prettier.format(). ↓
fix Use prettier.format() with the plugin loaded as shown in the quickstart.
Install
npm install prettier-plugin-latex yarn add prettier-plugin-latex pnpm add prettier-plugin-latex Imports
- default wrong
const prettier = require('prettier')correctimport prettier from 'prettier' - printPrettier wrong
import { printPrettier } from 'prettier-plugin-latex'correctimport { printPrettier } from 'prettier-plugin-latex/standalone' - plugin wrong
const plugin = require('prettier-plugin-latex')correctimport * as plugin from 'prettier-plugin-latex'
Quickstart
import prettier from 'prettier';
import * as plugin from 'prettier-plugin-latex';
const code = `\begin{enumerate}
\item[55,4] Hi there
\end{enumerate}`;
prettier.format(code, {
parser: 'latex',
plugins: [plugin],
tabWidth: 4,
printWidth: 80
}).then(result => console.log(result));