Registry / serialization / mhchemparser

mhchemparser

JSON →
library4.2.1jsnpmunverified

mhchemParser is a JavaScript/TypeScript library designed to convert chemical equations and physical units written in the mhchem syntax into standard LaTeX syntax. This conversion is crucial for rendering chemical notation accurately in environments that support LaTeX, such as MathJax and KaTeX. The current stable version is 4.2.1. It serves as a foundational parser, allowing developers to process mhchem input directly before feeding it into a LaTeX rendering engine. The library is actively maintained and differentiates itself by providing a robust, standalone parsing capability specifically for mhchem syntax, rather than being an integrated component of a larger rendering system. Its primary utility is for downstream software integration, enabling flexible handling of chemical typesetting.

npm install mhchemparser
INSTALL
IMPORT
SIG · MHCHEMPARSER
M
mhchemparser
serializationjavascriptv4.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mhchemParser
import { mhchemParser } from 'mhchemparser';
import mhchemParser from 'mhchemparser'; // Or import { toTex } from 'mhchemparser';
The library exports a class named `mhchemParser` with a static method `toTex` for parsing. It is not a default export, nor is `toTex` a direct named export.
mhchemParser.toTex
import { mhchemParser } from 'mhchemparser'; mhchemParser.toTex("CO2", "ce");
import { toTex } from 'mhchemparser'; toTex("CO2", "ce");
`toTex` is a static method of the `mhchemParser` class and must be called via the class name.
mhchemParser (CommonJS)
const { mhchemParser } = require('mhchemparser');
const mhchemParser = require('mhchemparser'); const toTex = require('mhchemparser').toTex;
For CommonJS environments, the `mhchemParser` class should be destructured from the require call.

Demonstrates how to import the mhchemParser class and use its static 'toTex' method for converting mhchem syntax in chemical equations, physical units, and embedded TeX strings into LaTeX.

import { mhchemParser } from 'mhchemparser'; // Parse a chemical equation const equationInput = "CO2 + C -> 2 CO"; const equationOutput = mhchemParser.toTex(equationInput, "ce"); console.log(`Input: \\ce{${equationInput}}\nOutput: ${equationOutput}`); // Expected output: Input: \ce{CO2 + C -> 2 CO} // Output: {\mathrm{CO}{\vphantom{A}}_{\smash[t]{2}} {}+{} \mathrm{C} {}\mathrel{\longrightarrow}{} 2\,\mathrm{CO}} // Parse a physical unit const unitInput = "123 kJ*mol-1"; const unitOutput = mhchemParser.toTex(unitInput, "pu"); console.log(`Input: \\pu{${unitInput}}\nOutput: ${unitOutput}`); // Expected output: Input: \pu{123 kJ*mol-1} // Output: 123\,\mathrm{kJ}{\cdot}\mathrm{mol}^{-1} // Parse a TeX string containing mhchem commands const texInput = "m_{\\ce{H2O}}"; const texOutput = mhchemParser.toTex(texInput, "tex"); console.log(`Input: ${texInput}\nOutput: ${texOutput}`); // Expected output: Input: m_{\ce{H2O}} // Output: m_{\mathrm{H}_{2}\mathrm{O}}}
Debug
Known issues
gotchaProviding malformed or syntactically incorrect mhchem input may lead to unexpected or incomplete LaTeX output, or internal errors depending on the severity of the input issue. While the parser aims for robustness, it's not a full semantic validator.
fix
Always consult the mhchem manual (mhchem.github.io/MathJax-mhchem/) for correct syntax. Validate user input before passing it to `mhchemParser.toTex` to ensure it conforms to expected mhchem patterns.
affects: >=1.0.0
gotchaThe `mhchemParser` class's `toTex` method expects a specific `type` argument ('ce', 'pu', or 'tex'). Providing an incorrect or unrecognized type will result in incorrect parsing behavior or errors.
fix
Ensure the second argument to `mhchemParser.toTex` is one of the supported strings: 'ce' for chemical equations, 'pu' for physical units, or 'tex' for general TeX strings containing mhchem commands.
affects: >=1.0.0
breakingWhile no explicit major breaking changes were detailed in the provided README excerpt, developers should always review the official changelog or GitHub releases when updating between major versions to identify potential API changes or behavior shifts.
fix
Before upgrading to a new major version, check the project's GitHub releases page and the `MathJax-mhchem` manual for any migration guides or breaking changes that might affect your integration.
affects: >=4.0.0
Errors
Common errors & fixes
TypeError: mhchemParser.toTex is not a function
Incorrect import of the `mhchemParser` class, leading to `mhchemParser` being `undefined` or not the expected class object.
fix
Ensure you are using `import { mhchemParser } from 'mhchemparser';` (ESM) or `const { mhchemParser } = require('mhchemparser');` (CommonJS). `toTex` is a static method of the exported class.
ReferenceError: mhchemParser is not defined
The `mhchemParser` class was not imported or destructured correctly, or the import statement was entirely missed.
fix
Add the correct import statement at the top of your file: `import { mhchemParser } from 'mhchemparser';` (ESM) or `const { mhchemParser } = require('mhchemparser');` (CommonJS).
MathJax or KaTeX renders raw `\ce{...}` or `\pu{...}` commands
The mhchem parser was not invoked, or its output was not correctly passed to the LaTeX rendering engine. The `\ce` and `\pu` commands are specific to mhchem syntax, not raw LaTeX.
fix
Ensure that you call `mhchemParser.toTex(yourInput, 'ce')` or `mhchemParser.toTex(yourInput, 'pu')` on your input string *before* passing it to MathJax or KaTeX for rendering. The output of `toTex` is standard LaTeX.
Upgrade
Version history
4.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Resources
mhchemparser — npm install mhchemparser · libregistry