To prevent random spaces when copying the text from a listing, you need to use
\lstset{columns=flexible}
But you will now note that the text is not neatly aligned anymore; to solve this, you need to also use
\lstset{keepspaces=true}
This will not solve your problem with spaces disappearing at the beginning of lines when copying. The following hack will produce visible spaces and then make them invisible by coloring them in the background color:
\makeatletter
\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
\makeatother
This hack is not perfect, however, as the typesetted character is really a visible space, not a space (so searching the pdf for char line
will not work) and some PDF readers (like Mac's preview) will copy a visible space. It works under Acrobat Reader and it's extremely pleasant to be able to quickly copy/paste code without problem (perhaps the problem can be circumvented by writing direct PDF code to tell that it's a space, I've never had the time to try). It might also not work with all typewriter fonts.
Here's the full code of your example:
\documentclass[12pt,oneside]{memoir}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{textcomp}
\definecolor{codebg}{HTML}{EEEEEE}
\definecolor{codeframe}{HTML}{CCCCCC}
\lstset{language=Awk}
\lstset{backgroundcolor=\color{codebg}}
\lstset{frame=single}
\lstset{framesep=10pt}
\lstset{rulecolor=\color{codeframe}}
\lstset{upquote=true}
\lstset{basicstyle=\ttfamily}
\lstset{showstringspaces=false}
\lstset{columns=flexible}
\lstset{keepspaces=true}
\makeatletter
\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
\makeatother
\begin{document}
This code example prints out all users on your system:
\begin{lstlisting}[language=c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 1024
int main() {
char line[MAX_LINE_LEN];
FILE *in = fopen("/etc/passwd", "r");
if (!in) exit(EXIT_FAILURE);
while (fgets(line, MAX_LINE_LEN, in) != NULL) {
char *sep = strchr(line, ':');
if (!sep) exit(EXIT_FAILURE);
*sep = '\0';
printf("%s\n", line);
}
fclose(in);
return EXIT_SUCCESS;
}
\end{lstlisting}
\end{document}
You can use \begin{lstlisting}[xleftmargin=5.0ex]
to adjust the left margin, or add this option to \lstset
. This will move your listing to the right including the lines numbers and any frames.
The lipsum
package is used to provide some dummy text before the listing. I also included the geometry
package with [showframe]
option to see the margins.
![enter image description here](https://i.stack.imgur.com/iUnRi.png)
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\usepackage{listings}
\begin{document}
\lipsum[1]
\begin{figure}[!htbp]
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{0pt}%
%\begin{center}
\centering
\lstset{language=Java, numbers=left, showspaces=false,
showstringspaces=false, tabsize=2, breaklines=true,
xleftmargin=5.0ex,
%numberstyle=\scriptsize,numbersep=0pt
}
\begin{lstlisting}
public class AutoRegistry {
public void registerCar(int ssn, int vin) {
// ...
PersonCarDirect personCar = new PersonCarDirect();
personCar.setId(ssn);
personCar.setVin(vin);
// ...
}
}
\end{lstlisting}
%\end{center}
%\vspace{-12pt}
\caption{AutoRegistry client class}
\label{fig:autoRegistryClientC}
\end{figure}
\end{document}
As pointed out by @Mico, you should use \centering
instead of \begin{center}...\end{center}
. This would also allow you to eliminate the \vspace{-12pt}
. See Should I use center or centering for figures and tables? for more details. In case someone copies the above code and does not read the other solutions, I have made those changes in the code, and left your original code as comments.
Align Number to just right of column
If you are tight on space and want to align the numbers just to the right of the column, then use:
\newlength{\MaxSizeOfLineNumbers}%
\settowidth{\MaxSizeOfLineNumbers}{99}% Adjust to maximum number of lines
\addtolength{\MaxSizeOfLineNumbers}{2.5ex}%
and set:
xleftmargin=\MaxSizeOfLineNumbers
The 2.5ex
is the adjustment that gets the numbers just to the left of the margin, and we shift over by the width of the digits 99
.
This of course assumes that the number of lines in your listings is less than 100, so adjust accordingly. Also, if you happen to change the number style using something like numberstyle=\tiny
, the {99}
parameter should be adjusted accordingly to {\tiny 99}
.
Best Answer
Unfortunately I really need the line numbers. I want to reference several lines within my text.
After I kept searching, I found a solution for this problem using the following snippet:
That way the line numbers won't be selected anymore. In addition using colums=flexible I get rid of the whitespaces when copy/pasting the source code out of the pdf-file.