I'm quite new to Asymptote, although it's great to manipulate vector graphics within LaTeX. Specifically, I'm using XeLaTeX.

What I'm trying to do is to change the color of a vector graphic to whatever one I defined with a \definecolor command (using the xcolor package), and others previously defined with the dvipsnames option. Also, I'm trying not to put the Asymptote code in the LaTeX file I'm working on, as it's quite big, and to have the opportunity to reuse it in future documents. Unfortunately, I tried with no luck.

I partially got Asymptote working with colors like red and yellow (the standard ones for xcolor without options), and with the code attached to the .tex file, as follows:


\geometry{paperwidth=4in, paperheight=3in,margin=2cm}




  settings.outformat = "pdf";
  import pstoedit;

  void star(pen color=black) {
    currentpen += 0.5bp;
    currentpen += squarecap;
    currentpen += miterjoin;
    currentpen += linetype(" ",false);

  \textcolor{myColor}{\textbf{A text with the custom color!}}\\[1cm]

And here's the latexmkrc file (to use latexmk to compile):

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

It's worth mentioning comments in Asymptote are //.

Compiling the .tex file as it's here, the output is shown in this image (the text color is the custom one):

Output of compiled MWE (as it is)

When I try to compile with latexmk -xelatex star.tex using a dvipsnames or custom color for the second star (i.e. leaving only one of the three provided lines uncommented), I get the following error:

star-2.asy: 29.12: no matching variable 'myColor' <-- Custom color
star-2.asy: 28.12: no matching variable 'Purple'  <-- dvipsnames color

In fact, I even get the same error when I try to put the contents of \begin{asydef}...\end{asydef} in an external file, say code.asy, and link it with the instruction \asyinclude{code.asy}. That's why you see the code in star.tex.

Is there a way to get rid of these errors to accomplish the task I'm trying to do? Any help/clues will be appreciated.

  • 1
    Hello, I am not sure if it is possible to give an color from the xcolor package to asymptote. With inline option and the LaTeX textcolor command everything is ok since it is a LaTeX command. But when you specify Purple or Mycolor asymptote does understand, the color is not defined in an asy way (see pen definition). For color definition (purple is defined for example) please consult asymptote.sourceforge.net/doc/Pens.html#Pens .
    – O.G.
    Commented May 7, 2016 at 15:57
  • 1
    To link star.asy to all the asy environments in your TeX file, include the command import star; or include 'star.asy'; in your asydef environment. Commented May 7, 2016 at 23:07
  • 1
    If you want to put the code for a single picture in an asy file, you can use the \asyinclude command to include the picture (and hopefully let latexmk and other tools know about the dependency). But the only way to simultaneously affect all Asymptote code in your TeX file is using the asydef environment. Commented May 7, 2016 at 23:13

1 Answer 1


You can't do this by defining the color in TeX code. However, there is a way to get the custom color you want. Using an html color converter and inputting #B28B3C for your custom color, it appears that the cmyk code for the color is cmyk(0%, 22%, 66%, 30%). Similarly, if you ask for Purple from the color converter, you get the cmyk code cmyk(0%, 100%, 0%, 50%). You can use these colors in Asymptote by adding the following lines to your asydef environment:

pen myColor = cmyk(0, 0.22, 0.66, 0.30);
pen Purple = cmyk(0, 1.00, 0, 0.50);

This is probably the simplest solution you're going to get.

If you really, really want TeX to do all the work for you, you might try the following:

  1. Switch from the asymptote package to the asypictureB package. (The TeX commands are different, but the actual Asymptote code is identical.) This allows you to use purely expandable TeX macros directly in the Asymptote code by prefixing them with @ instead of \. It has other advantages and disadvantages compared to the asymptote package; these are detailed in the asypictureB documentation.
  2. Having completed step 1, patch into the xcolor package and ensure that every time a color is defined (including those colors pre-defined by the package), a purely expandable macro is defined that can be parsed by Asymptote. For instance, after \definecolor{myColor}{HTML}{B28B3C}, there should exist a command \myColorAsy or similar that expands to cmyk(0, 0.22, 0.66, 0.30) or to rgb(178/255, 139/255, 60/255). Then, when you want to use this color in your Asymptote code, use @myColorAsy. Note that this will recreate the color every time you use it, so technically the Asymptote code will run more slowly; but for most purposes the difference will be imperceptible.

Personally, I think step 2 is way more work than it's worth. But if some enterprising soul wants to give a pure (La)TeX solution explaining how to hook into the xcolor package and achieve this, I'll certainly upvote it.

  • Ok, now I understand how to do it. Well, and what about calling the function from another file? As I stated in the question I get the same error for that, and I don't know why =(
    – user101590
    Commented May 7, 2016 at 22:59
  • 1
    See my comment to your question. Commented May 7, 2016 at 23:08
  • The two proposed methods are interesting. Thanks for your effort!
    – user101590
    Commented May 7, 2016 at 23:12

You must log in to answer this question.