蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 TeX 四层架构、选型矩阵与中英文源码导言区/正文区划分

PYTHONWindows x64、CPython 3.12.14 离线 LaTeX 语法、环境嵌套与工程依赖验证器(未调用本地 TeX 排版引擎)。explore-intro.py
explore-intro.py201 行
"""Offline LaTeX verification lab for Chapter 02 (intro)."""
import json
import re

VERSION = "CPython 3.12.14 offline LaTeX ecosystem & workflow verifier"


def case_tex_four_layer_architecture():
    layers = [
        {"level": 1, "name": "引擎", "examples": ["TeX", "pdfTeX", "XeTeX", "LuaTeX"], "unicode_native": ["XeTeX", "LuaTeX"]},
        {"level": 2, "name": "格式", "examples": ["LaTeX"]},
        {"level": 3, "name": "宏包", "examples": ["ctex", "amsmath", "geometry"], "ctan_scale": "5000+"},
        {"level": 4, "name": "编辑器", "examples": ["Overleaf", "TeXstudio", "VS Code + LaTeX Workshop", "TeXworks"]},
    ]
    can_compile_local_editor_only = False
    can_compile_overleaf_cloud = True
    actual = {
        "layer_order": [x["name"] for x in layers],
        "unicode_engines": layers[0]["unicode_native"],
        "sample_packages": layers[2]["examples"],
        "editor_only_local_compile": can_compile_local_editor_only,
        "overleaf_cloud_compile": can_compile_overleaf_cloud,
    }
    expected = {
        "layer_order": ["引擎", "格式", "宏包", "编辑器"],
        "unicode_engines": ["XeTeX", "LuaTeX"],
        "sample_packages": ["ctex", "amsmath", "geometry"],
        "editor_only_local_compile": False,
        "overleaf_cloud_compile": True,
    }
    return {
        "name": "tex_four_layer_architecture",
        "summary": "验证 TeX 四层生态模型(引擎/格式/宏包/编辑器)及本地编辑器与云端编译边界",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_latex_vs_word_and_scenarios():
    comparison_dims = [
        "排版质量", "学习曲线", "自动化程度", "数学公式支持", "跨平台支持", "文档结构", "扩展性"
    ]
    suitable_scenarios = [
        "学术论文与毕业论文", "数学、物理等理工讲义", "图书与长报告", "简历", "演示文稿"
    ]
    unsuitable_scenarios = ["大量图形设计海报", "多人实时批注办公文档", "无公式急稿短文档"]

    def recommend(has_heavy_math: bool, needs_realtime_comments: bool, long_doc: bool) -> str:
        if needs_realtime_comments and not has_heavy_math:
            return "Word/在线办公套件"
        if has_heavy_math or long_doc:
            return "LaTeX"
        return "轻量编辑器"

    actual = {
        "comparison_dim_count": len(comparison_dims),
        "suitable_count": len(suitable_scenarios),
        "unsuitable_count": len(unsuitable_scenarios),
        "thesis_rec": recommend(True, False, True),
        "office_memo_rec": recommend(False, True, False),
    }
    expected = {
        "comparison_dim_count": 7,
        "suitable_count": 5,
        "unsuitable_count": 3,
        "thesis_rec": "LaTeX",
        "office_memo_rec": "Word/在线办公套件",
    }
    return {
        "name": "latex_vs_word_and_scenarios",
        "summary": "校验 LaTeX 与 Word 七维对比、五类高适配场景及三类不建议强用场景决策模型",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_distribution_and_editor_matrix():
    distributions = {
        "TeX Live": {"platforms": ["Windows", "macOS", "Linux"], "basic_mb": 300, "full_gb": 8.0},
        "MacTeX": {"platforms": ["macOS"], "basic_mb": 100, "full_gb": 5.0},
        "MiKTeX": {"platforms": ["Windows"], "basic_mb": 300, "on_demand": True},
        "TinyTeX": {"platforms": ["Windows", "macOS", "Linux"], "basic_mb": 150, "on_demand": True},
    }
    editors = {
        "Overleaf": {"mode": "online", "zero_install": True},
        "TeXstudio": {"mode": "local_desktop", "zero_install": False},
        "VS Code + LaTeX Workshop": {"mode": "local_plugin", "zero_install": False},
        "TeXworks": {"mode": "local_desktop", "zero_install": False},
    }
    cross_platform_dists = sorted(k for k, v in distributions.items() if len(v["platforms"]) == 3)
    online_editors = sorted(k for k, v in editors.items() if v["mode"] == "online")
    actual = {
        "distribution_count": len(distributions),
        "cross_platform_dists": cross_platform_dists,
        "editor_count": len(editors),
        "online_editors": online_editors,
    }
    expected = {
        "distribution_count": 4,
        "cross_platform_dists": ["TeX Live", "TinyTeX"],
        "editor_count": 4,
        "online_editors": ["Overleaf"],
    }
    return {
        "name": "distribution_and_editor_matrix",
        "summary": "核对四大 TeX 发行版跨平台支持与四类编辑器运行模式矩阵",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_preamble_and_body_partition():
    doc_en = r"""
\documentclass{article}
\title{我的第一个 LaTeX 文档}
\author{作者}
\date{\today}
\begin{document}
\maketitle
\section{引言}
这是我的第一个 LaTeX 文档!\\
这是一个简单的段落。
\section{数学公式}
这是一个行内公式:$E = mc^2$。\\
\[
\int_a^b f(x) \, dx
\]
\end{document}
"""
    doc_cn = r"""
% 文档类:ctexart 等价于 article 加上中文支持
\documentclass{ctexart}
\title{我的第一份 \LaTeX{} 文档}
\author{林远}
\date{2026 年 8 月}
\begin{document}
\maketitle
LaTeX 特别擅长数学公式排版。质能方程的行内形式是 $E = mc^2$,
把它单独居中展示(行间公式)则写成:
\[ E = mc^2 \]
其中 $E$ 是能量,$m$ 是质量,$c$ 是真空中的光速。
\end{document}
"""

    def inspect_doc(src: str) -> dict:
        clean = re.sub(r"(?<!\\)%.*", "", src)
        cls = re.search(r"\\documentclass\{([^}]+)\}", clean).group(1)
        b = re.search(r"\\begin\{document\}", clean)
        e = re.search(r"\\end\{document\}", clean)
        preamble = clean[: b.start()]
        body = clean[b.end() : e.start()]
        return {
            "class": cls,
            "chinese_ready": cls.startswith("ctex"),
            "preamble_commands": sorted(set(re.findall(r"\\(documentclass|title|author|date)\b", preamble))),
            "has_maketitle": r"\maketitle" in body,
            "display_math": len(re.findall(r"\\\[.+?\\\]", body, re.S)),
        }

    actual = {"article_doc": inspect_doc(doc_en), "ctexart_doc": inspect_doc(doc_cn)}
    expected = {
        "article_doc": {
            "class": "article",
            "chinese_ready": False,
            "preamble_commands": ["author", "date", "documentclass", "title"],
            "has_maketitle": True,
            "display_math": 1,
        },
        "ctexart_doc": {
            "class": "ctexart",
            "chinese_ready": True,
            "preamble_commands": ["author", "date", "documentclass", "title"],
            "has_maketitle": True,
            "display_math": 1,
        },
    }
    return {
        "name": "preamble_and_body_partition",
        "summary": "拆解 article 与 ctexart 示例源码的导言区/正文区边界与中文文档类就绪状态",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_tex_four_layer_architecture(),
        case_latex_vs_word_and_scenarios(),
        case_distribution_and_editor_matrix(),
        case_preamble_and_body_partition(),
    ]
    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-intro-lab/explore-intro.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-intro.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 LaTeX 语法、环境嵌套与工程依赖验证器(未调用本地 TeX 排版引擎)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:LaTeX

课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2294.zip

下载课程关联工程包:LaTeX

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2296.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习