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'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}

Trouble with listing and frame

share|improve this question
2  
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 at 16:56
3  
could you provide a complete MWE that demonstrates the problem? it sounds interesting, but we'll need a starting point :) –  cmhughes Feb 25 at 16:56
1  
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 at 18:22
    
@cmhughes I provided a MWE as you asked. –  Driu Feb 25 at 18:22
1  
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 at 9:38

3 Answers 3

up vote 4 down vote accepted

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:

enter image description here

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!

share|improve this answer

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}
share|improve this answer
2  
A much better option is to use gobble=8 as per the suggestions in the comments. –  Peter Grill Feb 25 at 18:26
    
I never would've though that leading tabs could be a problem. Sorry for silly question. –  Driu Feb 25 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}

enter image description here

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.