iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💯

Creating Exam Answer Boxes with Typst

に公開1件

Since it looked interesting, I implemented something like answerbox.sty in Typst.

https://github.com/1STEP621/typst-anshere

Usage

You can get the general idea by looking at the Example.
https://github.com/1STEP621/typst-anshere/blob/main/example/example.typ

#anshere((
  q(),
  q(),
  q-blank,
  q-break,

  ..(q(),) * 3,
  q-break,

  q(content: [#h(1fr)m / s]),
  q(content: [#h(1fr)T#h(10pt)/#h(10pt)F#h(1fr)]),
  q(label: [$2x + 1$]),
  q-break,

  q(counter-reset: 1),
  q(),
  q-blank,
  q-break,

  q(numbering-width: 50pt),
  q(hide-numbering: true)
))

Basically, you chain q(), q-break, and q-blank inside anshere().
You can create a line break with q-break and a blank space with q-blank.
You can pass various arguments to q().

  • content
    Allows writing in the answer field.
    You can use h(1fr) to add spacing appropriately.
  • label
    Allows writing in the question number field.
  • counter-reset
    Forcibly resets the question number.
  • numbering-width
    Changes the width of the question number field.
  • hide-numbering
    Hides the question number.
#anshere((
  ..(q(numbering: "a"),) * 4,
  ..(q-blank,) * 2,
  q-break,

  q(counter-reset: 1),
  q(),
  q(counter-skip: 1),
  q(),
  q(counter-skip: 1),
  q(),
  q-break,

  q(counter-reset: 1),
  q(),
  q(counter-skip: -1),
  q(),
  q(counter-skip: -1),
  q(),
  q-break,
))
  • numbering
    Changes the numbering style.
  • counter-skip
    Skips n numbers. It is recommended to use this over counter-reset when possible.
#anshere((
  q(children: (
    q(),
    q(),
    q(children: (
        q(),
        q(),
    )),
  )),
))

#anshere(
  (
    q(children: (
      q(),
      q(children: (
        q(),
        q(children: (
          q(),
          q(),
        )),
      )),
    )),
  ),
  numberings: ("1", "1", "(1)", "あ", "a"),
)
  • children
    You can nest questions to create sub-questions within sub-questions. Note that you cannot use q-break or q-blank inside this (everything will be line-broken).

By passing numberings as an argument to anshere, you can specify the question number format collectively.

Impressions

Initially, I thought about calculating colspan and rowspan in a table, but I gave up on that and implemented it by nesting grids instead.
One issue that occurred because of the grid was that the row heights would become misaligned. For now, I've made it so that height is maintained—at least in Noto Serif CJK JP and Noto Sans CJK JP—by always displaying a zero-width space sym.zws, but it might still misalign depending on the font combination. I want to fix this properly using tables someday...

Discussion