This commit is contained in:
team3
2026-07-13 16:11:00 +02:00
parent 9cd8e02e22
commit a8e8e077bd
14 changed files with 454 additions and 62 deletions

View File

@@ -90,10 +90,22 @@ _TEX_UMLAUTE = {"a": "ä", "o": "ö", "u": "ü", "A": "Ä", "O": "Ö", "U": "Ü"
# \ss ist ein Kontrollwort und frisst das Folge-Leerzeichen: hei\ss t → heißt
_TEX_SZ = re.compile(r"\\ss(?![a-zA-Z])(?:\{\})?[ \t]?")
# Parameterlose Textmakros aus der Präambel: \newcommand{\PP}{\mathrm{P}}.
# Ohne Expansion bleibt \PP roh im Rumpf stehen — der Reader las daraus die
# „Klasse PP" (aak: P-Definition falsch betitelt, falsches Level, falscher
# Baustein). Eine Klammer-Schachtelungsebene reicht für \mathrm{P}-Fälle.
_TEX_MAKRO = re.compile(
r"\\(?:re)?newcommand\*?\{?\\([A-Za-z]+)\}?\{((?:[^{}]|\{[^{}]*\})*)\}")
def _tex_normalisieren(inhalt: str) -> str:
_, sep, rumpf = inhalt.partition(r"\begin{document}")
praeambel, sep, rumpf = inhalt.partition(r"\begin{document}")
if sep:
makros = {m.group(1): m.group(2) for m in _TEX_MAKRO.finditer(praeambel)
if "#" not in m.group(2)} # nur parameterlos
for name in sorted(makros, key=len, reverse=True): # \PP vor \P
rumpf = re.sub(rf"\\{name}(?![a-zA-Z])(?:\{{\}})?",
makros[name].replace("\\", "\\\\"), rumpf)
inhalt = rumpf
inhalt = inhalt.replace(r"\end{document}", "")
inhalt = _TEX_UMLAUT.sub(lambda m: _TEX_UMLAUTE[m.group(1)], inhalt)