Skip to content

Commit 15467c7

Browse files
staredclaude
andcommitted
Fix LaTeX rendering in hover explanations
Add KaTeX rendering for inline math formulas ($...$) in term definitions displayed in hover explanations. - Add renderLatexInText() function to process $...$ LaTeX syntax - Update showDefinition() to render LaTeX before displaying - Fixes: $V(x)$, $\hbar$, $m$, etc. now render as formatted math Example: "$V(x)$ represents forces" → properly formatted V(x) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4087dbd commit 15467c7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import katex from 'katex';
22
import './style.css';
33
import { loadContent, type ParsedContent } from './parser';
44

5+
// Render LaTeX in text (handles $...$ inline math)
6+
function renderLatexInText(text: string): string {
7+
return text.replace(/\$([^\$]+)\$/g, (_match, latex) => {
8+
try {
9+
return katex.renderToString(latex, { displayMode: false, throwOnError: false, strict: false });
10+
} catch (e) {
11+
return `$${latex}$`;
12+
}
13+
});
14+
}
15+
516
// Color schemes - arrays indexed by order of appearance
617
interface ColorScheme {
718
name: string;
@@ -76,7 +87,7 @@ function setupHoverEffects() {
7687
};
7788

7889
const showDefinition = (definition: string) => {
79-
hoverDiv.innerHTML = definition;
90+
hoverDiv.innerHTML = renderLatexInText(definition);
8091
hoverDiv.classList.add('visible');
8192
};
8293

0 commit comments

Comments
 (0)