5

I am including some code in my beamer document. I would like to get rid of all margins, so I can include as much code as possible. This is the code I am trying:

\documentclass[]{beamer}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc} 
\usepackage{listings} 
\usepackage{caption}  
\usepackage{color}
\definecolor{comentaryGreen}{rgb}{0,0.6,0}
\lstset{
         basicstyle=\tiny  ,
         numbers=none,              
         tabsize=1,
         extendedchars=true,         
         breaklines=true,
         language=Java,                              
         keywordstyle=\color{blue},       
         commentstyle=\color{comentaryGreen},
         rulecolor=\color{black},         
         showspaces=false,  
         showtabs=false,   
         showstringspaces=false, 
         xleftmargin=0pt,
         framexleftmargin=0pt,
         framexrightmargin=0pt,
         framexbottommargin=0pt,
 }
 \lstloadlanguages{
         Java
 } 
\usetheme{Antibes}
\usecolortheme{dolphin}

\begin{frame}
    \lstinputlisting[linerange={529-550}]{source/cGaussianBlur.java}
\end{frame}

\end{document}

(the code I used is available in http://imagej.nih.gov/ij/source/ij/plugin/filter/GaussianBlur.java)

This is the result:

enter image description here

How can I get rid of those margins?

José D.
  • 291
  • 2
    Off-topic comment: so I can include as much code as possible is no guarantee for a good presentation ;-) –  Sep 08 '14 at 08:55
  • if you want to remove right and left margins for all frames, just use \setbeamersize{text margin left=0cm,text margin right=0cm} – d-cmst Sep 08 '14 at 09:08
  • @dcmst Is it possible to do that for just one frame? – José D. Sep 08 '14 at 09:08
  • 1
    @Trollkemada yes, please see the answer here and let us know if it solves your problem so that this question can be closed as duplicate. – d-cmst Sep 08 '14 at 09:13
  • Have you tried to just pass an appropriate negative value to the xleftmargin option, e.g., xleftmargin=-1cm? – Daniel Sep 08 '14 at 09:27
  • @dcmst You may use a columns environment with a single column (with .99\paperwidth as width) to get the same result (no margin). – Paul Gaborit Sep 08 '14 at 14:03
  • you're all on the wrong track, see my answer – MaxNoe Sep 08 '14 at 18:15
  • @MaxNoe Well, that's a bit presumptuous. Since the OP didn't post his/her listing, you can't know for sure that the problem stems from indentation in it. – jub0bs Sep 08 '14 at 18:25
  • at least the indent of lstinputlisting leads to one additional tab space – MaxNoe Sep 08 '14 at 18:41
  • @MaxNoe You're right: the external file is indentend by 4 spaces. I hadn't noticed the link to it in the OP's question. – jub0bs Sep 08 '14 at 20:11
  • The part he is including is even indented by 8 spaces ;) – MaxNoe Sep 08 '14 at 20:14

2 Answers2

7

As pointed out by dcmst in his (her?) comment, you can follow, for instance, Paul Gaborit's approach to locally increase the text width:

\begin{frame}
  \begin{columns}
    \column{\dimexpr\paperwidth-10pt}
      \lstinputlisting{cGaussianBlur.java}
  \end{columns}
\end{frame}

However, there will still be some whitespace on the left:

enter image description here

Why? Because the lines in your source file are indented, and listings will include that indentation in the output; it will/can not remove it.

Actually, listings does provide a gobble key for removing a specified number of spaces at the beginning of each line of a listing. Besides, Martin Scharrer's lstautogobble package improved upon that by providing a key called autogobble, which automatically measures the number of spaces that need to be gobbled, so that you don't have to specify that number yourself, as mentioned in MaxNoe's answer.

However, those two approaches are only compatible with "embedded" listings, i.e. listings that live within your .tex file, inside an lstlisting environment (or the likes of it). They do not work for external listings, i.e. those living in external files and inserted with the \lstinpulisting macro.

To solve this problem, I wrote, a while back, a little package called lstautodedent. It defines a key called autodedent, which works very similarly to how lstautogobble's autogobble key works. The main difference is that it works for both embedded listings and external listings. Only one caveat: it doesn't play well with tab characters.

I haven't submitted that package on CTAN yet, but you can already grab the .sty file from my GitHub account.

To use it, load lstautodedent in your preamble, set the autodedent key as you would any other Boolean listings key, and (assuming your source code is indented with space characters, not tabs) you won't get any whitespace on the left:

enter image description here

Complete code

\documentclass[]{beamer}

\usepackage{lstautodedent}

% this is only to write a fraction of your source file to 
% an external file and make my answer self-contained
\usepackage{filecontents}
\begin{filecontents*}{cGaussianBlur.java}
    public float[][] makeGaussianKernel(final double sigma, final double accuracy, int maxRadius) {
        int kRadius = (int)Math.ceil(sigma*Math.sqrt(-2*Math.log(accuracy)))+1;
        if (maxRadius < 50) maxRadius = 50;         // too small maxRadius would result in inaccurate sum.
        if (kRadius > maxRadius) kRadius = maxRadius;
        float[][] kernel = new float[2][kRadius];
        for (int i=0; i<kRadius; i++)               // Gaussian function
            kernel[0][i] = (float)(Math.exp(-0.5*i*i/sigma/sigma));
        if (kRadius < maxRadius && kRadius > 3) {   // edge correction
            double sqrtSlope = Double.MAX_VALUE;
            int r = kRadius;
            while (r > kRadius/2) {
                r--;
                double a = Math.sqrt(kernel[0][r])/(kRadius-r);
                if (a < sqrtSlope)
                    sqrtSlope = a;
                else
                    break;
            }
            for (int r1 = r+2; r1 < kRadius; r1++)
                kernel[0][r1] = (float)((kRadius-r1)*(kRadius-r1)*sqrtSlope*sqrtSlope);
        }
\end{filecontents*}


\usepackage{listings} 
\usepackage{color}
\definecolor{comentaryGreen}{rgb}{0,0.6,0}
\lstset{
         basicstyle=\tiny  ,
         numbers=none,              
         tabsize=1,
         extendedchars=true,         
         breaklines=true,
         language=Java,                              
         keywordstyle=\color{blue},       
         commentstyle=\color{comentaryGreen},
         rulecolor=\color{black},         
         showspaces=false,  
         showtabs=false,   
         showstringspaces=false, 
         xleftmargin=0pt,
         framexleftmargin=0pt,
         framexrightmargin=0pt,
         framexbottommargin=0pt,
         autodedent,
 }
 \lstloadlanguages{
         Java
 } 
\usetheme{Antibes}
\usecolortheme{dolphin}

\begin{document}

\begin{frame}
  \begin{columns}
    \column{\dimexpr\paperwidth-10pt}
      \lstinputlisting{cGaussianBlur.java}
  \end{columns}
\end{frame}

\end{document}
jub0bs
  • 58,916
0

Your "problem" is your proper indentation of your code. lstlisting is a verbatim-environment and as such it typesets the tabs of your indentation.

The solution for this problem is delivered by the lstautogobble-package:

\usepackage{listings}
\usepackage{lstautogobble}

\lstset{autogobble,
        ...,
        ...,
    }

It subtracts the right amount of tabs so your LaTeX-code-indentation is not taken into account.

MaxNoe
  • 6,136
  • Unfortunately, autogobble has no effect on external listings, i.e. those living in some external file and inserted with \lstinpulisting. – jub0bs Sep 08 '14 at 20:10
  • oh, that's too bad. So copying the code into the texfilewould be the only solution then, I suppose. – MaxNoe Sep 08 '14 at 20:16