[Tex/LaTex] 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:

\usepackage{listings}  % Environment meant for source code (useful for word-wrapping verbatim text)
\lstset{language=[LaTeX]TeX,breaklines=true} % Word wrap within listings environment

\lstset{basicstyle = \verbatim@font}

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


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

Best Answer

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.

Related Question