forked from hongjr03/typst-assignment-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.typ
256 lines (223 loc) · 6.27 KB
/
template.typ
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
#import "@preview/numblex:0.1.1": numblex, circle_numbers
#import "@preview/indenta:0.0.3": fix-indent
#let font = (main: "IBM Plex Serif", mono: "IBM Plex Mono", cjk: "Noto Serif SC")
#let problem_counter = counter("problem")
#let prob-solution_counter = counter("prob-solution")
#let fake-par = {
v(-1em)
box()
}
#let prob_block(body) = {
v(-0.5em)
block(fill: rgb(250, 255, 250), width: 100%, inset: 8pt, radius: 4pt, stroke: rgb(31, 199, 31), body)
}
#let speci_block(title, body) = {
v(-0.5em)
block(
fill: rgb("#1f84c7"),
width: 100%,
inset: (left: 8pt, top: 4pt, bottom: 4pt),
radius: (top: 4pt, bottom: 0pt),
stroke: rgb("#1f84c7"),
)[
#set heading(numbering: none)
#set text(fill: white)
#v(0.3em)
==== #title
#v(-0.3em)
]
v(-12pt)
block(fill: rgb("#fcfdff"), width: 100%, inset: 8pt, radius: (top: 0pt, bottom: 4pt), stroke: rgb("#1f84c7"))[
// #v(0.5em)
#body
]
}
#let prob(text, body) = {
[
#set heading(numbering: none)
=== #text
]
v(0.5em)
if body == [] {
v(0.5em)
} else {
prob_block(body)
}
}
#let cprob(text, body) = {
[
#set heading(numbering: none)
#problem_counter.step()
=== 问题 #context[#problem_counter.display():#text]
]
v(0.5em)
if body == [] {
v(0.5em)
} else {
prob_block(body)
}
}
#let cqa(title, body) = {
[
#set heading(numbering: none)
#prob-solution_counter.step()
=== 问题 #context[#prob-solution_counter.display():#title]
]
v(0.5em)
if body == [] {
v(0.5em)
} else {
speci_block([解决方式], body)
}
}
#let indent(width, content) = grid(
columns: (width, auto),
[], content,
)
// Some math operators
#let prox = [#math.op("prox")]
#let proj = [#math.op("proj")]
#let argmin = [#math.arg] + [#math.min]
#let assignment_class(
size: 10.5pt,
title,
author,
course_id,
professor_name,
semester,
due_time,
id,
lang: "zh",
region: "cn",
body,
) = {
set text(font: (font.main, font.cjk), size: size, lang: lang, region: region)
set heading(numbering: numblex("一、", "1.", "(1)"))
show heading.where(level: 1): it => {
if (it.numbering != none) {
block(counter(heading.where(level: 1)).display("一") + "、" + it.body)
} else {
it
}
}
show heading: it => [
#v(0.2em)
#it
#v(0.5em)
]
// Very useful trick, special thanks to @AxiomOfChoices
// https://github.com/typst/typst/issues/2953#issuecomment-1858823455
show heading: current => (
context {
let elms = query(selector(heading).before(here())) // Finds all previous headings, inclusive
if elms.len() > 1 {
// If there are at least 2
let previous_heading = elms.at(-2) // Take the previous one
if ( // If it is close enough to the current one
previous_heading.location().position().y + 35pt > current.location().position().y
) {
return [#v(-0.3em) #current]
}
}
return current
}
)
// Comments above are not stated by Nowherechan.
// I think this setting is useless, but I still migrate it to typst 0.12
set raw(tab-size: 4)
show link: it => {
set text(fill: blue)
underline(it)
}
set list(indent: 6pt)
set enum(indent: 6pt)
set enum(numbering: numblex(numberings: ("1.", "a)", circle_numbers)), full: true)
set bibliography(title: [参考], style: "ieee")
set document(title: title, author: author)
set page(
paper: "a4",
header: context {
if (here().page() == 1) {
none
} else {
[
#h(1fr)
*#author* | *#title*
]
}
},
footer: context {
let page_number = counter(page).at(here()).first()
let total_pages = counter(page).final().last()
set align(center)
set text(size: 8pt)
[
#page_number / #total_pages
]
},
)
// Title and Header
let make_header(name, width: 453.5pt, max: 17pt, step: 0.1pt) = {
context {
if max == none {
max = text.size
}
let textsize = max
let size = measure(align(left, text(textsize)[#name]))
while size.width > width {
textsize = textsize - step
size = measure(align(left, text(textsize)[#name]))
}
return align(left, text(textsize)[#name])
}
}
let left_text = [
#h(1fr)
*#author* | #id
]
let comma = ","
if lang == "zh" {
comma = ","
}
// let right_text = [*#professor_name*] + [#comma*#semester* ] + [| *截止时间:*#due_time]
// if due_time == none or due_time == "" {
// right_text = [*#professor_name*] + [#comma*#semester* ]
// }
let right_text = [*#author* | #id #h(2em) #due_time]
line(length: 100%)
make_header[*#course_id* | *#title*]
""
h(1fr)
right_text
line(length: 100%)
set raw(tab-size: 4)
show raw: set text(font: (font.mono, font.cjk))
// Display inline code in a small box
// that retains the correct baseline.
show raw.where(block: false): box.with(fill: luma(240), inset: (x: 3pt, y: 0pt), outset: (y: 3pt), radius: 2pt)
let cjk-markers = regex("[“”‘’.,。、?!:;(){}[]〔〕〖〗《》〈〉「」【】『』─—_·…\u{30FC}]+")
show cjk-markers: set text(font: font.cjk)
show raw: it => {
show cjk-markers: set text(font: font.cjk)
it
}
// Display block code in a larger block
// with more padding.
// and with line numbers.
// Thank you @Andrew15-5 for the idea and the code!
// https://github.com/typst/typst/issues/344#issuecomment-2041231063
let style-number(number) = text(gray)[#number]
show raw.where(block: true): it => block(fill: luma(240), inset: 10pt, radius: 4pt, width: 100%)[#grid(
columns: (1em, 1fr),
align: (right, left),
column-gutter: 0.7em,
row-gutter: 0.6em,
..it.lines.enumerate().map(((i, line)) => (style-number(i + 1), line)).flatten(),
)]
set underline(stroke: 0.5pt, offset: 0.2em, extent: 0.2em, evade: false)
set par(first-line-indent: 2em, leading: 1.2em, justify: true)
show: fix-indent()
v(30pt)
fake-par
body
}