39

What options are there to specify transparent colors, say the background in the example below? I thought xcolor would be able to do this but I can not find any reference to "alpha" or "transparency" in the manual.

\documentclass{article}
\usepackage{listings,xcolor}

\lstset{frame=shadowbox, backgroundcolor=\color{green}}

\title{Transparency test}

\begin{document}
\maketitle
\begin{lstlisting}
Hello, world.
\end{lstlisting}
\end{document}

Is it possible to do it with XeLaTeX or LuaLaTeX?

Emre
  • 6,337
  • Just a side note: in my experience, transparency does not render well in many PDF-readers, especially when printing. – fheub Feb 24 '12 at 07:59
  • Pgf can do this, see section 84 (page 671 in my version) of the manual. I don't know if that is helpful in your case. – Psirus Feb 24 '12 at 08:26
  • 1
    @fheub: It's the printers to blame here, not the Readers. Transparency effects are a relatively new feature for PDF (≥ 1.4) and so far only few printers support it already. See http://tex.stackexchange.com/questions/40050/using-opacity-in-tikz-makes-the-entire-page-become-more-coarse-when-printed for a discussion on the technical details. – Daniel Feb 24 '12 at 15:00

3 Answers3

19

I think you might be looking for transparent.

\documentclass{article}
\usepackage{color}
\usepackage{transparent}
\begin{document}
  \colorbox{red}{%
    Black text in a red box %
    \transparent{1.0}%
    \colorbox{blue}{%
      and now a blue box is added%
    }
  }

  \colorbox{red}{%
    Black text in a red box %
    \transparent{0.5}%
    \colorbox{blue}{%
      and now a transparent blue box is added%
    }
  }
\end{document}

enter image description here

StrongBad
  • 20,495
12

You can get transparency with the pgf package and it will work with xetex/luatex too. But opacity settings get lost at a pagebreak and TEX-groups and boxes are not respected (as far as I can see one of the point of the transparent package is to confine transparency to boxes). That means that the opacity can "leak out" if you don't reset it explicitly at the correct places:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor,pgf}% http://ctan.org/pkg/{listings,xcolor,transparent}
\title{Transparency test}
\begin{document}
\maketitle
% Solid green
\lstset{frame=shadowbox, backgroundcolor=\color{green}}
\begin{lstlisting}
Hello, world.
\end{lstlisting}

% 50% transparent green
\lstset{frame=shadowbox, backgroundcolor={\color{green}\pgfsetfillopacity{0.5}}}
\begin{lstlisting}
Hello, world.
\end{lstlisting}


\lstset{frame=shadowbox, backgroundcolor=\color{green}}
\begin{lstlisting}
Hello, world.
\end{lstlisting}


\lstset{frame=shadowbox, backgroundcolor=\color{green}\pgfsetfillopacity{1}}
\begin{lstlisting}
Hello, world.
\end{lstlisting}

\end{document}
Ulrike Fischer
  • 327,261
6

I specified a pseudo transparent color through mixing it with white. You can easily brighten any color by choosing the amount of white it should be combined with.

\colorlet{LightSpringGreen}{White!70!SpringGreen}

The greater the number the brighter the color gets. There are much more options, as discussed in this question: Understanding xcolor color mixing model

JJD
  • 1,977
  • 4
  • 22
  • 36
  • 7
    But still it's an opaque color. In other words if you put it on some other text, it will block it completely. You have to change the opacity not the color tone. – percusse Sep 07 '12 at 20:58
  • 1
    @percusse I edited my answer to make it more clear. Thank you for th additional information. – JJD Sep 07 '12 at 22:29