"""Offline LaTeX verification lab for Chapter 06 (latex-math-basic)."""
import json
import re
VERSION = "CPython 3.12.14 offline LaTeX basic math syntax verifier"
def case_inline_display_and_delimiters():
doc = r"""
\documentclass{ctexart}
\usepackage{amsmath}
\begin{document}
一元二次方程 $ax^2 + bx + c = 0$ 的求根公式为
\[
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a},
\]
其中判别式 $\Delta = b^2 - 4ac$ 决定根的个数。
数学中最优美的公式当属欧拉公式
\[
\mathrm{e}^{\mathrm{i}\pi} + 1 = 0,
\]
它把五个最重要的常数联系在了一起。
\end{document}
"""
inline = re.findall(r"(?<!\\)\$(?!\$)(.+?)(?<!\\)\$", doc)
display = [m.strip() for m in re.findall(r"\\\[(.+?)\\\]", doc, re.S)]
uses_legacy_double_dollar = "$$" in doc
blank_before_display = bool(re.search(r"\n\s*\n\s*\\\[", doc))
actual = {
"inline_count": len(inline),
"display_count": len(display),
"uses_legacy_double_dollar": uses_legacy_double_dollar,
"blank_line_before_display": blank_before_display,
"uses_upright_constants": (r"\mathrm{e}" in doc and r"\mathrm{i}" in doc),
}
expected = {
"inline_count": 2,
"display_count": 2,
"uses_legacy_double_dollar": False,
"blank_line_before_display": False,
"uses_upright_constants": True,
}
return {
"name": "inline_display_and_delimiters",
"summary": "验证行内 $...$ 与行间 \\[...\\] 公式解析、旧式 $$ 禁令、段内无空行规范及正体常数 \\mathrm{e}/\\mathrm{i}",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_scripts_fractions_roots_and_left_right():
def parse_script_arg(expr: str) -> str:
m = re.search(r"[\^_](\{[^}]+\}|.)", expr)
return m.group(1) if m else ""
def check_left_right_pairs(expr: str) -> bool:
return len(re.findall(r"\\left[\(\[\{\|\\.]", expr)) == len(re.findall(r"\\right[\)\]\}\|\\.]", expr))
frac_samples = [r"\frac{a+b}{c}", r"\frac{1}{1+\frac{1}{x}}", r"\dfrac{a}{b}"]
root_samples = [r"\sqrt{2}", r"\sqrt[3]{x^2+1}"]
actual = {
"x10_bug_exponent": parse_script_arg("x^10"),
"x10_fixed_exponent": parse_script_arg("x^{10}"),
"nested_power_arg": parse_script_arg("x^{i^2}"),
"sub_arg": parse_script_arg("a_{ij}"),
"nth_root_degree": re.search(r"\\sqrt\[([^\]]+)\]", root_samples[1]).group(1),
"left_right_balanced": check_left_right_pairs(r"\left(\frac{a}{b}\right)"),
"dfrac_present": any(r"\dfrac" in s for s in frac_samples),
}
expected = {
"x10_bug_exponent": "1",
"x10_fixed_exponent": "{10}",
"nested_power_arg": "{i^2}",
"sub_arg": "{ij}",
"nth_root_degree": "3",
"left_right_balanced": True,
"dfrac_present": True,
}
return {
"name": "scripts_fractions_roots_and_left_right",
"summary": "解析上下标单字符与花括号分组差异(x^10 vs x^{10})、分数/n次根号及 \\left(\\right) 成对配平",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_greek_and_math_symbols_catalog():
lower_greek = [
r"\alpha", r"\beta", r"\gamma", r"\delta", r"\epsilon", r"\varepsilon",
r"\theta", r"\lambda", r"\mu", r"\pi", r"\sigma", r"\omega", r"\phi", r"\varphi",
]
upper_greek = [r"\Gamma", r"\Delta", r"\Sigma", r"\Omega"]
arithmetic_rel = [
r"\times", r"\div", r"\pm", r"\mp", r"\cdot", r"\leq",
r"\geq", r"\neq", r"\approx", r"\equiv", r"\ll", r"\gg",
]
set_logic = [
r"\in", r"\notin", r"\subset", r"\subseteq", r"\cup", r"\cap",
r"\emptyset", r"\infty", r"\forall", r"\exists", r"\Rightarrow", r"\Leftrightarrow",
]
calculus_decor = [r"\partial", r"\nabla", r"\to", r"\mapsto", r"\mathrm{d}x", r"\hat{x}", r"\bar{x}", r"\vec{v}"]
invalid_fullwidth = "α+β"
has_illegal_non_ascii_in_math = any(ord(ch) > 127 for ch in invalid_fullwidth)
actual = {
"lower_greek_count": len(lower_greek),
"upper_greek_count": len(upper_greek),
"upper_capitalized_only_first": all(cmd[1].isupper() and cmd[2:].islower() for cmd in upper_greek),
"arithmetic_rel_count": len(arithmetic_rel),
"set_logic_count": len(set_logic),
"calculus_decor_count": len(calculus_decor),
"detects_fullwidth_math_error": has_illegal_non_ascii_in_math,
}
expected = {
"lower_greek_count": 14,
"upper_greek_count": 4,
"upper_capitalized_only_first": True,
"arithmetic_rel_count": 12,
"set_logic_count": 12,
"calculus_decor_count": 8,
"detects_fullwidth_math_error": True,
}
return {
"name": "greek_and_math_symbols_catalog",
"summary": "核对 18 个希腊字母与 32 个高频运算/集合/微积分修饰符号命令及公式内全角字符错误检测",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_big_operators_and_ams_packages():
formulas = [
r"\sum_{i=1}^{n} i = \frac{n(n+1)}{2}",
r"\int_0^{\infty} \mathrm{e}^{-x^2}\,\mathrm{d}x = \frac{\sqrt{\pi}}{2}",
r"\lim_{n \to \infty} \left( 1 + \frac{1}{n} \right)^{n} = \mathrm{e}",
]
ops = [re.search(r"\\(sum|int|lim)(?![a-zA-Z])", f).group(1) for f in formulas]
math_fonts = {
r"\mathbb{R}": "amssymb",
r"\mathcal{F}": "latex_core",
r"\mathbf{v}": "latex_core",
r"\mathrm{e}^{\mathrm{i}\pi}": "latex_core",
r"\dfrac{a}{b}": "amsmath",
}
actual = {
"big_operators": ops,
"inline_limits_override": r"\displaystyle",
"amssymb_required_for_mathbb": math_fonts[r"\mathbb{R}"] == "amssymb",
"amsmath_required_for_dfrac": math_fonts[r"\dfrac{a}{b}"] == "amsmath",
}
expected = {
"big_operators": ["sum", "int", "lim"],
"inline_limits_override": r"\displaystyle",
"amssymb_required_for_mathbb": True,
"amsmath_required_for_dfrac": True,
}
return {
"name": "big_operators_and_ams_packages",
"summary": "验证求和/积分/极限三大运算符公式、\\displaystyle 行内形态控制及 amsmath/amssymb 归属",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_inline_display_and_delimiters(),
case_scripts_fractions_roots_and_left_right(),
case_greek_and_math_symbols_catalog(),
case_big_operators_and_ams_packages(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 LaTeX 语法、环境嵌套与工程依赖验证器(未调用本地 TeX 排版引擎)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 LaTeX 语法、环境嵌套与工程依赖验证器(未调用本地 TeX 排版引擎)。
入口文件:projects/latex-latex-math-basic-lab/explore-latex-math-basic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-latex-math-basic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 LaTeX 语法、环境嵌套与工程依赖验证器(未调用本地 TeX 排版引擎)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2300.zip
课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2294.zip