[Tex/LaTex] lstlisting environment ignores tabs

beamerlistingssourcecode

I have this LaTeX code:

    \lstset{
    language=Java,
    tabsize=8,
    keepspaces,
    extendedchars=true,
    rulecolor=\color{black},
    basicstyle=\footnotesize,
    aboveskip=5pt,
    upquote=true,
    columns=fixed,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    frame=single,
    showtabs=true,
    showspaces=false,
    showstringspaces=false,
}

\begin{frame}[fragile]
\begin{lstlisting}
    public class Teddy {
        public MethodName(String name, int var) {
             this.name = name;
        }
    }
\end{lstlisting}
\end{frame}

Beamer ignores my tabs inside the lstlisting because my frame is fragile. But I need it to be fragile, because I get the "Runaway Argument"-Exception, when there is no fragile.

What can I do, except importing the Lsting from an external file?

I don't want to define a new command for each source code listing, like proposed in this solution: How to prevent beamer from removing the tab alignment of lstlisting?

Best Answer

use it this way:

\documentclass{beamer}
\usepackage{listings}
    \lstset{
    language=Java,
    tabsize=8,
    keepspaces,
    extendedchars=true,
    rulecolor=\color{black},
    basicstyle=\footnotesize,
    aboveskip=5pt,
    upquote=true,
    columns=fixed,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    frame=single,
    showtabs=true,
    showspaces=false,
    showstringspaces=false,
}
\begin{document}
\defverbatim\lst{%
\begin{lstlisting}
    public class Teddy {
        public MethodName(String name, int var) {
            this.name = name;
        }
    }
\end{lstlisting}
}
\begin{frame}
\lst
\end{frame}
\end{document}

enter image description here