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.




\lstset{%frame=tb, % to form a frame around code

\frametitle{Sample Program}
public int increment() {
    int a = 5;
    int i = (++a) + (++a) + (a++);
    return i;

The output that I get:

enter image description here

  • 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


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.

  \@whilenum\count@<255 \do{%
    \advance\count@ by 1%
  \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%
  • 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 .