[Tex/LaTex] How to create beamer slides with source code that can be copied

beamerlistingssourcecode

We are converting our course slides from Powerpoint to LaTeX, to be able to offer all code in a more open format and to let students easily copy code from the slides. However, the latter does not seem to work: LaTeX or listings or beamer transform all code into more beautiful characters, which can not be copied anymore. Consider this LaTeX code:

\documentclass{beamer}

\usepackage{listings}

\begin{document}

\lstset{breakatwhitespace=true,
language=C++,
columns=fullflexible,
keepspaces=true,
breaklines=true,
tabsize=3, 
showstringspaces=false,
extendedchars=true}

\begin{frame}[fragile]
\begin{lstlisting}
const char *processing() const{
    somestring = "-1";
}
\end{lstlisting}
\end{frame}

\end{document}

Copying the code from a PDF opened in OSX Preview will result in a special asterisk, special quotes and even a different minus sign. How can this be avoided, how can we make sure all code can be copied immediately as-is?

Or is the PDF viewer to blame? Then which format should we render out LaTeX to, to make sure the code can be copied: HTML?

Best Answer

use the \defverbatim[...]\command definition:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{cmap}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{breakatwhitespace,
language=C++,
columns=fullflexible,
keepspaces,
breaklines,
tabsize=3, 
showstringspaces=false,
extendedchars=true}

\begin{document}
\defverbatim[colored]\lst{%
\begin{lstlisting}[tabsize=8,basicstyle=\ttfamily]
const char *processing() const{
    somestring = "-1";
}
\end{lstlisting}
}

\begin{frame}
\lst
\end{frame}

\end{document}

it is a problem with beamer which ignores the tabs. However, the above works