Input

\documentclass[
tikz,
border=2mm,
convert={
true,
%false,
density = 300,
convertexe = {convert},
%command={\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -quality 90 \outfile}
}
]
{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[]
[
red,
]
{
x^2
};
\end{axis}
\end{tikzpicture}

\end{document}


Problem

I want to use the convert option to generate PNG files on the fly. As you can see in the code example above: It works basically!

The problem is that the resulting PNG has transparency (transparent background and not a white background):

In my case this is not wanted. Normally if you use the convert command from the console or in a batch file (I use Windows 7/10) then you just add -alpha remove and the problem is solved.

However I do not know how to add the option within the standalone package. In the documentation of the standalone package it says that the default command that is passed to the convert command is:

command={\convertexe\space -density
\density\space \infile\space \ifx\size\empty\else
-resize \size\fi\space -quality 90 \outfile}


So I entered this in the class option and expected no change. In a second step I would have added the -alpha remove option.

But with this document class options:

\documentclass[
tikz,
border=2mm,
convert={
true,
%false,
density = 300,
convertexe = {convert},
command={\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -quality 90 \outfile}
}
]
{standalone}


Then I get this error:

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (MiKTeX 2.9) (preloaded format=pdflatex 2015.12.20)  29 MAR 2016 22:38
entering extended mode
**\expandafter\def\csname sa@internal@run\endcsname{1}\input{lala}
(lala.tex
! Undefined control sequence.
<argument> ...e = {convert}, command={\convertexe
\space -density \density \...
l.13

?
! Emergency stop.
<argument> ...e = {convert}, command={\convertexe
\space -density \density \...
l.13

End of file on the terminal!

1. Can you reproduce the problem?
2. Do I misread the documentation?
3. Maybe you know a solution!
• @cfr Thanks for the quick reply. I do not understand what you mean, sadly (I will be able to reply again tomorrow evening, maybe earlier but that's not sure). – Dr. Manuel Kuehner Mar 29 '16 at 21:06
• @cfr Do you mean that I should use the convert tool without using the standalone interface? That would mean that there is a bug in the standalone package (assuming I did not made a mistake)? – Dr. Manuel Kuehner Mar 29 '16 at 22:40
• OK. I reiterate my initial comment, despite deleting it earlier. I think you can do not as a class option. But you need to do it before, I think. – cfr Mar 29 '16 at 23:17
• The topic transparency appeared a couple of times in the last month. I should add an option for it. – Martin Scharrer Mar 30 '16 at 7:25

The error you get about an undefined \convertexe (and then for all other commands in the command argument, if you skip the error message) is because you are using them as a documentclass option. The \documentclass expands its arguments before the actual class is loaded and on this stage the macros are not defined, nor should they be expanded at this point anyway.

This is a general issue and standalone can't do anything about that. You can fix this by either wrap the command in an \unexpanded (requires an e-TeX enabled distribution) or by defining the content to a macro first which is used after a \noexpand.

\documentclass[
tikz,
border=2mm,
convert={
true,
%false,
density = 300,
convertexe = {convert},
command=\unexpanded{{\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -quality 90 -alpha off \outfile}}
}
]
{standalone}


or

\def\CONVERTCOMMAND{\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -quality 90 -alpha off \outfile}
\documentclass[
tikz,
border=2mm,
convert={
true,
%false,
density = 300,
convertexe = {convert},
command = {\noexpand\CONVERTCOMMAND},
}
]
{standalone}


I tested this successfull with the -alpha off option mentioned by cfr.

• Thanks Martin. Did I do it - according to the documentation - wrong? I understood that it is supposed to be an document class option? Maybe I skipped some paragraphs. – Dr. Manuel Kuehner Mar 30 '16 at 7:25
• @Dr.ManuelKuehner: It's ok as an class option. However, they are always expanded first by \documentclass, so having macros there is the actual problem. I should mentioned that in the documentation. – Martin Scharrer Mar 30 '16 at 7:26
• @Dr.ManuelKuehner: I updated the documentation now. Will be part of the next release. – Martin Scharrer Mar 30 '16 at 7:47

The following works for me. However, if this is required, then the documentation could surely make this much clearer. An option to inherit from the default configuration would also make life a lot easier for users.

Class and package options are processed in various ways. Hence, when setting options in this way gives trouble, it is often worth checking if there's a way to set them in some other way.

Usually, this involves using a configuration command in the document itself - either in the preamble or, sometimes, in the body of the document. However, although trying this doesn't throw an error, it doesn't do much good either in this case because, as I understand it, conversion takes place at \documentclass so anything later won't influence the process.

However, standalone offers the possibility of using a configuration file, standalone.cfg. It provides a default but this default can be overridden by dropping a custom version somewhere in the search path with higher priority. (E.g. in TEXMFHOME or in the working directory.)

Here's mine:

% standalone.cfg
\standaloneconfig{%
crop,
convert={%
density=600,
command={\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -quality 90 -alpha off \outfile},
},
}
\endinput


Note that convert does not list -alpha remove as an option on my system, so I used -alpha off instead.

Note also that crop is needed here because we are overriding the default standalone.cfg which is responsible for ensuring this default setting.

I tested with a minimal example - pgfplots is really not relevant to the question, as far as I could tell:

\documentclass[tikz,convert,border=10pt]{standalone}
\begin{document}
\tikz{\node {abc};}
\end{document}


Then

file <filename>.png


returns

<filename>.png: PNG image data, 348 x 279, 1-bit grayscale, non-interlaced


It is grey scale just because the input is black and white, of course. The point is that there is no alpha.

• Thanks cfr - great solution. This is beyond my skills. And yes, the (pgf)plot was not necessary in a minimal working example. – Dr. Manuel Kuehner Mar 30 '16 at 7:16
• Regarding the -alpha remove. I use it in my batch file and it works. I got it from here: stackoverflow.com/questions/2322750 But thanks for the hint. Maybe I use an outdated option. – Dr. Manuel Kuehner Mar 30 '16 at 7:19
• Note that while \standaloneconfig can also be used in the preamble, this doesn't work for convert because it's required at class load time, so using the standalone.cfg file is indeed required. I would place it in the working path if it is not required for all files you do. – Martin Scharrer Mar 30 '16 at 7:23
• I accepted Martins anwer since he is the maintainer of the package. I hope that's ok for you. – Dr. Manuel Kuehner Mar 30 '16 at 7:50
• @MartinScharrer Thanks. I didn't actually mean the command wouldn't work but more specifically that changing the convert configuration after \documentclass wouldn't have any effect. Is there something I could make clearer, do you think? – cfr Mar 30 '16 at 23:13