# Problem with spaces in listings package and hyphenation

I use the listings package and there is a space that is inserted when the line breaks and the code goes to that new line. Why is this space inserted in the new line? How can I remove it?

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}

\setlength{\parindent}{0cm}

\usepackage{listings}

\begin{document}
\lstset{basicstyle=\ttfamily}

\begin{lstlisting}[breaklines=true]

//Comments are // and /* */

//Namespaces Classes Methods

namespace General //Can be ommited
{
class Specific
{
static void Main
{
int[] array=new int[n] {1, 2,..., n};

int [,] array;

string[] names = new string[] {"Matt", "Joanne", "Robert"};

//Also the new can be omitted

int[] array={1, 2, ..., n};

string[] names = string {"Matt", "Joanne", "Robert"};

}
}
}

\end{lstlisting}

\end{document}


Edit I:

I used a long comment and I ended up with a peculiar result:

//When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a Get accessor is available, so the property is read-only.

Why is so much space before the word "available" and why the same word start a little to the left compared to the others? Also if it was the indentation shouldn't only the first line started with more space and not the others, or at least all the lines should have the same?

Edit II:

Also I think that the problem comes from hyphenation. I used breakindent=0pt, breakatwhitespace=true and there was an improvement but there was also a lot of space in some lines so I think that the problem stems from hyphenation. If yes how can I fix it? If not then what's is the reason for that?

-
A tip: You can use backticks  to mark your inline code as I did in my edit. –  Jubobs May 1 '14 at 22:34

breakindent=0pt,
breakatwhitespace,


you probably also want

columns=fullflexible,


Here is a comparison.

## With columns=fullflexible

See subsection 2.10 in the listings documentation for more details about the columns key.

\documentclass[12pt]{article}
\usepackage
[
top    = 0.3in,
bottom = 1.2in,
left   = 0.8in,
right  = 0.8in,
]{geometry}

\usepackage{listings}
\lstset{
basicstyle        = \ttfamily,
breakatwhitespace = true,
breakindent       = 0pt,
columns           = fullflexible,
}

\begin{document}
\begin{lstlisting}[breaklines=true]
//Comments are // and /* */
//Namespaces Classes Methods
//When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a Get accessor is available, so the property is read-only.
namespace General //Can be ommited
{
class Specific
{
static void Main
{
int[] array=new int[n] {1, 2,..., n};
int [,] array;
string[] names = new string[] {"Matt", "Joanne", "Robert"};
//Also the new can be omitted
int[] array={1, 2, ..., n};
string[] names = string {"Matt", "Joanne", "Robert"};
}
}
}
\end{lstlisting}
\end{document}

-
Still it doesn't seem the hyphenation to work as good as the plain text. The lines aren't equally balanced. Why is that? –  Adam May 3 '14 at 14:50
@Adam Unfortunately, I don't know enough about the specifics, but I can tell you that listings has a very peculiar way of typesetting text, much different from TeX's algorithm for breaking paragraph into lines. Therefore, you shouldn't expect your listings to look like normal text. Setting columns=fullflexible is as good as it's gonna get, I'm afraid. –  Jubobs May 3 '14 at 15:54
Thank you I will search for this further! –  Adam May 3 '14 at 15:58

The indentation can be turned off:

\lstset{breakindent=0pt}


The continuation lines can even start at the very left:

\lsetset{
breakindent=0pt,
autobreakindent=0pt,
}


See section "4.10 Margins and line shape" in the documentation of package listings`.

-