# Unwanted space around punctuation in copied & pasted listing

When I compile the .tex file below into .pdf using rubber -d mwe.tex and try to copy & paste the contents of the listing, I get spaces around . characters that were not present in the original listing. How do I force listings to preserve spacing (i.e. not add any) and always show exactly what I've typed?

\documentclass{article}
\usepackage{listings}
\lstset{language=sh}
\begin{document}
\title{Lol}
\maketitle
\section{Introduction}
Where does spaces around dot comes from:
\begin{lstlisting}
lxc.id_map = g 0 100000 65536
\end{lstlisting}
\end{document}

• Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). Jun 22, 2015 at 14:19
• You should avoid copying and pasting listings from PDF, as that is highly unreliable and impractical. This should be of interest: tex.stackexchange.com/a/195495/21891 Jun 22, 2015 at 14:21
• It is showing exactly what you've typed but it's just using fixed width spacing for each character, due to the design decisions made by the package author. You need either to switch it to a typewriter font or fiddle with the spacing to say you want it flexible. IMHO the set of defaults chosen by listings is rubbish, but at least it forces you to read the docs to change the formatting to something more sane. Jun 22, 2015 at 14:24
• To stop the horrible fixed pitch spacing you could use \lstset{language=sh, columns=fullflexible}, but again you still might get oddities if you cut and paste from PDF. Jun 22, 2015 at 14:32
• It mainly depends on the PDF viewer; if I copy from Skim I get no added space, they appear if I copy from Adobe Reader. Jun 22, 2015 at 16:03

@god's answer worked for me, however, the magic line that fixed the spacing for issues for copying and pasting in my case was: columns=fullflexible (as noted by @Thruston), when using the following font family basicstyle=\ttfamily\footnotesize.

For my document, I used the following settings:

\lstset{language=R,
basicstyle=\ttfamily\footnotesize,
columns=fullflexible
}


PS. I wanted to write this in a comment, but not enough rep and I think this could speed things up for users who just want to resolve the spacing issues without copying @god's whole style.

• This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Rico
Mar 21, 2016 at 14:52
• The question was how to get rid of extra white spaces which were not present in the original text when copy and pasting from a latex document. I went through @god's answer, but I found that only one of the options he detailed (noted by @Thruston) was necessary. IMO, this gives a more specific answer. Albeit, I could have stated this more clearly. Mar 21, 2016 at 15:29
• This solved my issue with extra spaces being inserted when copying code listings from Adobe Acrobat Reader DC - thanks! Apr 27, 2017 at 10:28
• This worked for me: it solved my problem with spaces inserted when copying listings from my pdf file. Thanks, so helpfully! Sep 20, 2021 at 20:06

Finally, here's what worked for me:

\lstset{language=sh,
frameround=fttt,
basicstyle=\ttfamily\footnotesize,
stringstyle=\itshape,
identifierstyle=, % nothing happens
inputencoding=utf8x,
extendedchars=\true,
showstringspaces=false,
showspaces=false,
showtabs=false,
particular underscores
frame=single,
columns=fullflexible,
captionpos=t,
tabsize=4,
keepspaces=true,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
title=\lstname
}