Repository: typst/typst
File: crates/typst-library/src/layout/ratio.rs (Rust)

Code before:
use std::fmt::{self, Debug, Formatter};
use std::ops::{Add, Div, Mul, Neg};

use ecow::EcoString;
use typst_utils::{Numeric, Scalar};

use crate::foundations::{repr, ty, Repr};

/// A ratio of a whole.
///
/// 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):

Review comment (suggestion):
I feel like something like this might be better:

```suggestion
/// setting the width or height of a container (e.g., [block], [rect], etc.),
/// as it can be used as part of a [relative length]($relative) to represent
/// a certain percentage of the surrounding container's - or the current
/// page's, if there isn't one - corresponding dimension. For example:
```

Code after:
use std::fmt::{self, Debug, Formatter};
use std::ops::{Add, Div, Mul, Neg};

use ecow::EcoString;
use typst_utils::{Numeric, Scalar};

use crate::foundations::{repr, ty, Repr};

/// A ratio of a whole.
///
/// A ratio is written as a number, followed by a percent sign. Ratios most
/// often appear as part of a [relative length]($relative), to specify the size
/// of some layout element relative to the page or some container.
///
/// ```example
/// #rect(width: 25%)
/// ```
///
/// However, they can also describe any other property that is relative to some
/// base, e.g. an amount of [horizontal scaling]($scale.x) or the
/// [height of parentheses]($math.lr.size) relative to the height of the content
/// they enclose.
///
/// # Scripting
/// Within your own code, you can use ratios as you like. You can multiply them
/// with various other types as shown below:
///
/// |  Multiply by    |  Example                | Result          |
/// |-----------------|-------------------------|-----------------|
/// | [`ratio`]       | `{27% * 10%}`           | `{2.7%}`        |
/// | [`length`]      | `{27% * 100pt}`         | `{27pt}`        |
/// | [`relative`]    | `{27% * (10% + 100pt)}` | `{2.7% + 27pt}` |
/// | [`angle`]       | `{27% * 100deg}`        | `{27deg}`       |
/// | [`int`]         | `{27% * 2}`             | `{54%}`         |
/// | [`float`]       | `{27% * 0.37037}`       | `{10%}`         |
/// | [`fraction`]    | `{27% * 3fr}`           | `{0.81fr}`      |
///
/// When ratios are displayed in the document, they are rounded to two
/// significant digits for readability.