// ============================================================ // A4 文档模板 // 封面:页边距 0,中文黑体 / 英文 Source Code Pro // 正文:上下 3.5cm,左右 2cm,中文宋体 / 英文 Times New Roman // 代码块:Source Code Pro // ============================================================ // ---- 字体配置(可按需修改)---- #let cover-cjk = "SimHei" // 封面中文:黑体 #let cover-latin = "Source Code Pro" // 封面英文 #let body-cjk = "SimSun" // 正文中文:宋体 #let body-latin = "Times New Roman" // 正文英文:新罗马 #let mono-font = "Source Code Pro" // 代码块 // ---- 封面网格系统 ---- // 坐标系:左上角为原点,x 向右、y 向下;坐标记为 (x, y) 即 (列, 行)。 // 1 单位 = 1.2cm,一个单位方块为边长 1.5cm 的正方形。 #let unit = 1.2cm #let sky = rgb("#4FC3F7") // 天蓝色 #let ochre = rgb("#C9A24B") // 土黄色 // 在网格 (x, y) 处放置内容(以页面左上角为基准) #let cell(x, y, body) = place(top + left, dx: x * unit, dy: y * unit, body) // 实心单位正方形 #let sq(x, y, color) = cell(x, y, rect(width: unit, height: unit, fill: color, stroke: none)) // 单位等腰直角三角形,corner 指定直角所在角: // "tl" 左上 / "tr" 右上 / "bl" 左下 / "br" 右下 #let tri(x, y, corner, color) = cell(x, y, { let pts = if corner == "tr" { ((0pt, 0pt), (unit, 0pt), (unit, unit)) } else if corner == "tl" { ((0pt, 0pt), (unit, 0pt), (0pt, unit)) } else if corner == "bl" { ((0pt, 0pt), (0pt, unit), (unit, unit)) } else { ((unit, 0pt), (unit, unit), (0pt, unit)) } polygon(fill: color, stroke: none, ..pts) }) // 单元格字母字号(NITLOAD 与主标题共用,保证视觉一致) #let glyph-size = 30pt // 在单位格内居中放置一个字母 #let glyph(x, y, ch, color) = cell(x, y, box( width: unit, height: unit, inset: 0pt, align(center + horizon, text(font: cover-cjk, size: glyph-size, weight: "medium", fill: color)[#ch]), )) // ---- 封面 ---- // 所有字段均可选,留空则自动跳过,实现自动排版。 #let cover( title: none, // 主标题(中文) title-en: none, // 主标题(英文,Source Code Pro 自行排版) subtitle: none, // 副标题 logo: none, // 图片路径,如 image("logo.png") author: none, // 作者(可为单个字符串或数组) info: (:), // 附加信息字典,如 (学号: "123", 指导教师: "张三") date: none, // 日期 fill: white, // 封面背景色 decorate: true, // 是否绘制天蓝/土黄网格装饰色块 ) = { // 封面页:页边距为 0 set page( paper: "a4", margin: 0pt, fill: fill, header: none, footer: none, ) set text(font: (cover-latin, cover-cjk), lang: "zh") // 网格装饰:NITLOAD 标识(坐标 (x, y) = (列, 行))。 if decorate { // 天蓝色实心单位正方形 // 土黄色实心单位正方形 sq(0, 0, sky); sq(1, 0, sky); sq(2, 0, ochre) sq(0, 1, sky); sq(2, 1, sky) sq(0, 2, sky); sq(2, 2, sky) sq(0, 3, sky); sq(2, 3, sky) sq(0, 4, sky); sq(2, 4, sky) sq(0, 5, sky); sq(2, 5, sky) // NITLOAD 竖排字样(列 x=1, 行 y=0..6),N 为土黄,其余天蓝 let letters = ("N", "I", "T", "L", "O", "A", "D") for (i, ch) in letters.enumerate() { glyph(1, i, ch, if i == 0 { ochre } else { sky }) } // 主标题:中文逐字对齐网格,英文居中于单元格行。 // 第 1 列到右缘的整格数 = 中文每行字数。 let title-cols = calc.floor((21cm - 1 * unit) / unit) let title-width = 21cm - 1 * unit let cs = if type(title) == str { title.clusters() } else { () } // 中文逐字对齐网格;混排西文字母按 2/3 格宽打包,预留整数格。 // 中文视觉重心偏大,整体右移 0.1 格、下移 0.2 格 let cn-dx = 0.1 * unit let cn-dy = 0.2 * unit let is-narrow = c => str.to-unicode(c.codepoints().first()) < 0x2E80 // 分段:连续西文合为一段,其余每字一段 let segs = () let run = "" for ch in cs { if is-narrow(ch) { run += ch } else { if run != "" { segs.push(run); run = "" } segs.push(ch) } } if run != "" { segs.push(run) } // 逐段放置:CJK 占 1 格,西文段占 ceil(字数 * 1/2) 格,溢出换行 let cur = 0 let row = 0 for seg in segs { let w = if is-narrow(seg.first()) { calc.ceil(seg.len() * 1 / 2) } else { 1 } if cur + w > title-cols { cur = 0; row += 1 } let col = 1 + cur place(top + left, dx: col * unit + cn-dx, dy: (7 + row) * unit + cn-dy, box(width: w * unit, height: unit, inset: 0pt, align(center + horizon, text(font: (cover-cjk, cover-latin), size: glyph-size, fill: black)[#seg]))) cur += w } let cn-rows = if segs.len() == 0 { 0 } else { row + 1 } // 英文:左对齐于中文下方;多行时行距对齐网格(行距 = unit),左/上边距相等。 // 英文行数需测量,故把依赖行数的笔画/三角形一并放入 context。 context { let en-rows = 0 if title-en != none { let en1 = text(font: (cover-latin, cover-cjk), size: glyph-size, fill: black)[#title-en] let lh = measure(en1).height let pad = (unit - lh) / 2 let inner = { set par(leading: unit - lh) en1 } let h = measure(box(width: title-width - pad, inner)).height en-rows = calc.max(1, int(calc.round((h - lh) / unit)) + 1) cell(1, 7 + cn-rows, box(width: title-width, inset: (left: pad, top: pad), inner)) } let title-rows = cn-rows + en-rows // 左侧一笔向下延伸至主标题下端,三角形收尾;右侧保持原样 for r in range(6, 6 + title-rows) { sq(0, r, sky) } tri(0, 6 + title-rows, "tr", sky) } tri(2, 6, "tl", sky) } pagebreak(weak: true) } // ---- 正文配置 ---- #let conf( number-pages: true, // 是否显示页码 doc, ) = { set page( paper: "a4", margin: (top: 3.5cm, bottom: 3.5cm, left: 2cm, right: 2cm), numbering: if number-pages { "1" } else { none }, number-align: center, ) set text(font: (body-latin, body-cjk), lang: "zh", size: 12pt) set par(justify: true, leading: 1em, first-line-indent: (amount: 2em, all: true)) // 标题字体:中文黑体,英文 Source Code Pro show heading: set text(font: (cover-latin, cover-cjk)) // 代码块与行内代码:Source Code Pro show raw: set text(font: mono-font) doc }