-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlean4-typesetting-standard.tex
More file actions
448 lines (356 loc) · 13.6 KB
/
lean4-typesetting-standard.tex
File metadata and controls
448 lines (356 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
\documentclass[UTF8,a4paper,12pt]{ctexart}
% ============================================================
% 基础宏包
% ============================================================
\usepackage{amsmath,amssymb,amsthm}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{fvextra}
\usepackage{hyperref}
\geometry{margin=2.5cm}
\emergencystretch=2em
\hypersetup{
colorlinks=true,
linkcolor=blue,
urlcolor=blue,
citecolor=blue
}
% ============================================================
% 字体设置
% ============================================================
% Lean4 代码推荐使用 JuliaMono。
% 若本机没有 JuliaMono,可临时改为 Cascadia Mono 或 Consolas;
% 但正式论文建议安装 JuliaMono,以获得更好的 Lean4 Unicode 支持。
\setmonofont{JuliaMono}[
Scale=MatchLowercase
]
% ============================================================
% 颜色方案
% ============================================================
\definecolor{LeanBg}{RGB}{248,248,248}
\definecolor{LeanFrame}{RGB}{170,180,190}
\definecolor{LeanText}{RGB}{25,25,25}
\definecolor{LeanSmallBg}{RGB}{250,250,250}
\definecolor{LeanSmallFrame}{RGB}{190,190,190}
\definecolor{LeanDefBg}{RGB}{245,252,247}
\definecolor{LeanDefFrame}{RGB}{70,145,90}
\definecolor{LeanThmBg}{RGB}{255,250,242}
\definecolor{LeanThmFrame}{RGB}{205,130,45}
\definecolor{LeanInlineBg}{RGB}{242,244,248}
% ============================================================
% 行内 Lean4 代码
% ============================================================
% 安全行内 Lean4 代码。
% 可以直接包含下划线,例如 \leaninline{N_ge_2}。
% 注意:该命令适合短代码;长表达式请使用 LeanSmall。
\newcommand{\leaninline}[1]{%
\begingroup
\setlength{\fboxsep}{1.2pt}%
\colorbox{LeanInlineBg}{%
\ttfamily\small\detokenize{#1}%
}%
\endgroup
}
% ============================================================
% Lean4 代码块环境
% ============================================================
%
% 重要使用规则:
%
% 1. 不使用 minted,不需要 --shell-escape。
% 2. 代码块基于 fvextra 的 Verbatim。
% 3. \begin{LeanSmall}、\begin{LeanDef}、\begin{LeanThm}、
% \begin{LeanCode} 后面不要加空格或注释。
% 4. 代码块标题写在代码块外部,不写成环境可选参数。
% 5. 较长 Lean4 表达式不要放在行内,改用 LeanSmall。
% 短小代码块:用于正文中的短定义、短表达式、短定理目标。
\DefineVerbatimEnvironment{LeanSmall}{Verbatim}
{
breaklines=true,
breakanywhere=true,
fontsize=\small,
tabsize=2,
frame=single,
framesep=2mm,
rulecolor=\color{LeanSmallFrame},
bgcolor=LeanSmallBg,
formatcom=\color{LeanText}
}
% 形式化定义代码块:绿色,用于关键定义。
\DefineVerbatimEnvironment{LeanDef}{Verbatim}
{
breaklines=true,
breakanywhere=true,
fontsize=\small,
numbers=left,
numbersep=5pt,
tabsize=2,
frame=single,
framesep=2mm,
rulecolor=\color{LeanDefFrame},
bgcolor=LeanDefBg,
formatcom=\color{LeanText}
}
% 定理声明代码块:橙色,用于主要定理声明。
\DefineVerbatimEnvironment{LeanThm}{Verbatim}
{
breaklines=true,
breakanywhere=true,
fontsize=\small,
numbers=left,
numbersep=5pt,
tabsize=2,
frame=single,
framesep=2mm,
rulecolor=\color{LeanThmFrame},
bgcolor=LeanThmBg,
formatcom=\color{LeanText}
}
% 普通较长代码块:灰色,用于附录或较长摘录。
\DefineVerbatimEnvironment{LeanCode}{Verbatim}
{
breaklines=true,
breakanywhere=true,
fontsize=\small,
numbers=left,
numbersep=5pt,
tabsize=2,
frame=single,
framesep=2mm,
rulecolor=\color{LeanFrame},
bgcolor=LeanBg,
formatcom=\color{LeanText}
}
% ============================================================
% 文档信息
% ============================================================
\title{Lean4 代码在 XeLaTeX 中文论文中的保守排版标准}
\author{XCreeperPa}
\date{\today}
\begin{document}
\maketitle
\begin{abstract}
本文给出一种适合中文 XeLaTeX 论文的 Lean4 代码排版标准。
该方案不依赖 minted,不调用外部语法高亮程序,也不需要开启 shell escape。
其核心目标是保证编译稳定、Lean4 Unicode 符号可显示、代码块具有清晰的语义层次。
本文档本身即应用该标准进行排版。
\end{abstract}
\section{设计目标}
本文档采用一种保守的 Lean4 代码排版方案。
该方案服务于形式化数学论文写作,尤其适合需要在中文正文中插入少量 Lean4 定义与定理声明的场景。
该标准的目标如下:
\begin{enumerate}
\item 保证 XeLaTeX 编译稳定;
\item 避免依赖外部程序;
\item 正常显示 Lean4 常用 Unicode 符号;
\item 在视觉上区分定义、定理声明和普通代码摘录;
\item 正文只放关键形式化片段,完整代码交由 GitHub 仓库保存。
\end{enumerate}
本标准不追求自动语法高亮。换言之,本文并不对 Lean4 关键字、标识符、注释等进行逐词染色。
取而代之的是结构化上色:定义使用绿色边框,定理声明使用橙色边框,普通代码使用灰色边框。
\section{基本原则}
正文中的 Lean4 标识符可以使用行内代码,例如
\leaninline{Strategy.Red}、\leaninline{Strategy.Blue}、\leaninline{GameConfig} 和
\leaninline{Claim}。
但是,行内代码只适合较短的名称。较长表达式应当放入行间小代码块。例如下列表达式不建议连续放在中文段落中:
\begin{LeanSmall}
Fin config.N → Strategy
Claim config → Prop
count_blue config profile > config.N / 2
\end{LeanSmall}
这样做可以避免行内不可断行内容造成版面溢出。
\section{行间小代码块}
短小定义、短小类型声明或短小定理目标使用 \leaninline{LeanSmall} 环境。
例如,红蓝选择问题中的策略剖面可以形式化为:
\begin{LeanSmall}
def StrategyProfile (config : GameConfig) :=
Fin config.N → Strategy
\end{LeanSmall}
再如,一个短小的计数定理声明可以写作:
\begin{LeanSmall}
theorem count_blue_all_red
(config : GameConfig) :
count_blue config (fun _ : Fin config.N => Strategy.Red) = 0
\end{LeanSmall}
\section{形式化定义代码块}
关键定义使用绿色的 \leaninline{LeanDef} 环境。该环境带有行号,适合正文中需要重点解释的 Lean4 定义。
\subsection{策略与游戏配置}
红蓝选择问题中只有两种策略:红色和蓝色。
游戏配置给出玩家数量,并要求玩家数不少于二。
其 Lean4 定义如下:
\begin{LeanDef}
inductive Strategy where
| Red : Strategy
| Blue : Strategy
deriving DecidableEq, Repr
structure GameConfig where
N : ℕ
N_ge_2 : N ≥ 2
\end{LeanDef}
这里,\leaninline{Strategy} 是策略类型,\leaninline{GameConfig} 是游戏配置。
字段 \leaninline{N} 表示玩家数量,\leaninline{N_ge_2} 表示假设 $N\ge 2$。
\subsection{个人收益函数}
个人收益函数表达如下规则:选择红色的玩家总是存活;选择蓝色的玩家只有在蓝色人数严格过半时存活。
在 Lean4 中,该定义为:
\begin{LeanDef}
def payoff_in_profile
(config : GameConfig)
(profile : StrategyProfile config)
(player : Fin config.N) : ℕ :=
match profile player with
| Strategy.Red => 1
| Strategy.Blue =>
if count_blue config profile > config.N / 2 then 1 else 0
\end{LeanDef}
该代码块使用绿色边框,因为它是正文中需要解释的核心形式化定义。
\subsection{最终合理策略集合}
理由系统中,最终合理策略集合定义为存在一个最终理由证书。
形式化定义如下:
\begin{LeanDef}
def ReasonableStrategies
(config : GameConfig)
(BaseReason : Claim config → Prop)
(DependencyAllowed : Claim config → Finset (Claim config) → Prop)
(Dominates : Claim config → Claim config → Prop)
(player : Fin config.N) : Strategy → Prop :=
fun strategy =>
Nonempty
(FinalReasonFor
config
BaseReason
DependencyAllowed
Dominates
player
strategy)
\end{LeanDef}
在论文正文中,应当先用自然语言说明该定义的含义,再给出 Lean4 摘录。
完整证明代码不宜全部放入正文,而应放入附录或代码仓库。
\section{定理声明代码块}
主要结论的 Lean4 定理声明使用橙色的 \leaninline{LeanThm} 环境。
该环境用于突出论文中的形式化验证目标。
\subsection{命题一的最终合理性结论}
命题一表达:在个人收益优先的理由结构中,红色是任意玩家的最终合理策略。
Lean4 中的对应定理声明为:
\begin{LeanThm}
theorem red_reasonable
(config : GameConfig)
(player : Fin config.N) :
ReasonableStrategies
config
(IndividualDominanceReason config)
(NoDependency config)
(IndividualDominates config)
player
Strategy.Red
\end{LeanThm}
与此配套,还应有蓝色不属于最终合理策略集合的定理声明。
正文中通常只需展示关键定理声明,完整证明见仓库。
\subsection{命题二的最终合理性结论}
命题二表达:当存在非空且尚未独自过半的铁蓝集合时,在集体福利优先的政策比较下,蓝色是任意玩家的最终合理策略。
Lean4 中的对应定理声明为:
\begin{LeanThm}
theorem blue_reasonable
(config : GameConfig)
(Base : Finset (Fin config.N))
(hBaseNonempty : Base.Nonempty)
(hBaseCard : Base.card ≤ config.N / 2)
(player : Fin config.N) :
ReasonableStrategies
config
(PolicyStrictImprovementReason config Base)
(NoDependency config)
(PolicyDominates config Base)
player
Strategy.Blue
\end{LeanThm}
这里的 \leaninline{Base} 表示外部锚点,即已经固定为蓝色的玩家集合。
\subsection{命题三的无理由结论}
命题三表达:在无自足理由、无外部锚点、无焦点的完全对称情形下,不存在任何最终合理策略。
Lean4 中的统一结论可以写为:
\begin{LeanThm}
theorem no_reason
(config : GameConfig)
(player : Fin config.N)
(strategy : Strategy) :
ReasonableStrategies
config
(NoBaseReason config)
(CollectiveOptimalDependency config)
(NoDominance3 config)
player
strategy → False
\end{LeanThm}
该定理同时覆盖红色和蓝色,因为 \leaninline{strategy} 是任意策略。
\section{较长代码摘录}
较长 Lean4 代码摘录使用灰色的 \leaninline{LeanCode} 环境。
该环境适合附录或技术说明部分。
例如,有限导出的理由证书可形式化为如下归纳类型:
\begin{LeanCode}
inductive Derivable
(config : GameConfig)
(BaseReason : Claim config → Prop)
(DependencyAllowed : Claim config → Finset (Claim config) → Prop) :
Claim config → Type where
| base
(claim : Claim config) :
BaseReason claim →
Derivable config BaseReason DependencyAllowed claim
| depends
(claim : Claim config)
(deps : Finset (Claim config)) :
DependencyAllowed claim deps →
(∀ dep : Claim config,
dep ∈ deps →
Derivable config BaseReason DependencyAllowed dep) →
Derivable config BaseReason DependencyAllowed claim
\end{LeanCode}
该环境包含行号,适合引用较长代码片段。
但正式论文中仍不建议全文粘贴所有 Lean4 文件。
完整代码应以 GitHub 仓库或 Zenodo 归档作为主要载体。
\section{Unicode 符号显示检查}
Lean4 代码中常见许多 Unicode 数学符号。
因此,等宽字体必须支持这些符号。
本标准推荐使用 JuliaMono 字体。
下列符号若能正常显示,则说明当前字体配置适合 Lean4 代码排版:
\begin{LeanSmall}
ℕ ≥ → ∀ ∈ ∧ ¬ ≤ := fun _ => Strategy.Blue
\end{LeanSmall}
如果这些符号显示为空框或缺字,应安装 JuliaMono,或者临时改用其他支持数学符号的等宽字体。
但从 Lean4 论文排版角度看,JuliaMono 是更稳妥的选择。
\section{正式论文中的使用建议}
在正式论文中,建议遵循如下规则:
\begin{enumerate}
\item 正文中只展示关键 Lean4 定义和最终定理声明;
\item 自然语言证明作为正文主体;
\item Lean4 证明代码作为附录说明或仓库材料;
\item 行内代码只用于短标识符;
\item 较长 Lean4 表达式使用 \leaninline{LeanSmall};
\item 核心定义使用 \leaninline{LeanDef};
\item 主要定理声明使用 \leaninline{LeanThm};
\item 较长技术摘录使用 \leaninline{LeanCode}。
\end{enumerate}
因此,一篇采用本标准的论文可以在正文中先解释红蓝选择问题的自然语言含义,再给出少量 Lean4 定义,最后通过定理声明说明形式化验证目标。
这样既能保持论文可读性,又能清楚展示形式化验证的精确内容。
\section{编译方式}
本标准不依赖外部语法高亮程序。
因此,编译时只需要普通 XeLaTeX:
\begin{LeanSmall}
xelatex main.tex
xelatex main.tex
\end{LeanSmall}
不需要使用 shell escape。
如果论文包含交叉引用、目录或参考文献,按照常规 LaTeX 流程多次编译即可。
\section{结论}
本文给出了一套面向 Lean4 中文论文的保守排版标准。
该标准放弃自动语法高亮,改用结构化彩色代码块,以换取更高的编译稳定性。
对于需要在六小时内完成论文排版和发布的场景,该方案比 minted 更稳妥。
本标准的核心组合是:
\begin{LeanSmall}
XeLaTeX
JuliaMono
fontspec
fvextra
\end{LeanSmall}
在该组合下,Lean4 代码能够以清晰、稳定、可读的方式嵌入中文数学论文。
\end{document}