6

Context: I'm trying to use Asymptote to draw explanatory arrows and comments atop a semitransparent lstlisting environment. In the course of doing so I've encountered a bizarre interaction between Asymptote and the package transparency that I've reduced to a minimal working example. I'm using LuaLaTeX but can reproduce in pdfLaTeX too.

For reasons not understood to me, an Asymptote asy environment will sometimes, though not always, almost completely disable transparency inside and outside and both fore and aft of the \begin{asy} ... \end{asy} block. Here's the MCVE example.

\documentclass{article}
\usepackage{asymptote}
\usepackage{transparent}

\begin{document}
\section{Before Asymptote}
{\transparent{0.1}Ghost X}
\section{In Asymptote}
\begin{asy}
if(false){                 //TOGGLE THIS
    label("x");
}else{
    draw((0cm,0)--(1cm,0));
}
\end{asy}
\section{After Asymptote}
{\transparent{0.1}Ghost X}
\end{document}

There is an if statement with a toggle inside, currently false. When it is set to false, I get ghostly text before and after the asy drawing:

Ghostly X

However, if I set the toggle to true, I don't:

Unghostly X

Note how both the text before and after is no longer transparent.

Variants I've tested:

  • The contents of the label are irrelevant.
  • Wrapping asy in a figure or minipage does not contain the disease.
  • Nor does adding \pdfpageattr{/Group <</S /Transparency /I true /CS /DeviceRGB>>} in the preamble or any other incantations as suggested here.
  • Both TeXStudio's embedded viewer and KDE's Okular show this, so it's not a peculiarity of the PDF viewer.
  • I've also just tried texpreamble("\usepackage{transparent}"); and label("{\transparent{0.1}x}"); and got the same results for both toggles, and $x$ was not transparent.
  • When you set the toggle to true and do both label("x"); and draw((0cm,0)--(1cm,0), opacity(0.1)); in the if-side of the branch, you get the same result (transparency disabled fore and aft), but the drawn line is transparent.

Why do only some Asymptote figures trigger spooky-action-at-a-distance and blow up everything else like this? And how can I work around it?

  • Does putting a group around the asy block help at all? – cfr Jan 24 '16 at 23:22
  • @cfr Not at all. That was the thing I tried before figure and minipage, If wrapping with minipage doesn't sandbox it, it's basically impossible for {} to sandbox it. – Iwillnotexist Idonotexist Jan 24 '16 at 23:36
3

I still don't know why it's happening, but here's a workaround for the MCVE:

  1. Use asypictureB package instead of asymptote. (Correspondingly, change all asy environments to asypicture, both \begin and \end, and make sure to add the mandatory argument after \begin{asypicture}.
  2. Add the line settings.outformat="pdf"; to the beginning of your asypictures. Otherwise it will get compiled to eps, which can cause peculiar spacing.

Here's the code:

\documentclass{article}
\usepackage{asypictureB}
\usepackage{transparent}

\begin{document}
\section{Before Asymptote}
{\transparent{0.1}Ghost X}
\section{In Asymptote}
\begin{asypicture}{name=foo}
settings.outformat = "pdf";
if(true){                 //TOGGLE THIS
    label("x");
}else{
    draw((0cm,0)--(1cm,0));
}
\end{asypicture}
\section{After Asymptote}
{\transparent{0.1}Ghost X}
\end{document}

and the result:

enter image description here

The only clue I have as to the reason is that the asymptote package has tighter interaction with the LaTeX code than the asypictureB package. This allows the asymptote package to automatically copy the fonts and packages used in the main document, but apparently it also allows for issues like this.

  • Thanks for your answer! I just came back here to report that adding settings.inlineimage=false; fixes the problem outside asy, albeight texpreamble("\usepackage{asymptote}"); and label("{\transparent{0.1}x}"); still do not render a ghostly x within asy. – Iwillnotexist Idonotexist Jan 25 '16 at 1:07
  • 1
    In general, the TeX commands to change color, transparency, etc. are not expected to function correctly inside Asymptote labels. Instead, modify the pen used for the label. – Charles Staats Jan 25 '16 at 1:11
  • 2
    Although now that I do some experimentation, I'm not sure that a pen's opacity actually affects labels. The color certainly does. – Charles Staats Jan 25 '16 at 1:20
  • 1
    Charles, I found out that it is possible to get transparent and colored labels by way of \transparent{0.2}\color{green} or suchlike - but one must invoke asy manually with argument -twice, or else use settings.twice=true;. Not because one needs to resolve references, but because when you run asy -vv you're treated with this: Package transparent Warning: Rerun to get transparencies right.. Running asy twice doesn't work - it seems like it deletes whatever transparent collected during its first run. I will be marking your answer and package as the solution in due time. Cheers! – Iwillnotexist Idonotexist Jan 25 '16 at 8:54

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.