# Phantom spaces in listings (pdf)

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)

-

## protected by JubobsAug 7 '14 at 23:20

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.