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
Best Answer
Unfortunately, space characters cannot reliably be copied from a PDF. This answer on Superuser is bang on:
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
).listings
'sbreaklines
, if copied and pasted "verbatim", may translate to invalid syntax in the language used.It simply is not a reliable way of distributing code.