TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been searching around but I can't find any info for adding an image background for my code listing using the listings package. Is there a way to do this?

share|improve this question
There is (always? ;-)) a way via TikZ... – Paul Gaborit Jul 17 '12 at 7:01
Follow up Question: Background images for code listings in beamer. – Peter Grill Jul 21 '12 at 18:33
up vote 12 down vote accepted

As PolGab says, there is always a tikz way. Here is one that uses the infamous \tikzmark to mark the begin and end of the listing, and then adds an image clipped to the size of the listing:

enter image description here



This however has some serious issues:

  • A blank line is required before the listing (surprising result otherwise).

  • Not sure why the fudge facts were required, but they end up aligning the frame to pretty close to where the fram=tb option would have drawn the horizontal lines.



\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

    \begin{tikzpicture}[overlay,remember picture]
    \coordinate (VCenter) at ($(#2)!0.5!(#3)$);
    \coordinate (Fudge) at (-\pgflinewidth,0);
    \coordinate (VFudge) at (0,\baselineskip);
    %\draw [red, thick,fill=yellow, fill opacity=0.2]% for debugging
            ($(#2)+ (Fudge) - 0.50*(VFudge)$) -- 
            ($(#2) + (\linewidth,0) - 0.50*(VFudge)$) -- 
            ($(#3) + (\linewidth,0) + 1.25*(VFudge)$) --
            ($(#3)+ (Fudge) + 1.25*(VFudge)$) -- cycle;
    \path (VCenter) -- ++($(0.5*\linewidth,0)$) 
    node [opacity=0.3, #1] {\includegraphics[width=\linewidth]{#4}};


\lstset{emph={%  Adjust any special keywords

Here is an example of listings with a background image:

\noindent% Line above MUST be blank
/* Hello World program */


struct cpu_info {
    long unsigned utime, ntime, stime, itime;
    long unsigned iowtime, irqtime, sirqtime;

    printf("Hello World");
Some text afterwards.
share|improve this answer
Very beautiful background... But the original question is "It there a way to do this" and not "How to do this". ;-) – Paul Gaborit Jul 17 '12 at 11:49
@PolGab -- Come on. Give a guy a break! :-) – Eric Jul 19 '12 at 3:47
Tried using this technique with the beamer package. Did not work. I just get a grey box around the listing. – Eric Jul 21 '12 at 16:49
If the codes are long enough to span multiple pages, then what happen? :-) – kiss my armpit Jul 21 '12 at 17:02
@HiggsBoson: Yep, this solution won't work in that case. – Peter Grill Jul 21 '12 at 18:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.