Convert ChatGPT Formula Format to Obsidian-Compatible Format

The formulas generated by ChatGPT use the following format:

1
2
3
\[
Formula Content
\]

However, Obsidian renders formulas using the following format:

1
2
3
$$
Formula Content
$$

When copying a formula from ChatGPT to Obsidian, this difference prevents proper rendering.

Solution

We can create a script for Obsidian to automatically replace the formula format when pasting.

1. Create the Script

We can solve this issue using a plugin in Obsidian.

In your vault, create a file named fixlatex.js under the template directory, and input the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module.exports = async (params) => {
const { quickAddApi } = params;

// Get clipboard content
const clipboardContent = await quickAddApi.utility.getClipboard();

// Check if content was successfully retrieved
if (!clipboardContent) {
new Notice("Clipboard is empty or inaccessible.");
return;
}

const modifiedContent = clipboardContent
.replace(/\\\[|\\\]/g, '$$$$') // Convert \[ \] to $$ $$
.replace(/\\\(\s*|\s*\\\)/g, '$$'); // Convert \( \) to $

// Write the modified content back to the clipboard
await navigator.clipboard.writeText(modifiedContent);

new Notice("Clipboard content has been processed and modified!");
};

2. Set Up the Script in QuickAdd

Install the QuickAdd plugin and create a Macro, configuring it as shown below, then save it. The first step in the Macro is to execute our user script fixlatex.js, the second step is to wait for 100 milliseconds, and the third step is to execute the paste action.

3. Set Sidebar Shortcut in Commander

Install the Commander plugin and set up the QuickAdd action we just created as a sidebar shortcut. You can also skip this step and directly use an Obsidian command to execute this action.

4. Verify the Effect

Now, on the ChatGPT webpage (currently there seems to be an issue when clicking the copy button in the app), click the copy button, then in Obsidian, click the sidebar shortcut or manually execute the QuickAdd command. This will copy the content from ChatGPT to Obsidian and automatically convert the LaTeX format.