3

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}
2
  • 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. Commented Jul 21, 2012 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
    Commented Jul 21, 2012 at 17:43

1 Answer 1

4

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.

8
  • 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
    Commented Jul 21, 2012 at 18:53
  • @Eric when I wrote "processed twice" I meant running pdflatex twice; I usually do one run and then another one. Commented Jul 21, 2012 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
    Commented Jul 21, 2012 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
    Commented Jul 21, 2012 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? Commented Jul 21, 2012 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .