Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KaTeX Macros With Global Scope #98

Closed
4 tasks done
fweth opened this issue Jul 5, 2024 · 3 comments
Closed
4 tasks done

KaTeX Macros With Global Scope #98

fweth opened this issue Jul 5, 2024 · 3 comments
Labels
🤞 phase/open Post is being triaged manually

Comments

@fweth
Copy link

fweth commented Jul 5, 2024

Initial checklist

Problem

I like to use KaTex macros, especially where I put all my custom commands in a single math block at the top of the file. This is supported by many KaTeX based editors, including VSCode with it's built in preview. However, I couldn't get this to work with remark:

import rehypeDocument from 'rehype-document';
import rehypeKatex from 'rehype-katex';
import rehypeParse from 'rehype-parse';
import rehypeStringify from 'rehype-stringify';
import { unified } from 'unified';

const htmlString = `
<p class="language-math">\\def\\foo{abc}</p>
<p class="language-math">\\abc</p>
`;

const html = await unified()
  .use(rehypeParse, { fragment: true })
  .use(rehypeDocument, {
    css: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css',
  })
  .use(rehypeKatex)
  .use(rehypeStringify)
  .process(htmlString);

document.body.innerHTML += html;

Here the scope of the definition is its own math block, so it can't be used outside of the block. Would be awesome if there is a way to get this working!

Solution

Abbreviations should be stored in a global context, not scoped to individual math blocks.

Alternatives

I can't think of an alternative solution.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Jul 5, 2024
@ChristianMurphy
Copy link
Member

Welcome @fweth! 👋
have you tried the macros option?

await t.test('should support `macros`', async function () {
const macros = {'\\RR': '\\mathbb{R}'}
assert.deepEqual(
String(
await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeKatex, {macros})
.use(rehypeStringify)
.process('<span class="math-inline">\\RR</span>')
),
String(
await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeStringify)
.process(katex.renderToString('\\RR', {macros}))
)
)
})

Abbreviations should be stored in a global context, not scoped to individual math blocks.

That feels unexpected.
From the readme:

to be able to embed rich diagrams of math to scientific documentation

Embedded documents, are just that, independent documents inside of a host document.

It sounds like you want interleaving of different languages, perhaps something like https://mdxjs.com/ or https://jupyter.org/

@fweth
Copy link
Author

fweth commented Jul 6, 2024

Thanks, this sounds great!

Still, KaTeX/MathJax editors like VSCode (with preview), iA Writer, HackMD, etc. support global math contexts. KaTeX docs also say

Macros defined by \gdef, \xdef, \global\def, \global\edef, \global\let, and \global\futurelet will persist between math expressions. (Exception: macro persistence may be disabled. There are legitimate security reasons for that.)

@fweth fweth closed this as completed Jul 6, 2024
Copy link

github-actions bot commented Jul 6, 2024

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤞 phase/open Post is being triaged manually
Development

No branches or pull requests

2 participants