Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
math_tutor_dev
public_math_tutor
Commits
738fb627
Commit
738fb627
authored
Mar 30, 2026
by
Kantz
Browse files
anpassung der notation an Synatx-hilfe von Anselm
parent
bdccc414
Changes
1
Hide whitespace changes
Inline
Side-by-side
math-tutor/frontend/src/utils/mathMarkdown.ts
View file @
738fb627
const
MATH_SEGMENT_PATTERN
=
/
(\$\$[\s\S]
*
?\$\$
|
\\\[[\s\S]
*
?\\\]
|
\\\([\s\S]
*
?\\\)
|
\$(?:\\
.|
[^
$
\\\n])
+
\$)
/g
;
// wandlet * in \* um sodass * in der MArkdown darstellung nich zum zeichen
// für kursiv oder fett gedruckt wird.
const
escapeAsterisks
=
(
value
:
string
)
=>
value
.
replace
(
/
\*
/g
,
"
\\
*
"
);
// Wandelt einfache Bruchschreibweisen in LaTeX-Brüche um.
// Beispiele:
// - (x+1)/(x-1) -> \frac{x+1}{x-1}
// - a/b -> \frac{a}{b}
//
// Diese Ersetzung läuft nur innerhalb erkannter Math-Segmente, damit
// normale Markdown-Links, URLs oder Fließtext unangetastet bleiben.
const
convertSimpleFractions
=
(
value
:
string
)
=>
{
let
output
=
value
;
// Vorrang für explizit geklammerte Ausdrücke:
// (x+1)/(x-1) => \frac{x+1}{x-1}
output
=
output
.
replace
(
/
\(([^
()
]
+
)\)\s
*
\/\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
numerator
:
string
,
denominator
:
string
)
=>
`\\frac{
${
numerator
}
}{
${
denominator
}
}`
);
// Einseitig geklammerte Ausdrücke:
// (a+b)/c => \frac{a+b}{c}
// a/(b+c) => \frac{a}{b+c}
// Diese Regel kommt vor der ganz einfachen a/b-Regel, damit die Klammern
// erhalten bleiben und nicht versehentlich nur Teilstücke ersetzt werden.
output
=
output
.
replace
(
/
\(([^
()
]
+
)\)\s
*
\/\s
*
([
A-Za-z0-9
]
+
)(?![\w
{
])
/g
,
(
_match
,
numerator
:
string
,
denominator
:
string
)
=>
`\\frac{
${
numerator
}
}{
${
denominator
}
}`
);
output
=
output
.
replace
(
/
([
A-Za-z0-9
]
+
)\s
*
\/\s
*
\(([^
()
]
+
)\)(?![\w
{
])
/g
,
(
_match
,
numerator
:
string
,
denominator
:
string
)
=>
`\\frac{
${
numerator
}
}{
${
denominator
}
}`
);
// Danach sehr einfache Kurzschreibweise:
// a/b => \frac{a}{b}
// Hinweis: Das ist bewusst konservativ gehalten, damit keine komplexen
// Ausdrücke oder schon vorhandene LaTeX-Befehle umgeschrieben werden.
output
=
output
.
replace
(
/
(
^|
[^\\\w
}
])([
A-Za-z0-9
]
+
)\s
*
\/\s
*
([
A-Za-z0-9
]
+
)(?![\w
{
])
/g
,
(
_match
,
prefix
:
string
,
numerator
:
string
,
denominator
:
string
)
=>
`
${
prefix
}
\\frac{
${
numerator
}
}{
${
denominator
}
}`
);
return
output
;
};
// Formatiert Potenzen so, dass zusammengesetzte Exponenten in Klammern
// vollständig in LaTeX-Klammern gesetzt werden.
// Beispiel:
// - x^(a+b) -> x^{a+b}
const
normalizePowers
=
(
value
:
string
)
=>
value
.
replace
(
/
([
A-Za-z0-9
\\]
+
)\s
*
\^\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
base
:
string
,
exponent
:
string
)
=>
`
${
base
}
^{
${
exponent
}
}`
);
// Wandelt sqrt(...) in LaTeX-Wurzelnotation um.
// Beispiel:
// - sqrt(x) -> \sqrt{x}
const
normalizeSquareRoots
=
(
value
:
string
)
=>
value
.
replace
(
/
\b
sqrt
\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
inner
:
string
)
=>
`\\sqrt{
${
inner
}
}`
);
// Wandelt exp(x) in e^x um.
// Beispiel:
// - exp(x) -> e^x
const
normalizeExp
=
(
value
:
string
)
=>
value
.
replace
(
/
\b
exp
\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
inner
:
string
)
=>
`e^
${
inner
}
`
);
// Wandelt log(x) in eine lesbare Logarithmus-Notation um.
// Beispiel:
// - log(x) -> \log x
const
normalizeLog
=
(
value
:
string
)
=>
value
.
replace
(
/
\b
log
\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
inner
:
string
)
=>
`\\log
${
inner
}
`
);
// Wandelt pi in das griechische LaTeX-Symbol um.
// Beispiel:
// - pi -> \pi
const
normalizePi
=
(
value
:
string
)
=>
value
.
replace
(
/
\b
pi
\b
/g
,
"
\\
pi
"
);
// Wandelt sin/cos/tan-Aufrufe in LaTeX-Makros um.
// Beispiele:
// - sin(a) -> \sin(a)
// - cos(a) -> \cos(a)
// - tan(a) -> \tan(a)
const
normalizeTrigFunctions
=
(
value
:
string
)
=>
value
.
replace
(
/
\b(
sin|cos|tan
)\s
*
\(([^
()
]
+
)\)
/g
,
(
_match
,
fn
:
string
,
inner
:
string
)
=>
`\
\$
{fn}(
${
inner
}
)`
);
// Wandelt inf/minf in Infinities um.
// Beispiele:
// - inf -> \infty
// - minf -> - \infty
const
normalizeInfinityTokens
=
(
value
:
string
)
=>
value
.
replace
(
/
\b
minf
\b
/g
,
"
-
\\
infty
"
)
.
replace
(
/
\b
inf
\b
/g
,
"
\\
infty
"
);
// Macht bereits geschriebene Mengenklammern in Math-Segmenten sichtbar.
// Beispiele:
// - \{1, 2, 3\} -> \\{1, 2, 3\\}
// - \{x \in \mathbb{R} \mid x > 0\} -> \\{x \in \mathbb{R} \mid x > 0\\}
//
// Wichtig:
// - Rohe {a,b,c} werden absichtlich NICHT umgeschrieben, damit z. B. x_{a,b}
// nicht fälschlich als Menge interpretiert wird.
// - Nur bereits escapte Klammern werden angehoben, damit MathJax sie sichtbar
// rendert statt sie als Gruppierungszeichen zu schlucken.
const
normalizeSetBraces
=
(
value
:
string
)
=>
value
.
replace
(
/
\\\{
/g
,
"
\\\\
{
"
)
.
replace
(
/
\\\}
/g
,
"
\\\\
}
"
);
export
const
escapeAsterisksInsideMath
=
(
input
:
string
):
string
=>
{
return
input
.
replace
(
MATH_SEGMENT_PATTERN
,
(
segment
)
=>
{
if
(
segment
.
startsWith
(
"
$$
"
)
&&
segment
.
endsWith
(
"
$$
"
))
{
const
inner
=
segment
.
slice
(
2
,
-
2
);
return
`$$
${
escapeAsterisks
(
inner
)}
$$`
;
return
`$$
${
normalizeSetBraces
(
normalizeInfinityTokens
(
normalizeTrigFunctions
(
normalizePi
(
normalizeLog
(
normalizeExp
(
normalizeSquareRoots
(
normalizePowers
(
convertSimpleFractions
(
escapeAsterisks
(
inner
)
)))))))))
}
$$`
;
}
if
(
segment
.
startsWith
(
"
\\
[
"
)
&&
segment
.
endsWith
(
"
\\
]
"
))
{
const
inner
=
segment
.
slice
(
2
,
-
2
);
return
`\\[
${
escapeAsterisks
(
inner
)}
\\]`
;
return
`\\[
${
normalizeSetBraces
(
normalizeInfinityTokens
(
normalizeTrigFunctions
(
normalizePi
(
normalizeLog
(
normalizeExp
(
normalizeSquareRoots
(
normalizePowers
(
convertSimpleFractions
(
escapeAsterisks
(
inner
)
)))))))))
}
\\]`
;
}
if
(
segment
.
startsWith
(
"
\\
(
"
)
&&
segment
.
endsWith
(
"
\\
)
"
))
{
const
inner
=
segment
.
slice
(
2
,
-
2
);
return
`\\(
${
escapeAsterisks
(
inner
)}
\\)`
;
return
`\\(
${
normalizeSetBraces
(
normalizeInfinityTokens
(
normalizeTrigFunctions
(
normalizePi
(
normalizeLog
(
normalizeExp
(
normalizeSquareRoots
(
normalizePowers
(
convertSimpleFractions
(
escapeAsterisks
(
inner
)
)))))))))
}
\\)`
;
}
if
(
segment
.
startsWith
(
"
$
"
)
&&
segment
.
endsWith
(
"
$
"
))
{
const
inner
=
segment
.
slice
(
1
,
-
1
);
return
`$
${
escapeAsterisks
(
inner
)}
$`
;
return
`$
${
normalizeSetBraces
(
normalizeInfinityTokens
(
normalizeTrigFunctions
(
normalizePi
(
normalizeLog
(
normalizeExp
(
normalizeSquareRoots
(
normalizePowers
(
convertSimpleFractions
(
escapeAsterisks
(
inner
)
)))))))))
}
$`
;
}
return
segment
;
});
};
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment