Is there any simple way of creating a three-line table like Latex?

In Latex, a three-line table can be created by \toprule, \midrule and \bottomrule. Is there any similar solution in Typst?

1 Like

Sure. If you look at the documentation of the booktab package for LaTeX, you will find these two lengths:

\heavyrulewidth=.08em
\lightrulewidth=.05em

So we can use them to mimic the booktab look, like this:

#import "@preview/metalogo:1.0.2": TeX, LaTeX

#set table(stroke: none)

#let toprule = table.hline(stroke: 0.08em)
#let bottomrule = toprule
#let midrule = table.hline(stroke: 0.05em)

#table(
  columns: 2,
  toprule,
  table.header(
    [Name],
    [Made public]
  ),
  midrule,
  [Typst], [2023],
  LaTeX, [1984],
  TeX, [1978],
  bottomrule
)

a

3 Likes

Thank you very much!

Thanks a lot for your answer. How can I make a “three-line table” template or somethings, while there are a lot of tables in a document, and it’s kind of tiring to write “toprule”/“midrule”/“bottomrule” in every table instance.

1 Like

You can create a function that does this for you. This example takes a list of values as its only argument:

#import "@preview/metalogo:1.0.2": TeX, LaTeX

#let three-line-table(cells) = {
  let toprule = table.hline(stroke: 0.08em)
  let bottomrule = toprule
  let midrule = table.hline(stroke: 0.05em)
  table(
    columns: 2,
    stroke: none,
    toprule,
    table.header(..cells.slice(0, count: 2)),
    midrule,
    ..cells.slice(2),
    bottomrule
  )
}

#three-line-table(
  (
    [Name], [Made public],
    [Typst], [2023],
    LaTeX, [1984],
    TeX, [1978],
  )
)
Result

image

2 Likes

Nice, but the solution you proposed is hard-coded to columns: 2. Here is a more general solution using arguments; it uses the same arguments as a normal table, but adds another named one called header, which is an array of cells (I don’t know how to extract table.header from the arguments, so I put its content aside as an additional argument).

#import "@preview/metalogo:1.0.2": TeX, LaTeX

#set page(
  width: auto,
  height: auto,
  margin: 3em,
)

#set table(stroke: none)

#let toprule = table.hline(stroke: 0.08em)
#let bottomrule = toprule
#let midrule = table.hline(stroke: 0.05em)

#let booktab(..args) = {
  let named = args.named()
  let header = named.remove("header")
  table(
    ..named,
    toprule,
    ..header,
    midrule,
    ..args.pos(),
    bottomrule
  )
}

#booktab(
  columns: 2,
  header: (
    [Name],
    [Made public],
  ),
  [Typst], [2023],
  LaTeX, [1984],
  TeX, [1978],
)
3 Likes

The rubber-article – Typst Universe has a nice and easy way to do it, using ctable:

This function will display a custom table. The table uses the pillar package under the hood to interact with the table in a similar manner as in Latex. This means, that the columns and vertical lines can be defined with a string. Furthermore, the table automatically adds 3 horizontal lines.

#import "@preview/rubber-article:0.4.0": ctable

#ctable(
  cols:"l|cr",
  [A], [B], [C],
 ..range(1,16).map(str),
)

image

4 Likes