KaTeX to MathJax

by Darko Pejakovic
5
4
3
2
1
Score: 56/100

Description

Category: Coding & Technical Tools

The KaTeX to MathJax plugin streamlines the integration of mathematical expressions into Obsidian by converting KaTeX notation to MathJax format. This is particularly useful for users who copy mathematical content from platforms like ChatGPT, which often use KaTeX. The plugin features automatic conversion upon pasting, ensuring that your notes maintain proper formatting without extra effort. Additionally, users can manually convert text within individual notes or across the entire vault using the command palette. This functionality enhances the usability of mathematical expressions in your notes, making it easier to work with complex equations and maintain a polished appearance in your documents.

Reviews

No reviews yet.

Stats

29
stars
6,426
downloads
3
forks
530
days
232
days
232
days
2
total PRs
0
open PRs
1
closed PRs
1
merged PRs
8
total issues
0
open issues
8
closed issues
12
commits

Latest Version

8 months ago

Changelog

Changelog – KaTeX → MathJax Converter Plugin

Fixes

  • Prevent inline $…$ wrapping inside display $$…$$ blocks

    • Re-ordered processing in processMath:

      1. Wrap math-dominant lines (wrapBareMathLinesOutside) first.
      2. Only then apply inline conversions (convertInlineDelims, wrapInlineBareLatex) outside display spans.
    • Added guard in isMathLine to skip any line that already contains a $.

Features

  • New settings toggles in the settings tab:

    • Plain parentheses ( … ) as delimiters → converts (x^2+1)$x^2+1$ when LaTeX-like.

    • Plain brackets [ … ] as delimiters → same logic for [a,b].

    • Convert bare inline LaTeX → detects and wraps tokens like:

      • 90^\circ$90^{\circ}$
      • \sqrt{…}$\sqrt{…}$
      • \frac{…}{…}$\frac{…}{…}$
      • simple subscripts/superscripts like x_i, y^2.
    • Wrap single math lines → isolated equations like a^2 + b^2 = c^2 become:

      $$
      a^2 + b^2 = c^2
      $$
      
    • Wrap matrix/align environments → wraps bare \begin{bmatrix} … \end{bmatrix} into display math.

Enhancements

  • Comprehensive LaTeX-like detector (isLatexLike) Expanded to cover:

    • Greek letters
    • Calculus operators (\partial, \nabla, \sum, \int)
    • Vector/matrix environments
    • Common functions (sin, cos, log, exp, etc.)
    • Unicode math symbols (≤, ≥, ∞, ∇, etc.)
  • Better guards for parentheses/brackets

    • Avoids converting (a) list markers or [link](url) syntax.
    • Uses local context (contextIsMathy) to only treat (n) etc. as math in math-heavy prose.

README file from

Github

Obsidian Plugin: Convert KaTeX to MathJax

A simple plugin for Obsidian that converts KaTeX notation to MathJax, ensuring seamless use of mathematical expressions copied from sources like OpenAI's ChatGPT.


Why This Plugin?

ChatGPT and other platforms often render mathematical expressions using KaTeX, while Obsidian uses MathJax for mathematical notation. This discrepancy can lead to formatting issues when copying and pasting content.

This plugin eliminates the hassle by automatically converting KaTeX expressions to MathJax, making it easier to integrate ChatGPT-generated content or other KaTeX-based math into your Obsidian vault.


Features

  • Default paste conversion: Automatically converts KaTeX expressions on paste (toggleable via settings).

  • Command Palette Actions:

    • Paste with conversion: Manually paste KaTeX content with MathJax conversion applied.
    • Convert current text file: Convert all KaTeX expressions in the current note to MathJax.
    • Convert all files: Batch-convert KaTeX expressions in every markdown file across your vault.
  • Works seamlessly with clipboard operations.

  • Fine-grained settings to control conversion heuristics (see below).


Settings

Open Settings → Community Plugins → Convert KaTeX to MathJax to adjust:

✅ Enable default paste conversion

Automatically convert KaTeX to MathJax when pasting.

Example Paste:

\(a^2 + b^2 = c^2\)

Result:

$a^2 + b^2 = c^2$

✅ Wrap matrix/align environments in $$ … $$

Ensures multi-line math environments are recognized as display math.

Example Paste:

\begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix}

Result:

$$
\begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix}
$$

✅ Treat plain parentheses ( … ) as math

Converts outermost parentheses to inline math if contents look LaTeX-like.

Example Paste:

(x^2 + 1)

Result:

$x^2 + 1$

✅ Treat plain brackets [ … ] as math

Same as parentheses, but for square brackets.

Example Paste:

[a,b]

Result:

$[a,b]$

✅ Convert bare inline LaTeX

Wraps common inline LaTeX tokens into $…$.

Examples

  • 90^\circ$90^{\circ}$
  • \sqrt{4}$\sqrt{4}$
  • \frac{1}{2}$\frac{1}{2}$
  • x_i, y^2$x_i$, $y^2$

✅ Wrap single math lines into $$ … $$

Automatically converts isolated math-heavy lines into display math.

Example Paste:

a^2 + b^2 = c^2

Result:

$$
a^2 + b^2 = c^2
$$

Installation

Manual Installation

  1. Download or build the plugin files (main.js and manifest.json).

  2. Copy them into your Obsidian vault directory:

    VaultFolder/.obsidian/plugins/obsidian-convert-katex-to-mathjax/
    
  3. Restart Obsidian.

  4. Enable the plugin in Settings > Community Plugins.


How to Use

Default Paste Behavior

  1. Toggle "Enable default paste conversion" in the plugin settings.
  2. Simply paste copied KaTeX content into your Obsidian editor—it's automatically converted to MathJax.

Command Palette Actions

Open the Command Palette (Ctrl + P / Cmd + P) and search for the following commands:

  • Paste with conversion: Pastes clipboard content with conversion applied.
  • Convert current text file: Converts all KaTeX expressions in the current note to MathJax.
  • Convert all files: Scans and converts KaTeX expressions in all markdown files across your vault.

Development

If you'd like to make changes or contribute:

git clone https://github.com/your-repo/convert-katex-to-mathjax.git
cd convert-katex-to-mathjax
npm install
npm run dev

Contributions

Contributions, suggestions, and bug reports are welcome! Feel free to submit a pull request (PR) or open an issue in the repository.


Support

If you find this plugin helpful and would like to support its development, consider buying me a coffee:

Buy Me A Coffee


License

This project is licensed under the MIT License.