6

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}
god
  • 159
  • 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: http://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
  • You could try \lstset{language=sh, basicstyle=\ttfamily} to get it to use type writer font for everything, which makes the spacing look better, but @Jubobs comments about cutting and pasting still apply - it's notoriously unreliable to c&p spaces from PDF. – Thruston Jun 22 '15 at 14:30
  • 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 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
  • Playing around with basicstyle=\ttfamily and columns=fullflexible did not do the trick unfortunately. And there seems to be nothing about it in lstlistings docs either. – god Jul 06 '15 at 10:48

2 Answers2

7

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

RTbecard
  • 241
-1

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!

god
  • 159