Skip to content

Instantly share code, notes, and snippets.

@npanuhin
Last active October 28, 2023 00:42
Show Gist options
  • Save npanuhin/3843fdf240e8b0156f31e2a6b317156c to your computer and use it in GitHub Desktop.
Save npanuhin/3843fdf240e8b0156f31e2a6b317156c to your computer and use it in GitHub Desktop.
My typst homework template
// The project function defines how your document looks.
// It takes your content and some metadata and formats it.
// Go ahead and customize it to your liking!
// Save heading and body font families in variables.
#let _body-font = "Linux Libertine"
// #let _body-font = "PT Serif"
#let _sans-font = "HK Grotesk"
// let _sans-font = "Inconsolata"
// #let _sans-font = "Liberation Sans"
// #let _sans-font = "TeX Gyre Heros"
#let homework_project(
target: "",
author: "",
date: "",
title: "",
body
) = {
// let document_title = [#target: #title]
// Set the document's basic properties.
set document(author: author, title: title)
set page(
numbering: "1",
margin: (
x: 1.5cm,
y: 1.5cm,
// top: 2cm,
// bottom: 1.5cm
),
number-align: end,
// header: [
// #move(
// dy: 0.4em,
// [
// #grid(
// columns: (1fr, 1fr, 1fr),
// align(left)[
// #target
// ],
// align(center)[
// #title
// ],
// align(right)[
// #author
// ]
// )
// ]
// )
// #line(
// length: 100%
// )
// #v(0.6em)
// ],
// footer: [#target: #title #h(1fr) #counter(page).display("1")]
)
// Set body font family.
set text(font: _body-font, lang: "en")
show heading: set text(font: _sans-font)
set par(justify: true)
set text(hyphenate: false)
// set text(font: _sans-font)
place(
top + right,
dy: -1.9em,
align(right)[
// #target \
#text(
size: 1.1em,
font: "TeX Gyre Heros",
[#author]
)
]
)
align(center)[
= #title
]
linebreak()
body
}
#let centered(body) = {
set align(center)
[#body]
}
#let under(body, subscript, additional_space: 3pt, down_shift: 1pt) = style(styles => {
let totalwidth = measure($limits(body)_subscript$, styles).width
let bodywidth = measure(body, styles).width
let overlapwidth = (totalwidth - bodywidth) / 2 + additional_space
let subscriptwidth = measure(subscript, styles).width
$
#h(overlapwidth) // weak: true
// The nested box ensures that the subscript is not broken into multiple lines
&limits(body)_(
#box(
width: 0pt,
baseline: down_shift,
align(
center,
box(
width: subscriptwidth,
subscript
)
)
)
)
#h(overlapwidth) // weak: true
$
})
#let qed = {
h(1fr)
$qed$
}
#let polynom_div(left_params, divisor, result) = style(styles => box({
place(
top + left,
line(
start: (
measure(left_params.at(0), styles).width + 1.5em,
-0.2em
),
length: 2.55em,
angle: 90deg,
stroke: 0.5pt
)
)
place(
top + left,
line(
start: (
measure(left_params.at(0), styles).width + 1.5em,
1em
),
length: calc.max(
measure(divisor, styles).width,
measure(result, styles).width
) + 1.33em,
stroke: 0.5pt
)
)
place(
top + left,
dx: measure(left_params.at(0), styles).width + 2.3em,
dy: -0.1em,
divisor
)
let i = 0
for item in left_params {
if calc.rem(i, 2) == 0 {
if i != 0 {
linebreak()
box(
width: 0em,
height: 0em,
place(
top + left,
line(
start: (
calc.quo(i, 2) * 2.5em - 2.1em,
-1em
),
length: calc.max(
measure(left_params.at(i - 2), styles).width,
measure(left_params.at(i - 1), styles).width
) + 0.5em,
stroke: 0.5pt
)
)
)
}
h(calc.quo(i, 2) * 2.5em + 0.5em)
$item$
} else {
linebreak()
box(
width: 0em,
height: 0em,
place(
top + left,
dx: calc.quo(i, 2) * 2.5em -0.3em,
dy: -1.44em,
$-$
)
)
h(calc.quo(i, 2) * 2.5em + 0.5em)
$item$
}
i += 1
}
place(
top + left,
dx: measure(left_params.at(0), styles).width + 2.3em,
dy: 1.3em,
result
)
}))
#let div_by = [
$#move(dy: -0.16em, [$dots.v$])$
]
#let homework_title(target_name, author, date, title) = {
set text(font: _sans-font)
set align(right)
[
#target_name
\
#author
\
#date
]
set align(center)
[= #title]
linebreak()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment