# Asymptote (XeLaTeX): Use custom-defined colors/Call drawing functions from external file

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:

\documentclass[12pt]{article}

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

\usepackage{parskip}

\usepackage[dvipsnames]{xcolor}
\definecolor{myColor}{HTML}{B28B3C}

\usepackage[inline]{asymptote}

\begin{asydef}
settings.outformat = "pdf";
import pstoedit;

void star(pen color=black) {
currentpen += 0.5bp;
currentpen += squarecap;
currentpen += miterjoin;
currentpen += linetype(" ",false);
fill((691.141,600.863)--(703.473,569.43)--(743.625,569.496)
--(711.098,550.133)--(723.578,518.742)--(691.141,538.207)
--(658.703,518.742)--(671.184,550.133)--(638.656,569.496)
--(678.805,569.43)--cycle,color+evenodd);
}
\end{asydef}

\begin{document}
\pagenumbering{gobble}
\textcolor{myColor}{\textbf{A text with the custom color!}}\\[1cm]
\begin{minipage}{2.5cm}
\begin{asy}[width=2.5cm]
star(color=blue);
\end{asy}
\end{minipage}
\hfill
\begin{minipage}{2.5cm}
\begin{asy}[width=2.5cm]
//star(color=Purple);
//star(color=myColor);
star(color=red);
\end{asy}
\end{minipage}
\end{document}


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):

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.

• 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. May 7 '16 at 15:57
• 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. – Charles Staats May 7 '16 at 23:07
• 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. – Charles Staats May 7 '16 at 23:13

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 May 7 '16 at 22:59
• See my comment to your question. – Charles Staats May 7 '16 at 23:08
• The two proposed methods are interesting. Thanks for your effort! – user101590 May 7 '16 at 23:12