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?

Where does spaces around dot comes from:
lxc.id_map = g 0 100000 65536
  • 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).
    – jub0bs
    Commented Jun 22, 2015 at 14:19
  • 1
    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
    – jub0bs
    Commented 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.
    – Thruston
    Commented Jun 22, 2015 at 14:24
  • 2
    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.
    – Thruston
    Commented Jun 22, 2015 at 14:32
  • 1
    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.
    – egreg
    Commented Jun 22, 2015 at 16:03

2 Answers 2


@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:


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
    Commented 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.
    – user84793
    Commented 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! Commented 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! Commented Sep 20, 2021 at 20:06

Finally, here's what worked for me:

identifierstyle=, % nothing happens
particular underscores

Thanks a lot to Thruston for helpful comments!

  • 1
    Theoretically god can always answer his/her own questions. Commented Jul 6, 2015 at 11:06
  • Unless we hit omnipotence paradox :-)
    – god
    Commented Jul 17, 2015 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .