38

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?

3
  • Just a side note: in my experience, transparency does not render well in many PDF-readers, especially when printing.
    – fheub
    Feb 24, 2012 at 7: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, 2012 at 8: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 tex.stackexchange.com/questions/40050/… for a discussion on the technical details.
    – Daniel
    Feb 24, 2012 at 15:00

3 Answers 3

17

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

8
  • 1
    But I can't see any difference in the example given in the documentation of the transparent package.
    – Keks Dose
    Feb 24, 2012 at 9:55
  • @KeksDose I added a mwe.
    – StrongBad
    Feb 24, 2012 at 13:04
  • 1
    Using xetex or luatex gives Package transparent Warning: Loading aborted, because pdfTeX is not running in PDF mode.
    – Emre
    Feb 24, 2012 at 23:59
  • @Emre I misread your question. I thought you were trying to do it with latex, but were willing to consider xetex or lautex.
    – StrongBad
    Feb 26, 2012 at 10:16
  • 3
    This applies transparency both to the box and text. Not very useful that way. Nov 5, 2017 at 17:38
11

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}
1
  • 1
    You are right; the opacity "leaks", so this is not the complete solution.
    – Emre
    Feb 24, 2012 at 23:58
4

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

2
  • 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 7, 2012 at 20:58
  • 1
    @percusse I edited my answer to make it more clear. Thank you for th additional information.
    – JJD
    Sep 7, 2012 at 22:29

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.