6

I use TexMaker. With the preamble that I have been using, I am able to get the indentation right for programs when used in a article, report, etc. But when I use the same structure within a frame in beamer, the indentation gets ignored. I have tried the fixes suggested in other posts of similar kind, but none of them work for me.

MWE:

\documentclass{beamer}
\usetheme{boadilla}
\usecolortheme{beaver}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{%frame=tb, % to form a frame around code
   language=Java,
   aboveskip=3mm,
   belowskip=3mm,
   showstringspaces=false,
   columns=flexible,
   basicstyle={\small\ttfamily},
   numbers=none,
   numberstyle=\tiny\color{gray},
   keywordstyle=\color{blue},
   commentstyle=\color{dkgreen},
   stringstyle=\color{mauve},
   breaklines=true,
   breakatwhitespace=true,
   tabsize=3,
}

\begin{document}
\begin{frame}[fragile]
\frametitle{Sample Program}
\begin{lstlisting}
public int increment() {
    int a = 5;
    int i = (++a) + (++a) + (a++);
    System.out.println(a);
    return i;
}
\end{lstlisting}
\end{frame}
\end{document}

The output that I get:

enter image description here

5
  • Welcome to TeX.SX! You should have something you've not mentioned in the question that prevents the indentation. Please add a minimal working example (MWE) starting with \documentclass{...} and ending with \end{document} that reproduces the issue. Jan 4, 2015 at 20:00
  • I edited my post as you said. Note: I usually copy-paste my java code from Eclipse to TexMaker Jan 4, 2015 at 20:15
  • I still cannot reproduce the issue. Try upgrading your TeX distribution. Jan 4, 2015 at 20:40
  • @karlkoeller I believe the code contains tabs, which are being converted to spaces upon posting, making the issue tricky to reproduce.
    – G. Poore
    Jan 4, 2015 at 20:54
  • @karlkoeller: Poore is correct! Jan 4, 2015 at 21:25

1 Answer 1

6

This is due to indentation with tabs. There are many workarounds, as well as a patch.

Equivalent questions have been asked previously [1, 2]. In those cases, the proposed solution was to use spaces rather than tabs for indentation, or to define a new command for each listing.

Another possibility is to use fragile=singleslide. That will disable overlays, but will make the tabs function correctly.

The basic problem is that the tab character vanishes when beamer writes the frame contents to a temp file with the fragile option. A patch that fixes this is below; it may be added to your document's preamble, after beamer is loaded. Note that if you use this with the XeTeX engine, you will need to use the -8bit command-line option (see this for details). I have also opened an issue at the Beamer github page for this.

\makeatletter
\def\beamer@verbatimreadframe{%
  \begingroup%
  \let\do\beamer@makeinnocent\dospecials%
  \count@=127%
  \@whilenum\count@<255 \do{%
    \advance\count@ by 1%
    \catcode\count@=11%
  }%
  \beamer@makeinnocent\^^L% and whatever other special cases
  \beamer@makeinnocent\^^I% <-- PATCH: allows tabs to be written to temp file
  \endlinechar`\^^M \catcode`\^^M=12%
  \@ifnextchar\bgroup{\afterassignment\beamer@specialprocessframefirstline\let\beamer@temp=}{\beamer@processframefirstline}}%
\makeatother
1
  • 2
    I've added the patch to beamer and will look to do a release some time this week.
    – Joseph Wright
    Jan 5, 2015 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .