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]\"");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");
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.
xcolor
package to asymptote. Withinline
option and the LaTeXtextcolor
command everything is ok since it is a LaTeX command. But when you specifyPurple
orMycolor
asymptote does understand, the color is not defined in an asy way (seepen
definition). For color definition (purple
is defined for example) please consult asymptote.sourceforge.net/doc/Pens.html#Pens .star.asy
to all the asy environments in your TeX file, include the commandimport star;
orinclude 'star.asy';
in yourasydef
environment.asy
file, you can use the\asyinclude
command to include the picture (and hopefully letlatexmk
and other tools know about the dependency). But the only way to simultaneously affect all Asymptote code in your TeX file is using theasydef
environment.