# How to left align lstlisting code with text

I'm using listings package to include sample code to my document and the frame option to surround the code inside a box. The problem is that I got too much white space between the left rule of the frame and the code (see pic below).

The problem seems not to be the frame itself, but the left alignment of the code. I tried to update the xleftmargin option, but nothing happened. The only way I can decrease the white space is to give a negative length to the left rule of the frame with the framexleftmargin option, but I don't like it since I want the frame width to be of the same width of the text. I just want to decrease the left white space to take advantage of the full line and get less broken lines within the code.

This is my current configuration.

\lstset{language=Java,
keywordstyle=\color{RoyalBlue},
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black}
}


Please help me to solve this problem. I'm stuck on it for hours.

Thank you.

EDIT: Provided a minimal working example, as required.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
keywordstyle=\color{RoyalBlue},
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black}
}

\begin{document}
\begin{lstlisting}
public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
LongWritable mkey = new LongWritable();
LongWritable mval = new LongWritable();

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
long src, dst;

if (tokenizer.hasMoreTokens()) {
src = Long.parseLong(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("Invalid edge line: " + line);
dst = Long.parseLong(tokenizer.nextToken());

mkey.set(src);
mval.set(dst);
context.write(mkey, mval);
context.write(mval, mkey);
}
}
}
\end{lstlisting}
\end{document}


-
Did you indent the listings text in the source code of your document? If so, read about the gobble option in the »listings« manual. That said, it would be better if you prepare a self-contained and minimal example. – Thorsten Donig Feb 25 '14 at 16:56
could you provide a complete MWE that demonstrates the problem? it sounds interesting, but we'll need a starting point :) – cmhughes Feb 25 '14 at 16:56
In a listing spaces is not ignored, so all the indentation in your listing is of course included in the PDF as well, as is to be expected. Thorsten's suggestion sounds promising though. – Torbjørn T. Feb 25 '14 at 18:22
@cmhughes I provided a MWE as you asked. – Driu Feb 25 '14 at 18:22
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Feb 26 '14 at 9:38

## 3 Answers

Just load the lstautogobble package and insert autogobble=true in your \lstset, so you don't have to bother about code indentation.

MWE

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings,lstautogobble}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
keywordstyle=\color{RoyalBlue},
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black},
autogobble=true
}

\begin{document}
\begin{lstlisting}
public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
LongWritable mkey = new LongWritable();
LongWritable mval = new LongWritable();

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
long src, dst;

if (tokenizer.hasMoreTokens()) {
src = Long.parseLong(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("Invalid edge line: " + line);
dst = Long.parseLong(tokenizer.nextToken());

mkey.set(src);
mval.set(dst);
context.write(mkey, mval);
context.write(mval, mkey);
}
}
}
\end{lstlisting}
\end{document}


Output:

The autogobble option gobbles the spaces in the whole listing according to the number of spaces in the first line. If such a package has been implemented, this means that your question is not silly at all!

-

Just remove the spaces in front of your code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
keywordstyle=\color{RoyalBlue},
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black}
}

\begin{document}
\begin{lstlisting}
public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
LongWritable mkey = new LongWritable();
LongWritable mval = new LongWritable();

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
long src, dst;

if (tokenizer.hasMoreTokens()) {
src = Long.parseLong(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("Invalid edge line: " + line);
dst = Long.parseLong(tokenizer.nextToken());

mkey.set(src);
mval.set(dst);
context.write(mkey, mval);
context.write(mval, mkey);
}
}
}
\end{lstlisting}
\end{document}

-
A much better option is to use gobble=8 as per the suggestions in the comments. – Peter Grill Feb 25 '14 at 18:26
I never would've though that leading tabs could be a problem. Sorry for silly question. – Driu Feb 25 '14 at 18:49
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\parindent=0pt
\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
keywordstyle=\color{RoyalBlue},
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black},
xleftmargin=\dimexpr\fboxsep-\fboxrule,
xrightmargin=\dimexpr\fboxsep-\fboxrule,
gobble=8
}

\begin{document}

foo bar bza \hfill foo bar bza

\begin{lstlisting}
public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
LongWritable mkey = new LongWritable();
LongWritable mval = new LongWritable();

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
long src, dst;

if (tokenizer.hasMoreTokens()) {
src = Long.parseLong(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens())
throw new RuntimeException("Invalid edge line: " + line);
dst = Long.parseLong(tokenizer.nextToken());

mkey.set(src);
mval.set(dst);
context.write(mkey, mval);
context.write(mval, mkey);
}
}
}
\end{lstlisting}
\end{document}


-