fix: "숫자+단위"뿐인 인라인 LaTeX($60\sim80\text{cm}$)를 평문으로 푼다

모델이 "60~80cm", "25°C" 같은 걸 습관적으로 `$...$`로 감싸는데, KaTeX로 렌더하면
수식 이탤릭체라 한글 문장에서 어색하고 TTS·복사도 깨진다. \text{}/\mathrm{} 단위가
있고 나머지가 숫자·범위기호·간단한 연산자뿐이면 유니코드 평문으로 치환한다
(\sim→~, \times→×, ^2→² 등). 치환 뒤 `\ ^ _ { } =`가 남으면 진짜 수식이라 그대로 둔다.

chat-render.js(메인챗)·katex-inline.js(앱페이지) 둘 다.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rfme1WVEPkpNwnf5oVNXXc
This commit is contained in:
kimandClaude Sonnet 5 committed 2026-09-07 10:27:01 +09:00
1 parent bf6aae12b3
commit 5834242261
2 files changed
+47 -2

No files matched your search

+24 -1
View File
@@ -350,6 +350,27 @@ function _walkTextNodes(root, fn) {
for (const n of nodes) fn(n);
}
// `$60\sim80\text{cm}$`, `$25\text{°C}$`처럼 "숫자 + 단위"뿐인 인라인 수식은 모델이 습관적으로
// LaTeX로 감싼 것일 뿐 진짜 수식이 아니다(KaTeX로 렌더하면 이탤릭 수식체라 한글 문장에서 어색하고
// TTS·복사도 망가진다). 단위(\text{}/\mathrm{})가 있고 나머지가 숫자·범위기호·간단한 연산자뿐이면
// 평문으로 푼다. 치환 뒤에도 `\ ^ _ { } =`가 남으면 진짜 수식이므로 손대지 않는다.
function _unwrapTrivialUnitMath(s) {
if (!s.includes('$')) return s;
return s.replace(/\$([^$\n]+)\$/g, (full, body) => {
if (!/\\(?:text|mathrm)\{/.test(body)) return full;
const t = body
.replace(/\\(?:text|mathrm|mathbf|mathit)\{([^{}]*)\}/g, '$1')
.replace(/\\sim/g, '~').replace(/\\approx/g, '≈').replace(/\\times/g, '×')
.replace(/\\cdot/g, '·').replace(/\\div/g, '÷').replace(/\\pm/g, '±')
.replace(/\\(?:le|leq)\b/g, '≤').replace(/\\(?:ge|geq)\b/g, '≥').replace(/\\neq\b/g, '≠')
.replace(/\\to\b/g, '→').replace(/\\(?:ldots|dots)\b/g, '…')
.replace(/\\(?:circ|degree)\b/g, '°')
.replace(/\^\{?2\}?/g, '²').replace(/\^\{?3\}?/g, '³')
.replace(/\\[,!\s]/g, ' ').replace(/\s+/g, ' ').trim();
return /[\\^_{}=]/.test(t) ? full : t;
});
}
// `$` 다음에 숫자가 오면 보통 통화지만, `$60\sim80\text{cm}$`처럼 다음 `$`까지의 구간에
// 백슬래시(LaTeX 명령)가 있으면 그건 수식이다 — 그 `$`는 건드리지 않는다.
function _maskCurrencyDollars(s) {
@@ -365,7 +386,9 @@ function renderKatexIn(container) {
if (!window.renderMathInElement || !container) return;
try {
_walkTextNodes(container, (n) => {
if (n.nodeValue.includes('$')) n.nodeValue = _maskCurrencyDollars(n.nodeValue);
if (n.nodeValue.includes('$')) {
n.nodeValue = _maskCurrencyDollars(_unwrapTrivialUnitMath(n.nodeValue));
}
});
renderMathInElement(container, {
delimiters: [
+23 -1
View File
@@ -24,6 +24,26 @@
for (var i = 0; i < nodes.length; i++) fn(nodes[i]);
}
// `$60\sim80\text{cm}$`, `$25\text{°C}$`처럼 "숫자 + 단위"뿐인 인라인 수식은 모델이 습관적으로
// LaTeX로 감싼 것일 뿐 진짜 수식이 아니다. 단위(\text{}/\mathrm{})가 있고 나머지가 숫자·범위기호·
// 간단한 연산자뿐이면 평문으로 푼다. 치환 뒤에도 `\ ^ _ { } =`가 남으면 진짜 수식이라 손대지 않는다.
function unwrapTrivialUnitMath(s) {
if (s.indexOf('$') === -1) return s;
return s.replace(/\$([^$\n]+)\$/g, function (full, body) {
if (!/\\(?:text|mathrm)\{/.test(body)) return full;
var t = body
.replace(/\\(?:text|mathrm|mathbf|mathit)\{([^{}]*)\}/g, '$1')
.replace(/\\sim/g, '~').replace(/\\approx/g, '≈').replace(/\\times/g, '×')
.replace(/\\cdot/g, '·').replace(/\\div/g, '÷').replace(/\\pm/g, '±')
.replace(/\\(?:le|leq)\b/g, '≤').replace(/\\(?:ge|geq)\b/g, '≥').replace(/\\neq\b/g, '≠')
.replace(/\\to\b/g, '→').replace(/\\(?:ldots|dots)\b/g, '…')
.replace(/\\(?:circ|degree)\b/g, '°')
.replace(/\^\{?2\}?/g, '²').replace(/\^\{?3\}?/g, '³')
.replace(/\\[,!\s]/g, ' ').replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '');
return /[\\^_{}=]/.test(t) ? full : t;
});
}
// `$` 다음에 숫자가 오면 보통 통화지만, `$60\sim80\text{cm}$`처럼 다음 `$`까지의 구간에
// 백슬래시(LaTeX 명령)가 있으면 그건 수식이다 — 그 `$`는 건드리지 않는다.
function maskCurrencyDollars(s) {
@@ -39,7 +59,9 @@
if (!window.renderMathInElement || !container) return;
try {
walkTextNodes(container, function (n) {
if (n.nodeValue.indexOf('$') !== -1) n.nodeValue = maskCurrencyDollars(n.nodeValue);
if (n.nodeValue.indexOf('$') !== -1) {
n.nodeValue = maskCurrencyDollars(unwrapTrivialUnitMath(n.nodeValue));
}
});
window.renderMathInElement(container, {
delimiters: [