Quantcast
Viewing latest article 24
Browse Latest Browse All 70

Fixed width listings columns without extra spaces after copy from pdf

I would like the code listings from my Beamer slides to have the same spacing after having been copied from the pdf, and pasted into a text editor. I have been working with the columns option of the lstset macro from the listings package.

If I use columns=fixed, the code listings in the pdf have the alignment I want, just as they appear in the latex. Alas, when the text of the code listing is copied (from e.g. Evince or Acrobat Reader) and pasted into a text editor, the result is malformed, or additional spaces are inserted; often around punctuation marks.

If I use columns=flexible or columns=fullflexible, the text appears much better in the text editor (apart from absence of space indentation at the start of lines), but now the code listing in the pdf does not have the alignment that I have prepared in the latex file.

For example:

\documentclass{beamer}\usepackage{listings}\lstset{  language=C++,  basicstyle=\footnotesize\ttfamily,  upquote=true,  columns=fixed,%  columns=flexible,%  columns=fullflexible,  frame=single,}\begin{document}\begin{frame}[fragile]{C++ Example}\begin{lstlisting}void oload(int i)    { std::cout << i << std::endl; }void oload(float f)  { std::cout << f << std::endl; }void oload(double d) { std::cout << d << std::endl; }int main(int argc, char *argv[]){  int    i = 9;  float  f = 3.14f;  double d = 3.14;  oload(i);  oload(f);  oload(d);  return 0;}\end{lstlisting}\end{frame}\end{document}

With columns=fixed the resulting text copied from the pdf generated through the latex above is garbled and will not compile:

void oload ( int i ){ std :: cout << i << std :: endl ; }void oload ( float f ) { std :: cout << f << std :: endl ; }void oload ( double d ) { std :: cout << d << std :: endl ; }int main ( int{inti =float f =double d =oload ( i );oload ( f );oload ( d );return 0;}argc , char * argv [])9;3.14 f ;3.14;

Meanwhile, with columns=fullflexible, the text from the pdf is passable, and will compile, but the code listing in the pdf does not maintain the original use of multiple spaces; for example, the braces and statements within the 3 original oload function definitions do not align:

void oload(int i) { std::cout << i << std::endl; }void oload(float f) { std::cout << f << std::endl; }void oload(double d) { std::cout << d << std::endl; }int main(int argc, char *argv[]){int i = 9;float f = 3.14f;double d = 3.14;oload(i);oload(f);oload(d);return 0;}

Is there any way to have a fixed width listings for columns, without the extra spaces/malformation after copying from the pdf?


Viewing latest article 24
Browse Latest Browse All 70

Trending Articles