# Phantom spaces in listings

I have a problem with SQL code in a PDF document. For code representation I'm using the listings package. Everything works perfect until I copy-paste that code from the created PDF document. I get some phantom spaces in string type. When people are reading my PDF document, they need to copy-paste some of the code. Visually the code looks good in PDF, but when I try to copy-paste my code to NotePad or any other editor I get some unexpected spaces in strings. In the example below, when I copy-paste my SQL code, instead of getting:

SELECT ST_Area(the_geom)
FROM katastar
WHERE kc_broj = '1414';


I get

SELECT ST_Area(the_geom)
FROM katastar
WHERE kc_broj = ' 1414 ';


See the spaces in between quotes.

\documentclass{article}
\usepackage{listings,xcolor}

\lstset{
tabsize=4,
basicstyle=\scriptsize,
%upquote=false,
aboveskip=\baselineskip,
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
showtabs=false,
showspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color[rgb]{0.627,0.126,0.941},
language=SQL
}

\begin{document}
\begin{lstlisting}
SELECT ST_Area(the_geom)
FROM katastar
WHERE kc_broj = '1414';
\end{lstlisting}
\end{document}

• Is this the only place you get spurious spaces? – Charles Stewart Nov 4 '10 at 9:59
• I even get many more of these annoying spaces when I compile your document and view it in acroread. I'll add a few lines to your code above, so that it compiles. OK? (Not sure if your document looks the same. By the way, which pdf viewer do you use?) – Hendrik Vogt Nov 4 '10 at 10:06
• For now this is only place that I notice. Problem is that this is quite important. Alternative is to use standard verbatim but then I wouldn't get color coding that listings provide. I'm reading pdf in Acrobat Reader. – Mario Miler Nov 4 '10 at 10:13
• I can confirm this. In fact, I gat many more extra spaces, the first line looks like this: SELECT S T _ A r e a ( t h e _ g e o m ). As far as pdf viewer goes, I tried zathura, xpdf, evince and acroread with the exact same result. Converting the document with pdftotext also produces the spaces. – Jan Hlavacek Nov 4 '10 at 12:22
• Interesting thing is that when I use latex to produce dvi, and cut and paste from that, I get spurious spaces only around the parentheses and the quotes. Converting that to pdf with dvipdfm inserts all the other spaces, the same I get when using pdflatex. – Jan Hlavacek Nov 4 '10 at 12:26

This behaviour is caused by \lstset{columns=fixed}. Changing to columns=flexible or columns=fullflexible should make it go away.

• OK, this works, but then the columns aren't fixed any more (surprise!), i.e., it doesn't appear monospace any more. Nevertheless, now we have an explanation where the spaces are coming from. – Hendrik Vogt Nov 4 '10 at 12:21
• He he -- so you want your cake and to eat it too! ;) – Geoffrey Jones Nov 4 '10 at 12:24
• I don't think it should be impossible in this case to have the cake and eat it too. First, listings package seems to create monospaced font by inserteng spaces between characters, but what if you used an actual monospaced font? i tried it with inconsolata, and with flexible columns, the characters seem to all have the same width, but spaces don't. Not sure why, and what is going on there. Second, in PDF, isn't it possible to specify an "alternate" text, that would be selected and pasted, different from what's displayed on the screen? – Jan Hlavacek Nov 4 '10 at 17:21
• @JanHlavacek: With basicstyle=\ttfamily,columns=flexible I get now correct spacing, including for spaces, which works for copy&paste. I guess listings fixes the incorrect width of spaces in the in the meantime. – Martin Scharrer Dec 23 '11 at 11:45
• @GeoffreyJones: could you modify your answer to reflect the final solution? I think that the basicstyle=\ttfamily was a crucial part of obtaining the final look and having cut&paste work properly. – Yossi Farjoun Apr 11 '12 at 20:42

You could always provide the SQL code as "attachments" to the PDF. Of course, that might really only work with Acrobat Reader...

I could fix this problem by setting a different monospace font:

\newfontfamily\listingsfont[Scale=.7]{DejaVu Sans Mono}
\lstset{basicstyle=\listingsfont}


(This is more or less what @Jan Hlavacek describes in his comment)

I was looking for a subject about this problem to write what I found out playing around with the fontfamilies.

I used the option columns=fullflexible on my example but it created other problems that was serious to leave them like this. For example I have the following code in a lstlisting environment.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Package: ExampleMyBoxed               %
%                                         %
%   version: 0-1 (beta)                   %
%   Author: Kostis Leledakis              %
%                                         %
%   License: Do anything you want         %
%                                         %
%   Date: 01/Jul/2017                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[1995/05/16]


the above code with the option columns=fullflexible gives the next result:

But if I remove that option and add the option

basicstyle=\fontfamily{pcr}\selectfont,

then the above problem disappears from the pdf and I can copy paste from the pdf to a text editor. The result in the pdf is:

Of course the above text will not be copied with it's spaces to the text editor... We have to fix it manually... but the rest of the text will not contain extra spaces (it will contain less but may be you can fix it too by combining my answer here with the answer of @PhilippeGoutet here I will make a comment there too because his method with "phantom spaces" doesn't work on my (linux) pdf readers).

Anyway even if you find a solution to add the spaces too even if you don't find and you lose some spaces.. setting :

basicstyle=\fontfamily{pcr}\selectfont


removes the extra spaces without the need of fullflexible columns...

• surprisingly, this is the only answer that worked for me (the suggested basicstyle, disregarding all the explanations) – Dalker Oct 4 '19 at 14:27
• Thanks @Dalker ... Happy you found that a good approach ... – koleygr Oct 5 '19 at 1:36