How to draw rounded corners for lines and arbitrary shapes?

Hello everyone. Is there a way to round the sharp corners of a line, n-polygon, or any arbitrary path?

For instance, a simple line given by

#import "@preview/cetz:0.3.4"

#cetz.canvas({
  import cetz.draw: *
    line(
      stroke: (thickness: 3pt, cap: "round"),
      (), (1,0),(1,1)
    )
})

Is there an equivalent to something like

\tikz
  \draw[
     rounded corners=3pt,
     line cap=round,
     thick
  ]
     (0,0) -- (1,0) -- (1,1);

Thank you in advance. And thanks for improving the question.

Below are two pictures showcasing the desired output (as rendered by TiKZ) and my current attempt at recreating it in Typst.

The desired output via LaTeX:
LaTeX_TiKZ_Example

Current result via Typst:
CeTZ

Hey @MJ.0, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

Make sure your title is a question you’d ask to a friend about Typst. :wink:

1 Like

It would be great if you could add some pictures showing the results you’re looking for, as not all of us have too much prior experience with LaTeX! :slight_smile:

With that said, for lines specifically, I wonder if you might be looking for Bezier curves. In CeTZ, you can follow this tutorial: A Picture for Karl's New Students | CeTZ Documentation

Though this is also supported natively in Typst through curve: Curve Function – Typst Documentation

Which allows you to implement very complex paths.

For rectangles, though, you can use a rect with radius: Rectangle Function – Typst Documentation

Thanks a lot for the friendly correction.

Thanks for your time, I have updated the question and attached some pictures. The use of Bezier curves/control points is not bad, however it seems more fitting in curves/graphs on a plot or something, but it would be less convenient and impractical to reuse.

I was hoping that just like there is a dedicated property for rectangles to round the corners, there would be a similar property for arbitrary paths/n-polygons; just like it would be impractical to use Bezier curves whenever one wants to draw a simple rounded rectangle.

This isn’t supported natively in typst or cetz unfortunately. Some packages such as fletcher implement corner rounding for paths by calculating and placing separate line segments and joining them with arcs. If you have dashed lines, you need to additionally correct the dash phases for each segment to make the lines and arcs look continuous. If you have a strong stomach for scripting you can take a look at the source code for fletcher.draw-edge-polyline() to see how this can be done. Or perhaps you might want to try fletcher instead of cetz, depending on your drawing.

1 Like

Thank you for your input.

Hopefully, this feature will find its way into Typst one day, as it improves with each update.

For completeness: for non-sharp corners there is the join: "round" stroke property:

#import "@preview/cetz:0.3.4"

#cetz.canvas({
  import cetz.draw: *
    line(
      stroke: (thickness: 3pt, cap: "round", join: "round"),
      (), (1,0),(1,1)
    )
})

image

though it doesn’t allow for controlling the radius as in @MJ.0’s example.

2 Likes