Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I asked this question originally in attempt to get background images behind my code listings.

Background images for code listings

What I did not mention is that I am using the beamer package for slides. This technique does not seem to work with beamer. I get a gray box behind the code listing.

How do I get background images behind code listings in beamer? `

\documentclass{beamer}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}

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

\newcommand*{\AddBackgroundImage}[4][]{%
%\pgfmathsetmacrp{\height}{}%
    \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
    \clip
            ($(#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}};
    \end{tikzpicture}%
}%

\usetheme {default}
\lstset{ language=Haskell 
       , basicstyle=\small\sffamily
       , numbers=left 
       , numberstyle=\tiny\color{gray}  
       }


\begin{document}     
\begin{frame}[fragile]  

\noindent% Line above MUST be blank
\tikzmark{Start}%
\begin{lstlisting}
data PersonRecord = 
  PersonRecord { firstname      :: FirstName,
                 lastname       :: LastName,
                 middleinitial  :: Maybe MiddleInitial,
                 gender         :: Gender,
                 favoritecolor  :: FavoriteColor,
                 dateofbirth    :: CalendarTime 
               } deriving (Show)

\end{lstlisting}
\end{frame}
\tikzmark{End}
\AddBackgroundImage{Start}{End}{images/black-screen}%
%
\noindent
Some text afterwards.
\end{document}
share|improve this question
1  
Please add to your question a MWE illustrating the problem. I ask you for this MWE because I did some tests with the solution in the question linked, and everything worked as expected. –  Gonzalo Medina Jul 21 '12 at 17:04
    
This is what I have thus far. I'm really not sure what I could be doing wrong. You mentioned something about 2 passes. Is there a special way I should be running the latex command? –  Eric Jul 21 '12 at 17:43
add comment

1 Answer

up vote 4 down vote accepted

Simply moving \tikzmark{End} and \AddBackgroundImage inside the frame environment, solves the problem (the document has to be processed twice):

\documentclass{beamer}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}

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

\newcommand*{\AddBackgroundImage}[4][]{%
%\pgfmathsetmacrp{\height}{}%
    \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
    \clip
            ($(#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}};
    \end{tikzpicture}%
}%

\usetheme {default}
\lstset{ language=Haskell 
       , basicstyle=\small\sffamily
       , numbers=left 
       , numberstyle=\tiny\color{gray}  
       }


\begin{document}     

\begin{frame}[fragile]  

\tikzmark{Start}%
\begin{lstlisting}
data PersonRecord = 
  PersonRecord { firstname      :: FirstName,
                 lastname       :: LastName,
                 middleinitial  :: Maybe MiddleInitial,
                 gender         :: Gender,
                 favoritecolor  :: FavoriteColor,
                 dateofbirth    :: CalendarTime 
               } deriving (Show)

\end{lstlisting}
\tikzmark{End}
\AddBackgroundImage{Start}{End}{ctanlion}%

Some text afterwards.
\end{frame}

\end{document}

enter image description here

CTAN lion drawing by Duane Bibby.

share|improve this answer
    
I've been reading man pages and google yet still I can find nothing about processing an option twice other than the -x option to latex or pdflatex which is for handling cross-references. Can you post the command you are using to process this document? –  Eric Jul 21 '12 at 18:53
    
@Eric when I wrote "processed twice" I meant running pdflatex twice; I usually do one run and then another one. –  Gonzalo Medina Jul 21 '12 at 19:01
    
The mistake above was do to my quick simplification. Even with the fix I still get only a grey box where the image should be. I tried two different .png files, I tried evince viewer and acroread. I taken all suggestion and done extra research. I'm guess I'm just out of luck. –  Eric Jul 21 '12 at 19:09
    
latex --version pdfTeX 3.1415926-2.3-1.40.12 (TeX Live 2012/dev/Arch Linux) kpathsea version 6.1.0dev –  Eric Jul 21 '12 at 19:10
    
@Eric Can you upload the actual image elsewhere (and provide a link here) so I can do some tests with the actual elements? –  Gonzalo Medina Jul 21 '12 at 19:11
show 3 more comments

Your Answer

 
discard

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.