2

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). – jub0bs Jun 22 '15 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 Jun 22 '15 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 Jun 22 '15 at 14:24
  • 1
    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 Jun 22 '15 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 Jun 22 '15 at 16:03
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:

\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 '16 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. – RTbecard Mar 21 '16 at 15:29
  • This solved my issue with extra spaces being inserted when copying code listings from Adobe Acrobat Reader DC - thanks! – Peet Whittaker Apr 27 '17 at 10:28
0

Finally, here's what worked for me:

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

Thanks a lot to Thruston for helpful comments!

  • Theoretically god can always answer his/her own questions. – Friendly Ghost Jul 6 '15 at 11:06
  • Unless we hit omnipotence paradox :-) – god Jul 17 '15 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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