Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

enter image description here

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?

enter image description here

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?

enter image description here

share|improve this question
    
A tip: You can use backticks ` to mark your inline code as I did in my edit. –  Jubobs May 1 at 22:34

2 Answers 2

up vote 2 down vote accepted

In addition to

breakindent=0pt,
breakatwhitespace,

you probably also want

columns=fullflexible,

Here is a comparison.

With columns=fixed (default)

enter image description here

With columns=fullflexible

enter image description here


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}
share|improve this answer
    
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 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 at 15:54
    
Thank you I will search for this further! –  Adam May 3 at 15:58

The indentation can be turned off:

\lstset{breakindent=0pt}

breakindent=0pt

The continuation lines can even start at the very left:

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

breakindent=0pt,autobreakindent=0pt

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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