I have a line like this:







sub asy {return system("asy '$_[0]'");}

As expected, it draws a solid line with rounded edges like this:

However, I would like to draw just the outer border of that line, something like this:

How can I do this?

  • Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Jun 5 '16 at 1:14
  • Sorry I don't know much about Asymptote, but what you provided produces no output for me. Is there something special I need to run when using Asymptote, besides pdflatex? – Peter Grill Jun 5 '16 at 1:18
  • I also have the latexmrc file with some code in it, will edit in a sec. – AMACB Jun 5 '16 at 1:23
  • A user here, Malipivo, has used a \pdfliteral feature to do outlines of glyphs, which may apply in your situation. Here are some examples: tex.stackexchange.com/questions/169636/…, and tex.stackexchange.com/questions/18472/tikz-halo-around-text/…. – Steven B. Segletes Jun 5 '16 at 1:38
  • @StevenB.Segletes Can't seem to make that work with a line in asymptote. – AMACB Jun 5 '16 at 1:44

I have taken the idea in this post, using the graph package to construct the path[] getBorder(path, pen) function as shown in the following code.

import graph;

path[] getBorder(path p, pen origPen)
    real offset = linewidth(origPen)/2.0/72.0;
    pair offsetPoint(real t) { return point(p, t) + offset*(rotate(90)*dir(p,t)); }
    path path1 = graph(offsetPoint, 0, length(p), operator ..);
    offset = -offset;
    path path2 = graph(offsetPoint, 0, length(p), operator ..);

    path[] paths;
    if (cyclic(p)) { paths = path1^^path2; }
    else { paths = path1..reverse(path2)..cycle; }

    return paths;

pen origPen = 10+black;

draw(getBorder((0,0){E}..{S}(2,0), origPen), red);
draw(getBorder((0,1)--(1,0), origPen), blue);
draw(getBorder(shift(1,0.5)*scale(0.5)*unitcircle, origPen), green);

enter image description here

The code works for cyclic or non-cyclic paths. The origPen definition determines the resulting width of the path outline. The function assumes that the origPen is defined with a roundcap.

Unfortunately, the nature of the function means that sharp corners or small radii will not be treated properly. Maybe someone else knows how to overcome this problem.

enter image description here


You can over-draw it using a pen with the background colour and a slightly narrower width.

Here's an example: (compile with asy or integrate into your existing LaTeX doc).

guide sausage = (0,0) -- (2,3);
draw(sausage, black+5);
draw(sausage, white+4.2);

this produces:

enter image description here

The draw(*guide*, *pen*) command draws the guide with the specified pen. To set the width of your pen, you just add a real number to an existing pen. In this case I've used the predefined (coloured) pens black and white. Adding 5 to black makes a pen that's 5 PostScript points wide. If you wanted one that's 5mm wide then you could do black+5mm thanks to the clever way that Asymptote deals with units.

  • That almost works, but the problem is I need to make these overlap each other, which doesn't work with this approach. – AMACB Jun 6 '16 at 1:47
  • OK, perhaps you can update the question to say that? – Thruston Jun 6 '16 at 6:59
  • Getting the outline of a stroked path is hard -- I don't think it's implemented in Asymptote (but I'm not an expert). You would certainly find it easier to define oval paths and draw them directly. Could you try that approach? – Thruston Jun 6 '16 at 8:28

I think you should change the size of defaultpen to something less that what you are using, i.e., less than 0.5cm.

I hope this helps!

  • I'm trying to draw the region within a certain length of the line, not what you are doing. – AMACB Jun 5 '16 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.