# How to create beamer slides with source code that can be copied?

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?

-
It copies correctly (except the tab before somestring) in Evince. Does it help to add \usepackage{microtype} to your preamble? –  N.N. Aug 29 '11 at 13:37
Unfortunately that does not help. –  Bart Braem Aug 29 '11 at 13:46
On a closer look I was wrong about copying from Evince. It also ends up with an asterisk and minus sign that is different from the ones you end up if you type then on your keyboard ('*' and '-'). –  N.N. Aug 29 '11 at 13:51

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

-
This avoids the problems with the quotes and the asterisk, great suggestion! However the minus is still transformed, and the tabs are still removed. I added source code for this minimal C-program to quickly test results: This avoids the problems with the quotes and the asterisk, great suggestion! However the minus is still transformed, and the tabs are still removed. I added source code for this minimal C-program to quickly test results: #include "stdio.h"  int main(char* arg){  printf("test\n");  return -1; } (Sorry for the comment update hickups) –  Bart Braem Aug 29 '11 at 14:19
a problem with beamer, see my edit –  Herbert Aug 29 '11 at 14:41
I already noticed that solution, I had hoped to avoid it but if it's required then I'll add it. Is there a way to control line breaks in the copied content? Sometimes lines break and sometimes they don't, the behaviour is hard to predict. –  Bart Braem Aug 29 '11 at 14:51
In the end this really is a good solution and I can live with the line break problems. Thanks for your help! –  Bart Braem Aug 31 '11 at 8:55