Measure, Layout

This section is outdated. It may be still useful, but it is strongly recommended to study new context system (using the reference).

Style & Measure

Style documentation.

Measure documentation.

measure returns the element size. This command is extremely helpful when doing custom layout with place.

However, there is a catch. Element size depends on styles, applied to this element.

#let content = [Hello!]
#content
#set text(14pt)
#content
Rendered image

So if we will set the big text size for some part of our text, to measure the element's size, we have to know where the element is located. Without knowing it, we can't tell what styles should be applied.

So yep, you are right. We need the context.

#let thing(body) = context {
  let size = measure(body, styles)
  [Width of "#body" is #size.width]
}

#thing[Hey] \
#thing[Welcome]

Layout

Layout is similar to measure, but it returns current scope parent size.

If you are putting elements in block, that will be block's size. If you are just putting right on the page, that will be page's size.

For some technical reasons, however, it can't use context and needs to use the very similar scheme (it is the one the context has emerged from, in fact):

/// It's a black box that receives the parent size and renders something with it:
#layout(size => {
  let half = 50% * size.width
  [Half a page is #half wide.]
})
Rendered image

It may be extremely useful to combine layout with measure, to get width of things that depend on parent's size:

#let text = lorem(30)
#layout(size => style(styles => [
  #let (height,) = measure(
    block(width: size.width, text),
    styles,
  )
  This text is #height high with
  the current page width: \
  #text
]))
Rendered image