How to shrink a table to the page no matter what, like \adjustbox

I’ve googled about this for a while and I haven’t found a great solution.

Let’s say I have a very wide and long table, so much that it stretches on to the next page, or typst tries to spread long cells over multiple rows.

I don’t want any of these solutions, I simple want to make the font, spacing, sizing, etc. smaller, re-scaling everything, so that it fits on the page. In LaTeX, you can do this with \begin{adjustbox} etc.

I haven’t found a solution along these lines using typst yet. Can someone give me an example?

Hello. Unfortunately, it doesn’t seem like zero and one-liner can work together here, but without zero it’s very straightforward:

// #import "@preview/zero:0.5.0": *
#import "@preview/suiji:0.4.0": gen-rng-f, random-f
#import "@preview/one-liner:0.2.0": fit-to-width
// #show: format-table(none, auto)
#let rng = gen-rng-f(0)
#let columns = 25
#let rows = 16
#let (rng, numbers) = random-f(rng, size: columns * rows)
#fit-to-width(table(
  columns: columns,
  table.header(..range(1, columns + 1).map(n => [V#n])),
  ..numbers.map(x => [#calc.round(x * 6 - 3, digits: 2)])
))

1 Like

Thanks, and sorry for the delayed reply.

I think this is an okay solution, but I’m not super confident because

  1. It doesn’t allow fit-to-height, meaning I still have bunched up text at the bottom, in cases where my table is too tall rather than too wide
  2. Adjustbox doesn’t just affect the font size. It shrinks everything, including spacing in and around the cell. So this is probably not going to look as good as adjustbox.

Does typst have some notion of “relative sizes within a box”? I wonder if typst is set up to implement this feature.

Ask author to improve the package, if possible.

relative

The docs are outdated right now.


Do you not want to use scale?

Thank you. scale does exactly what I want. If you have any ideas to say “scale this so that y does not exceed page height and x does not exceed page width” in a convenient way, that would be even more helpful, but I think I can get there eventually.

1 Like

To do that, you need a reference value to track while rescaling. How do you get this value? Which value indicates that you can stop scaling down/up? There is also scale.reflow that might need to be used for that.

@Peter_W_Deffebach Would you mind showing details of how you coded scale?
I tried #scale(auto) and I get:

Typst error: error: x and y cannot both be auto

Manually adjust scale ratio until the table looks good.

Hmm, I though scale would be able to do what:

\begin{adjustbox}{max width=\textwidth}
    % Your large table or figure code here
\end{adjustbox}

does. I mean, I want to throw a table and hope it will shrink enough to fit within the text width if needed…

Use the appropriate language tag in code block info string, even though this forum doesn’t support many other languages. How to post in the Questions category

scale.factor is a shorthand, as explained in the docs, and it’s a bug that auto is documented as possible value (needs to be reported). auto for x/y will just copy the same non-auto value of the opposite axis. This is the only auto behavior.

You can open a feature request, though it kinda sounds like a complicated one and not much practical purpose other than fixing what can’t be fit by default, which is a rare case.

1 Like

Thanks @Andrew. I did put ````latex` for the code block, but like you said, it probably does not have support.
I will explore other options. I was just wondering how scale worked for @Peter_W_Deffebach.

1 Like