15

How do I create a pdf file with a transparent background?

\documentclass{article}
\usepackage{xcolor}

\begin{document}
    \nopagecolor 
    hi
\end{document}

This doesn't work since \nopagecolor resets the page color to the default background, but how do I make it transparent, as one can with e.g. a png image?

JPi
  • 13,595
  • 1
    I was under the impression that a pdf would default to transperant. You don't need any special code for it. Open the pdf in an image-editor, and you will see that it is transperant. – Runar Aug 07 '16 at 20:17
  • Hmm. In gimp it opens with a white background. – JPi Aug 07 '16 at 20:20
  • 2
    I tried opening a transperant pdf with gimp and you are right, it turns into a white background. Might be a bug in Gimp. – Runar Aug 07 '16 at 20:36
  • 2
    You can use Image Magic, it will display a transparent background with \nopagecolor or if background is not explicitly set (which is transparent by default). – alwaysask Aug 07 '16 at 20:58
  • Image Magic is not supported in Windows 7 32-Bit...any help for those whoe have windows 7 32-Bit – Soham Chatterjee Sep 03 '20 at 18:14

2 Answers2

14

\nopagecolor of pdftex.def (and LuaTeX) disables the page color, it does not use any other color like white. Compare and look closely at the cropped images:

\documentclass{article}
\usepackage{xcolor}

\begin{document}
  % Page 1
  \nopagecolor
  \verb|\nopagecolor|
  \newpage

  % Page 2
  \pagecolor{white}
  \verb|\pagecolor{white}|
\end{document}

Page 1 with \nopagecolor

Page 2 with \pagecolor{white}

The second page has a white background.

The first page does not have a background, which can also be verified by the page stream operators for the first page with some added comments:

stream
0 g 0 G  % set black as fill and stroke color
0 g 0 G  % set black as fill and stroke color
0 g 0 G  % set black as fill and stroke color
BT  % begin text
/F15 9.9626 Tf 148.712 657.235 Td [(\134nopagecolor)]TJ  % Write "\nopagecolor"
0 g 0 G  % set black as fill and stroke color
0 g 0 G  % set black as fill and stroke color
ET  % end text
endstream

The white background of GIMP adds the white background. I have not seen an option to disable it (version 2.8). If the white color is only used for the background, then option "Colors → Color to Alpha" can convert white back to the alpha transparency channel.

Heiko Oberdiek
  • 271,626
0

I know this is an old post but this might help someone using Overleaf. Create a file called latexmkrc. Populate it with the following:

END { system('convert -density 600 -background transparent -flatten output.pdf pages.png'); }

Compile this file. In the logs, scroll down and select other logs and files. Your transparent file is "pages.png".