I am trying to make a presentation which can optionally compiled for handouts. This presentation also contains highlighted code snippets. For highlighting I use the code from this question. (Which works fine in my real presentation, however produces highlights too far to the right in the minimal examples.)
When I compile for handout mode, the last of the highlighted lines is still present in the listing. Therefore I am looking for some way to conditionally disable the highlighting.
What I have so far:
The conditional:
\newif\ifhandout
\handouttrue
\ifhandout
\documentclass[handout]{beamer}
\else
\documentclass{beamer}
\fi
The highlighting:
\begin{frame}[fragile]{ListingTest}
\begin{lstlisting}[linebackgroundcolor={
\btLstHL<1>{1-3}
\btLstHL<2>{6,9}
}]
... some code thats at least 9 lines long
\end{lstlisting}
\end{frame}
The problem now is, that I don't know how I achieve optional compilation of the optional linebackground argument. An additional problem is that I have to escape commands inside the listing environmt. I have tried placing ifs at various locations like:
\ifhandout
\begin{lstlisting}
(*@\else
\begin{lstlisting}[linebackgroundcolor={
\btLstHL<1>{1-3}
\btLstHL<2>{6,9}
}](*@\fi@*)
... code
\end{lstlisting}
But nothing worked as expected. (I even managed to place them in a way that collapsed all code in the listing in a single line.)
Does someone know, how I can exclude the argument in [] from compilation when handout ist true?
Here is the complete "minimal" example for testing. Thank you very much in advance.
\newif\ifhandout
\handouttrue
\ifhandout
\documentclass[handout]{beamer}
\else
\documentclass{beamer}
\fi
\usepackage{lmodern}
\usepackage{pgf, pgffor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\lstset
{
inputencoding=utf8,
language=c++,
basicstyle=\tiny\ttfamily,
numberstyle=\tiny,
numbers=left,
stepnumber=1,
numbersep=5pt,
escapeinside={(*@}{@*)},
showstringspaces=false,
backgroundcolor=\color{white},
frame=single,
tabsize=4,
morekeywords={decltype},
literate={ö}{{\"o}}1
{ä}{{\"a}}1
{ü}{{\"u}}1
{Ä}{{\"A}}1
{Ö}{{\"O}}1
{Ü}{{\"U}}1
{ß}{{\ss}}1
{~}{{\textasciitilde}}1
}
\lstloadlanguages{C++}
% Some Code copied from stackoverflow for highlighting
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \btIfInRange{number}{range list}{TRUE}{FALSE}
%
% Test in int number <number> is element of a (comma separated) list of ranges
% (such as: {1,3-5,7,10-12,14}) and processes <TRUE> or <FALSE> respectively
\newcount\bt@rangea
\newcount\bt@rangeb
\newcommand\btIfInRange[2]{%
\global\let\bt@inrange\@secondoftwo%
\edef\bt@rangelist{#2}%
\foreach \range in \bt@rangelist {%
\afterassignment\bt@getrangeb%
\bt@rangea=0\range\relax%
\pgfmathtruncatemacro\result{ ( #1 >= \bt@rangea) && (#1 <= \bt@rangeb) }%
\ifnum\result=1\relax%
\breakforeach%
\global\let\bt@inrange\@firstoftwo%
\fi%
}%
\bt@inrange%
}
\newcommand\bt@getrangeb{%
\@ifnextchar\relax%
{\bt@rangeb=\bt@rangea}%
{\@getrangeb}%
}
\def\@getrangeb-#1\relax{%
\ifx\relax#1\relax%
\bt@rangeb=100000% \maxdimen is too large for pgfmath
\else%
\bt@rangeb=#1\relax%
\fi%
}
\newcommand<>{\btLstHL}[1]{%
\only#2{\btIfInRange{\value{lstnumber}}{#1}{\color{blue!30}\def\lst@linebgrdcmd{\color@block}}{\def\lst@linebgrdcmd####1####2####3{}}}%
}%
\makeatother
\begin{document}
\begin{frame}[fragile]{ListingTest}
\begin{lstlisting}[linebackgroundcolor={
\btLstHL<1>{1-3}
\btLstHL<2>{6,9}
\btLstHL<3>{7}
\btLstHL<4>{8}
}]
/**
* Prints Hello World.
**/
#include <stdio.h>
int main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}
\end{frame}
\end{document}
\mode<handout>...to select the relevant content for the presentation and the handout separately? – Sep 17 '15 at 14:16\begin{lstlisting}[...]in amodebut it didn't work. I would not really like to duplicate everything betweenbeginandend. – Devon Cornwall Sep 17 '15 at 19:40