Skip to content

add katex support #1385

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

Merged
merged 5 commits into from
Aug 22, 2025
Merged

add katex support #1385

merged 5 commits into from
Aug 22, 2025

Conversation

20jasper
Copy link
Contributor

@20jasper 20jasper commented Aug 22, 2025

We need latex for my math stuffs. Thank

Katex will transform latex into mathml so it looks pretty and works nicely with screen reader. Yipee

One qualm I have is the current CSS import I added is on every single blog post (adds around 27KB to each page). Is there a way to include this only when it's needed? Is there a better way to hook into SCSS for this? I feel dirty adding that import

The way I conditionally import in my blog is just setting a "math" boolean in the frontmatter of md files

Latex formatting for exponents and other math symbols

Copy link

vercel bot commented Aug 22, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
playfulprogramming Ready Ready Preview Comment Aug 22, 2025 1:20pm

@@ -37,6 +37,8 @@ const { postHtml, post, authors, collection, collectionPosts, license } =

const postHeadings = postHtml.headingsWithIds;
const GHLink = `https://github.com/${siteMetadata.repoPath}/tree/main/content/${post.path}/index.md`;

import "katex/dist/katex.min.css";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best way to include CSS?

@fennifith
Copy link
Member

Is there any detectable class/property in the rehype output after the math plugins? You could add an extra step using unist-util-visit to check if the math styles were used, set a flag on the post metadata if so, and then conditionally import the styles based on that.

https://github.com/playfulprogramming/playfulprogramming/blob/main/src/utils/markdown/types.ts#L13 - This object should be passed through all the unified steps in the vfile arg, and should also get passed to blog-post.astro, so that's a good place to put it.

@20jasper
Copy link
Contributor Author

Is there any detectable class/property in the rehype output after the math plugins? You could add an extra step using unist-util-visit to check if the math styles were used, set a flag on the post metadata if so, and then conditionally import the styles based on that.

https://github.com/playfulprogramming/playfulprogramming/blob/main/src/utils/markdown/types.ts#L13 - This object should be passed through all the unified steps in the vfile arg, and should also get passed to blog-post.astro, so that's a good place to put it.

Indeed, here's some example output. It has katex and katex-mathml classes

<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mo></mo><mo stretchy="false">(</mo><msup><mn>2</mn><mn>53</mn></msup><mo></mo><mn>1</mn><mo stretchy="false">)</mo><mo separator="true">,</mo><msup><mn>2</mn><mn>53</mn></msup><mo></mo><mn>1</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[-(2^{53} - 1), 2^{53} - 1]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0641em;vertical-align:-0.25em"></span><span class="mopen">[</span><span class="mord"></span><span class="mopen">(</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em"><span style="top:-3.063em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">53</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222em"></span></span><span class="base"><span class="strut" style="height:1.0641em;vertical-align:-0.25em"></span><span class="mord">1</span><span class="mclose">)</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em"><span style="top:-3.063em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">53</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222em"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em"></span><span class="mord">1</span><span class="mclose">]</span></span></span></span>

@20jasper
Copy link
Contributor Author

I could also probably assume no one else is just throwing mathml in their posts and include it if there is mathml at all

@20jasper
Copy link
Contributor Author

I've managed to conditionally set a frontmatter prop, but for whatever reason, astro is always importing the CSS no matter how I try to conditionally import

@20jasper
Copy link
Contributor Author

I was able to conditionally import via a CDN link, but not sure how we feel about that

@20jasper 20jasper mentioned this pull request Aug 22, 2025
@fennifith fennifith merged commit a192849 into main Aug 22, 2025
5 checks passed
@fennifith fennifith deleted the katex-support branch August 22, 2025 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants