1

I have many Asymptote files and they all need the LaTeX package siunitx. One way of doing this is to include the line usepackage("siunitx"); in every single file, but I would like to do this automatically for all asy files. I am trying to use a config.asy file in the same directory of those asy files. However, when I compiled with Asymptote (2.65), there is no error but there is also no png or other kind of output files produced.

My config.asy file:

import plain;
settings.autoplain=true;

settings.batchView=false;
settings.tex="pdflatex";
settings.outformat="png";
settings.render=4;
settings.embed=true;
settings.toolbar=false;
settings.command="usepackage('siunitx')";

My mwe.asy file:

size(50mm);
draw(Label("\SI{2}{\centi\meter}"), (0,0)--(2,2));

When compiled by asy mwe.asy, nothing is produced and also there is no error. What's wrong with my config.asy? or is there any other smarter way to automatically load some LaTeX packages for all the asy files?

EDIT My understanding is that config.asy is loaded by all asy files before doing anything else, that's why I am trying to use configuration file instead of something like import mytexpreamble.asy in every single asy file.

  • If I delete the line settings.command="usepackage('siunitx')"; in config.asy and add either usepackage("siunitx"); or texpreamble("\usepackage{siunitx}");, it will give me a correct png file after compiling; however if I put either usepackage("siunitx"); or texpreamble("\usepackage{siunitx}"); together with the line settings.command="usepackage('siunitx')"; in config.asy, it also produces nothing. – bingung May 7 at 22:39
  • Yes but then I would need to put usepackage("siunitx"); into every single asy file I have. I don't want to do this, I want to put a single line in the configuration file config.asy and every asy file will then load those settings. Leaving settings.command="usepackage('siunitx')"; out in config.asy will give me error if I don't include usepackage("siunitx"); in mwe.asy – bingung May 7 at 22:52
  • My understanding is that the config.asy is loaded by all asy files before doing anything else, that's why I am trying to use configuration file instead of something like import mytexpreamble.asy in every single asy file. – bingung May 7 at 23:01
  • @Schrödinger'scat no worry I will just clarify a bit in my question. – bingung May 7 at 23:11
  • config.asy is documented here. One possible explanation: perhaps the directory from which asy is being run is not the same as the directory containing all the asy files? One possible workaround: try putting config.asy in the (new) .asy directory under your home directory. – Charles Staats May 8 at 17:45
2

I'm sure, there is a better and proper way to achieve this, but a quick-and-dirty fix is this:

copy plain.asy to the directory of those asy files and insert the line

usepackage("siunitx");

at the end of it.

Actually, the code in the plain.asy suggests that if there is a command specified in settings.command, the Asymptote executes it and exits immediately after that.

You can instead use

settings.user="usepackage('siunitx')";

but in this case the command

usersetting();

must be present in the beginning of the asy file.

| improve this answer | |
  • Thanks! Didn't aware there is an exit(); at the end of command, so settings.user is the thing I should look for. While I see no harm in putting the usepackage("siunitx"); in plain.asy (at least in this case), it seems using usersetting(); with settings.user is a better practice. I guess it is designed for this purpose. – bingung May 8 at 23:48
3

It looks like there is a command texpreamble that might do what you need.

From https://sourceforge.net/p/asymptote/discussion/409349/thread/abc87704/ :

In the file config.asy you can set

autoimport="common";

and, in the file common.asy, you must put:

texpreamble("\newcommand{\U}{\underline}");

Regards.

| improve this answer | |
  • Thanks! This approach has the benefit of including also the user-defined functions into a single common.asy file, but will need to create an extra file common.asy. Whether settings.user with usersetting(); or autoimport is better seems depend on the user case. – bingung May 9 at 11:14

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.