docs: improved ratio and relative length docs

Co-authored-by: PgBiel <9021226+PgBiel@users.noreply.github.com>
This commit is contained in:
Andrew Voynov 2025-01-24 22:53:06 +03:00
parent 176b070c77
commit 2c251d42fe
No known key found for this signature in database
GPG Key ID: 1BE92DD685700329
2 changed files with 43 additions and 1 deletions

View File

@ -8,7 +8,47 @@ use crate::foundations::{repr, ty, Repr};
/// A ratio of a whole.
///
/// Written as a number, followed by a percent sign.
/// Written as a number, followed by a percent sign. A common use case is
/// setting the width or height of a container (e.g., [block], [rect], etc.),
/// thus it is used as a part of a [relative length](relative) (because
/// internally containers use relative length for width and height fields):
///
/// ```example
/// #block(width: 240pt, {
/// rect(width: 25%, layout(size => size.width))
/// })
/// ```
///
/// Here the block width is set to `{250pt}` (just to demonstrate the use of
/// ratio with containers), and inside of it the rectangle width is set to
/// `{25%}`, which means "get 25% of the width of the innermost container" (240
/// ⋅ 0.25 = 60). The reason it shows `{50pt}` instead of `{60pt}` is due to
/// the default value of inset. If we set it to `{0pt}`, then we will get
/// the expected result (although the number will be cramped):
///
/// ```example
/// #block(width: 240pt, {
/// rect(width: 25%, inset: 0pt, layout(size => size.width))
/// })
/// ```
///
/// If you are trying to use the page width/height's value as the base for the
/// ratio, then the value is equal to the width/height of the page minus the
/// margins (left and right for width, top and bottom for height). To force
/// Typst to use the full length you have a few options (this highly depends on
/// your exact use case):
///
/// 1. set page margins to `{0pt}` (`#set page(margin: 0pt)`)
/// 2. multiply by a full length value (`{21cm * 69%}`)
/// 3. use padding which will negate the margins (`#pad(x: -2.5cm, ...)`)
/// 4. use page [background](page.background)/[foreground](page.foreground)
/// field that doesn't use the margins (note that it will render the content
/// outside of the document flow, see [place] to control the content
/// position)
///
/// However, within your own code, you can use ratios as you'd like. You can
/// multiply ratio by ratio, [length], [relative length](relative), [angle],
/// [int], [float], and [fraction].
///
/// # Example
/// ```example

View File

@ -14,6 +14,8 @@ use crate::layout::{Abs, Em, Length, Ratio};
/// addition and subtraction of a length and a ratio. Wherever a relative length
/// is expected, you can also use a bare length or ratio.
///
/// You can multiply relative length by [ratio], [int], and [float].
///
/// # Example
/// ```example
/// #rect(width: 100% - 50pt)