I was missing LaTeX’s \textwidth
command, as I needed to use it for some scaling stuff, so here you go:
/// Computes the current page's textwidth
/// -> length
#let textwidth() = context {
// Margins are set automatically to 2.5/21 times the smaller dimension of the
// page.
let default-margin = (2.5 / 21) * calc.min(page.width, page.height)
if page.margin == auto {
return page.width - (2 * default-margin)
}
if "x" in page.margin {
return page.width - (2 * page.margin.x.length)
}
let left-margin = if "left" in page.margin {
page.margin.left.length
} else if "inside" in page.margin {
page.margin.inside.length
} else if "rest" in page.margin {
page.margin.rest.length
} else { default-margin }
let right-margin = if "right" in page.margin {
page.margin.right.length
} else if "outside" in page.margin {
page.margin.outside.length
} else if "rest" in page.margin {
page.margin.rest.length
} else { default-margin }
return page.width - left-margin - right-margin
}
The margins can be configured in many forms (see Page Function – Typst Documentation), so that’s why it’s so long.
TIP: If you’d like to use it inside a context
function, just pass page
as an parameter:
/// Computes the current page's textwidth
///
/// - page (function): Typst's `page` function
/// -> length
#let textwidth(page) = {
// ...
}
#let foo() = context {
textwidth(page)
}