# How to copy/paste multiple spaces from lstlistings?

This is a continuation of a question that I asked already: How to copy/paste from lstlistings?.

The solution presented there does not work if the text within the lstlistings environment contains consecutive spaces. Consecutive spaces get copied as a single space. See the following example:

\documentclass[letterpaper]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[lmargin=2.5cm,rmargin=2.5cm,tmargin=1.5cm,bmargin=3.5cm]{geometry}
\usepackage{listings}  % Environment meant for source code (useful for word-wrapping verbatim text)
\lstset{language=[LaTeX]TeX,breaklines=true} % Word wrap within listings environment

\begin{document}
\lstset{basicstyle = \verbatim@font}

\begin{lstlisting}
There are no spaces in here []
There is one spaces in here [ ]
There are two spaces in here [  ]
\end{lstlisting}

\end{document}


The the contents get copied/pasted such that the third line only contains one space, when it should contain two. Does anyone know how to fix this?

There are no spaces in here []
There is one spaces in here [ ]
There are two spaces in here [ ]


Thank you

Unfortunately, space characters cannot reliably be copied from a PDF. This answer on Superuser is bang on:

The problem with PDF is that it doesn't really encode text. All it does is pretty much saying "Put those glyphs there, and those glyphs there". It's a format intended for print and high-fidelity document preview on screen but not so much to actually retain any semantics or contents.

So the only thing a PDF reader can see is what letters are approximately on a line. It can't see spaces as a space character, since there is no such thing in a PDF. All it has is smaller and larger gaps between letters. And thanks to kerning or justified text those aren't even consistent.

So what PDF readers usually do is to guess what gaps are spaces and what gaps are not. Depending on the algorithm used the results are fairly good or horrible.

Besides, here are three other reasons why you should think twice before encouraging your readers to copy & paste code typeset in a PDF (e.g. with listings).

1. copying listings that span multiple pages of a PDF document is tedious and error-prone;
2. the results of copying content from a PDF for subsequent pasting vary widely from one PDF viewer to another (and you don't have control over which viewer(s) your readers use);
3. line breaks introduced by listings's breaklines, if copied and pasted "verbatim", may translate to invalid syntax in the language used.

It simply is not a reliable way of distributing code.

• There is however a way to tell PDF readers what text to copy. I once saw a PDF which displayed one text and, when copied, copied a totally different text. So, how can we use this with LaTeχ, copying precisely the listing? – mirabilos Aug 6 '15 at 12:23
• @mirabilos I don't know whether that's possible. – jub0bs Aug 6 '15 at 12:39
• @mirabilos, @jubobs: This question mention accsupp, which seems to provide this functionality. – Martin J.H. Sep 9 '15 at 19:33
• @MartinJ.H. accsup doesn't completely solve the problem. – jub0bs Sep 9 '15 at 19:34