# How to remove the leading unnecessary white spaces when using linerange option?

To be honest, I am looking for people who already read the listings documentation completely. If those people also cannot solve it, then the documentation does not mention anything about it. Sorry for this inconvenience.

But at least I can show my effort by providing you with the minimal working example.

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{Program.cs}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate
{
class Program
{
// start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
// stop
}
}
\end{filecontents*}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{Common}
{
language={[Sharp]C},
numbers=left,
numbersep=1em,
numberstyle=\tiny\noaccsupp,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=2,
columns=flexible,
includerangemarker=false,
rangeprefix=//\ ,
}

\lstdefinestyle{A}
{
style=Common,
backgroundcolor=\color{yellow!10},
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{blue}\bf,
identifierstyle=\color{black},
stringstyle=\color{red},
}

\begin{document}
\section*{Full Code}
\lstinputlisting[style=A]{Program.cs}
\section*{Code Snippet}
\lstinputlisting[style=A,linerange=start-stop]{Program.cs}
\end{document}*


My question is how to remove the leading white space when using linerange option?

## Edit

Consider some extreme cases below.

## Case 1

namespace Delegate
{
class Program
{
// start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}
// stop


The code should be rendered as follows

with includrangemarker=true

        // start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}
// stop


with includerangemarker=false

        static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}


## Case 2

namespace Delegate
{
class Program
{
// start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}
// stop


The code should be rendered as follows

with includerangemarker=true,

    // start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}
// stop


with includerangermarker=false

        static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
}
}

-
Unfortunately the gobble option and the autogobble from lstautogobble package have no effect with \lstinputlisting... – karlkoeller Dec 24 '13 at 16:21
What do you mean? Anyway, these can be of interest: tex.stackexchange.com/q/43686/27635 and tex.stackexchange.com/q/48903/27635. – karlkoeller Dec 24 '13 at 16:31
Today I'm lazy, too... – karlkoeller Dec 24 '13 at 16:46
@WeirdstressFunction I know, if would be ideal, but listings makes the implementation of such a feature very tricky. I don't know how to implement it. Sorry. The best I can do is what I wrote in my earlier comment. – Jubobs Mar 14 '14 at 17:07
@WeirdstressFunction I noticed my code breaks comment highlighting, but I just fixed it. I'll post the updated code when I get the Tab characters covered as well. I'll be done after that. – Jubobs Mar 14 '14 at 19:30

Here is a solution. More details follow.

## Limitations of the gobble option (provided by listings)

The listings package provides a key called gobble, which allows the user to specify a fixed number of characters (spaces or otherwise) to be gobbled at the beginning of each line. However, gobble suffers from the following limitations.

• It lacks automation: the user has to look at the listing beforehand to determine how many spaces should be gobbled.
• The gobble key is only compatible with embedded listings (i.e. typeset using an lstlisting environment), most emphatically not with listings residing in standalone files (i.e. typeset using \lstinputlisting).

## Limitations of the autogobble option (provided by lstautogobble)

The lstautogobble package provides a Boolean key called autogobble, which automates the gobbling of leading spaces; more specifically, it measures the leading spaces on the very first line (whether it be typeset or not) of the whole listing and passes that value to the gobble key. However, autogobble suffers from the following limitations.

• Because it counts leading white spaces on the very first line of the listing, it may not remove the appropriate amount of leading white space if the range of lines typeset doesn't start by line 1.
• Because it is based on the gobble key, it inherits the latter's incompatibility with \lstinputlisting.

## A new key for removing leading white space: autounindentautodedent

The approach below defines a Boolean key called autounindent autodedent, which,

• if set, removes leading white space even if only a range of lines are typeset (i.e. if either the firstline option or the linerange option is used),
• is compatible with both lstlisting and \lstinputlisting.

## Known limitations

The code below doesn't handle tab characters properly yet.

Moreover, the desired behaviour in the extreme cases covered in your edit seems very tricky to implement, because it would require two passes. I'm out of my depth! The best I can do is to gobble as many spaces as there are at the beginning of the line right after the // start marker (i.e. the line starting by static void in your code).

Please notify me of any other problem.

Update: I've renamed the key to autodedent (which is easier to read and write than autounindent). I also corrected a problem reported by the OP, according to this answer by Heiko Oberdiek. Finally, for convenience, I've implemented that feature in a small package called lstautodedent; the alpha version is available at https://bitbucket.org/Jubobs/lstautodedent

## Code

\documentclass[preview,border=12pt,12pt]{standalone}

\usepackage{filecontents}

\begin{filecontents*}{Program.cs}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate
{
class Program
{
// start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
// stop
}
}
\end{filecontents*}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{lstautodedent}

\lstdefinestyle{Common}
{
language={[Sharp]C},
numbers=left,
numbersep=1em,
numberstyle=\tiny\noaccsupp,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=2,
columns=flexible,
includerangemarker=false,
rangeprefix=//\ ,
}

\lstdefinestyle{A}
{
style=Common,
backgroundcolor=\color{yellow!10},
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{blue}\bf,
identifierstyle=\color{black},
stringstyle=\color{red},
}

\begin{document}
\section*{Full Code}
\lstinputlisting[style=A]{Program.cs}
\section*{Code Snippet}
\lstinputlisting[style=A,linerange=start-stop,autodedent]{Program.cs}
\end{document}*

-
@TheLastError If I polish the code and solve the problems I've listed, do you think that would make for a useful package? – Jubobs Mar 10 '14 at 13:47
I think it can be merged with Martin Scharer's package as he also develops addon for listings. Please kindly ask him the details. – kiss my armpit Mar 10 '14 at 13:51
@FifaEarthCup2014 I got no response from Martin Scharrer, so I ended up implementing the feature in a small package called lstautodedent. – Jubobs Jun 14 '14 at 17:38
OK. Thanks for implementing that package. – kiss my armpit Jun 14 '14 at 18:01
@cyanide-basedfood I'm considering submitting lstautodedent on CTAN. Have you experienced any problem? Do you have any bug to report? – Jubobs Sep 4 '14 at 14:50