o

one-liner

@preview

Automatically adjust the text size to make it fit on one line filling the available space.

v0.3.0
MIT

Package Information

Last Updated
Minimum Typst Version
0.12.0
Authors
Marco
Categories
layouttextutility

1. Get the package

Download the package using the TPIX CLI:

tpix get @preview/one-liner:0.3.0

2. Import in your Typst file

Add this to your .typ file:

#import "@preview/one-liner:0.3.0": *

Version History

0.3.00.12.0
25e904176e17...
0.2.00.12.0
f630cfdd8a8b...
0.1.00.12.0
377575857b7d...

One-liner typst package

One-liner is a package containing helper functions to fit text to the available width, without wrapping, by
adjusting the text size based upon the context. This is useful in templates where you
don't know the length of text that is supposed to fit in specific locations in your template.

Example

The current version(0.3.0) one-liner contains 3 functions: fit-to-width that can used as follows:

#import "@preview/one-liner:0.3.0": fit-to-width 

#block(
  height: 3cm,
  width: 10cm,
  fill: luma(230),
  inset: 8pt,
  radius: 4pt,
  align(horizon + center,fit-to-width(lorem(2))),
)

#block(
  height: 3cm,
  width: 10cm,
  fill: luma(230),
  inset: 8pt,
  radius: 4pt,
  align(horizon + center,grow-to-width(lorem(2))),
)

#block(
  height: 3cm,
  width: 10cm,
  fill: luma(230),
  inset: 8pt,
  radius: 4pt,
  align(horizon + center,shrink-to-width(lorem(2))),
)

Here we have a block of specific dimensions. Using fit-to-width will change the font-size of the content passed
to fit-to-width so it will fit the full width without wrapping the content.

Example image of fit-to-with result

CAUTION

This package currently only works for target paged(for PDF, PNG, and SVG export).
Target HTML will result in an empty div element

fit-to-width function

Besides content the function has two parameters:

max-text-size of type length. It has a default of 3 times the current text size. When fit-to-width is limited by the max-text-size you will see that not
the entire width of space will be used.

min-text-size of type length. It has a default of 0.33 times the current text size. When fit-to-width is limited by the min-text-size you will see that the text will wrap,
because the min-text-size is bigger than the size that would be required to prevent wrapping.

Changelog

0.3.0

  • changed the default values of max-text-size and min-text-size to a relative of current text-size instead of fixed pt sizes.
  • added functions shrink-to-width and grow-to-width

0.2.0

  • Also take available height into consideration when calculating scale ratio.

0.1.0

  • Initial version

Disclaimer

This package was only tested in a few of my own documents and only to fit text. Not tested with other content yet.