Skip to content

Commit 1e319a9

Browse files
committed
katex not working
1 parent 5e9c71c commit 1e319a9

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

app/blog/posts/now-playing-implementation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class AudioCNN(nn.Module):
193193
- Formula:
194194

195195
$$
196-
||f(a) - f(p)||_2^2 + margin \leq ||f(a) - f(n)||_2^2
196+
f(a) - f(p)_2^2 + margin \leq ||f(a) - f(n)||_2^2
197197
$$
198198

199199
Here, $(f(x))$ is the embedding function (our CNN in this case), and \(a\), \(p\), and \(n\) are the anchor, positive, and negative inputs, respectively.

app/components/mdx.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Image from 'next/image'
33
import { MDXRemote } from 'next-mdx-remote/rsc'
44
import { highlight } from 'sugar-high'
55
import React from 'react'
6+
import katex from 'katex'
7+
import 'katex/dist/katex.min.css'
8+
69

710
function Table({ data }) {
811
let headers = data.headers.map((header, index) => (
@@ -86,6 +89,38 @@ function createHeading(level) {
8689
return Heading
8790
}
8891

92+
93+
function BlockMath({ value }) {
94+
try {
95+
return (
96+
<pre className="katex-block">
97+
<code
98+
dangerouslySetInnerHTML={{
99+
__html: katex.renderToString(value, { throwOnError: false }),
100+
}}
101+
/>
102+
</pre>
103+
)
104+
} catch (e) {
105+
return <pre>{value}</pre> // Fallback if KaTeX fails to render
106+
}
107+
}
108+
109+
// KaTeX rendering for math
110+
function InlineMath({ value }) {
111+
try {
112+
return (
113+
<span
114+
dangerouslySetInnerHTML={{
115+
__html: katex.renderToString(value, { throwOnError: false }),
116+
}}
117+
/>
118+
)
119+
} catch (e) {
120+
return <span>{value}</span> // Fallback if KaTeX fails to render
121+
}
122+
}
123+
89124
let components = {
90125
h1: createHeading(1),
91126
h2: createHeading(2),
@@ -97,8 +132,11 @@ let components = {
97132
a: CustomLink,
98133
code: Code,
99134
Table,
135+
inlineMath: InlineMath, // Add KaTeX inline math component
136+
math: BlockMath, // Add KaTeX block math component
100137
}
101138

139+
102140
export function CustomMDX(props) {
103141
return (
104142
<MDXRemote

app/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Analytics } from '@vercel/analytics/react'
77
import { SpeedInsights } from '@vercel/speed-insights/next'
88
import Footer from './components/footer'
99
import { baseUrl } from './sitemap'
10+
import 'katex/dist/katex.min.css'
1011

1112
export const metadata: Metadata = {
1213
metadataBase: new URL(baseUrl),

app/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { BlogPosts } from 'app/components/posts'
2+
import 'katex/dist/katex.min.css' // Import KaTeX CSS here
3+
24

35
export default function Page() {
46
return (

next.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const nextConfig = {
44
output: 'export',
55
basePath: process.env.PAGES_BASE_PATH,
66
assetPrefix: process.env.NEXT_PUBLIC_BASE_PATH || '',
7+
latex: {
8+
renderer: 'katex',
9+
options: {
10+
macros: {
11+
'\\RR': '\\mathbb{R}'
12+
}
13+
}
14+
}
715

816
};
917

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@vercel/analytics": "^1.1.3",
1414
"@vercel/speed-insights": "^1.0.9",
1515
"geist": "1.2.2",
16+
"katex": "^0.16.22",
1617
"next": "14.2.8",
1718
"next-mdx-remote": "^4.4.1",
1819
"postcss": "^8.4.35",

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)