How to write long multiplication/division math blocks in Typst



Hello Typst community,
I need some help at writing this into Typst, namely to convert these math calculations into Typst code. I guess there is a trick to make the script better without having to make it complicated.

I once had to do polynomial long division for a homework assignment, and I did it with table:

#table(
  columns: 8,
  stroke: none,
  table.vline(x: 3, stroke: (thickness: 0.8pt), start: 1, end: 2),
  table.hline(y:1, start: 3, stroke: (thickness: 0.8pt)),
  table.hline(y: 3, start: 3, end: 6),
  table.hline(y: 5, start: 4, end: 7),
  $$, $$, $$, $6x^2$, $+6x$, $+0$, $$, $$,
  $x^2$, $+x$, $-2$, $6x^4$, $-2x^3$, $+x^2$, $-3 x$, $+1$,
  $$, $$, $$, $6x^4$, $+6 x^3$, $+2 x^2$, $$, $$,
  $$, $$, $$, $$, $6 x^3$, $+6 x^2$, $-3 x$, $$,
  $$, $$, $$, $$, $6x^3$, $+6x^2$, $+2x$, $$,
  $$, $$, $$, $$, $$, $$, $2 x$, $+1$
)

I think the only way right now to get the double line like you have beneath “1051” and “182” is by adding a super thin row with a stroke on top and on bottom (Double lines and tables: a feature request? - #6 by Andrew), but maybe there’s a better way now, I don’t know.

However, I should have used grid, not table. (“The grid element is intended for presentational and layout purposes, while the table element is intended for, in broad terms, presenting multiple related data points.” Grid - Typst Documentation)

#let r(it) = text(red, it)
#let g(it) = text(green, it)

#grid(
  columns: 5,
  align: center,
  stroke: none,
  inset: 0.2em,
  $1$, $4$, $dot$, r($1$), g($3$),
  grid.hline(),
  $$, $$, r($1$), r($4$), $$,
  $$, $$, $$, g($4$), g($2$),
  grid.hline(),
  $$, $$, $1$, $8$, $2$,
  grid.hline(start: 2),
  grid.cell(colspan: 5, inset: 1pt)[],
  grid.hline(start: 2)
)

This looks amazing exactly how I imagine long multiplication should be. Thank you so much fot this.

Can you also create long division math problem just like the image? The polynomial long division looks a little bit different.

Just to additionally comment to your answer: Im fine with underlining one line only, on the result. I didn’t care much until you mentioned it.

There is some previous work around the topic of writing out the solutions to problems like this:

Multiplication and long division are included but the format is different from yours:


I saved it in case if i want to calculate that way. Thanks for your help.