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.


        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},

SELECT ST_Area(the_geom) 
 FROM katastar 
 WHERE kc_broj = '1414';
  • Is this the only place you get spurious spaces? Nov 4, 2010 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?) Nov 4, 2010 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. Nov 4, 2010 at 10:13
  • 1
    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. Nov 4, 2010 at 12:22
  • 1
    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. Nov 4, 2010 at 12:26

4 Answers 4


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. Nov 4, 2010 at 12:21
  • He he -- so you want your cake and to eat it too! ;) Nov 4, 2010 at 12:24
  • 4
    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? Nov 4, 2010 at 17:21
  • 4
    @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. Dec 23, 2011 at 11:45
  • 5
    @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. Apr 11, 2012 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}

(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                     %


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

enter image description here

But if I remove that option and add the option


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:

enter image description here

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 :


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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.