[Tex/LaTex] Generating a transparent image from LaTeX

conversionpngtransparency

I have a T-shirt I designed in LaTeX, and now I need to convert the LaTeX to a white-foreground transparent-background image. How can I do that easily?

Best Answer

You have multiple options to create a Scalable Vector Graphics (SVG) image, or a raster Portable Network Graphics (PNG) file from the compiled PDF.

For creating an SVG from the PDF file I'd recommend using Inkcscape since it is a multiplatform application which runs on Linux, Windows and OS X. For creating a raster PNG I'd recommend using Gimp, which is also multiplatform. You can import the PDF into these applications and either save them into SVG or PNG. In case you have drawn everything in your design with black, then you'll have to invert the colors of the imported PDF in Gimp, or change the fill and stroke colors to white in Inkscape.

There are several command-line tools what you can use to achieve the same thing. One of them is ImageMagick (also multiplatform) which you can use to create a PNG from your PDF directly, also inverting the colors in one single step:

convert -density 100 input.pdf -negate -format png output.png

You can play with the density command-line option to get a higher resolution PNG image.

This option specifies the image resolution to store while encoding a raster image or the canvas resolution while rendering (reading) vector formats such as Postscript, PDF, WMF, and SVG into a raster image. Image resolution provides the unit of measure to apply when rendering to an output device or raster image. The default unit of measure is in dots per inch (DPI). The -units option may be used to select dots per centimeter instead.

[…]

— ImageMagick: Command-line Options


Example

A circle drawn in LaTeX using TikZ:

\documentclass[10pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}

\usepackage[active, tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
\filldraw (0,0) circle [radius=3cm];
\end{tikzpicture}
\end{document}

Converting it to a raster white circle using ImageMagick:

convert -density 100 example.pdf -negate -format png output.png

Transparency was retained while the black color was inverted to white thanks to the -negate option.