escape-latex is a JavaScript utility for sanitizing strings by escaping LaTeX special characters, making them safe for inclusion in LaTeX documents. The current stable version is 1.2.0. As a focused utility, it typically has an infrequent release cadence, with updates primarily for bug fixes or minor enhancements. Its key differentiators include the ability to selectively escape characters that would simply malform LaTeX (`# $ % & \ ^ _ { }`) versus those that affect formatting (spaces, en-dashes, em-dashes) via the `preserveFormatting` option. Furthermore, it provides an `escapeMapFn` callback, allowing developers to fully customize or extend the character-to-escape mapping based on specific LaTeX requirements, offering flexibility beyond standard escaping mechanisms. It is designed for use in Node.js environments.
npm install escape-latexVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic, formatting-preserving, and custom character escaping using `escape-latex` in an ESM context.
To re-enable en-dash and em-dash escaping, set the `preserveFormatting` option to `true` when calling `escape-latex`.
If the final LaTeX output needs to closely resemble the input string's formatting, including multiple spaces, tabs, and specific dash representations, set the `preserveFormatting` option to `true`.
When using `escapeMapFn`, always ensure that the custom mappings produce valid LaTeX syntax. It's recommended to combine your custom escapes with `defaultEscapes` and `formattingEscapes` using `Object.assign({}, defaultEscapes, formattingEscapes)` to avoid losing default escaping behavior.The default `escape-latex` function will escape these. Ensure you are passing the string through `lescape()` before including it in your LaTeX document.
To preserve multiple spaces, tabs, and other formatting characters, call `lescape("Hello World", { preserveFormatting: true });`. This will convert spaces into non-breaking space commands (`~`).Use the `escapeMapFn` option to extend or modify the character escape mapping. For example, `escapeMapFn: (d, f) => { d['€'] = '\euro{}'; return Object.assign({}, d, f); }`.No dependency data recorded yet.