Beamer List Listings – How to Prevent Beamer from Removing Tab Alignment in lstlisting

beamerlistings

I want to show my C# code snippet on the presentation slides using beamer.
Because the code is long, I set the frame to be breakable using the frame option allowframebreaks. The code now can be shown in several frames. Unfortunately, the tab alignment in the original source no longer be preserved.


alt text


\documentclass[dvipsnames,cmyk]{beamer}
\usepackage{listings}

\lstset%
{%
   language=[Sharp]C,
   backgroundcolor=\color{Black},
   basicstyle=\color{White}\tiny\ttfamily,
   keywordstyle=\color{Orange},
   identifierstyle=\color{Cyan},
   stringstyle=\color{Red}, 
   commentstyle=\color{Green},  
   breaklines=true,
   breakatwhitespace=true,
   tabsize=10,
   showstringspaces=false%
}



\begin{document}
\begin{frame}[allowframebreaks,fragile]{MyListing}
\begin{lstlisting}
using System;

publid delegate void Foo(object o);

public class Program: object
{
 public static void Main(string[] args)
 {
   Console.WriteLine("Hello World");//This is a comment.
   /*This is a comment too.*/
 }
 public static void PSTricks(this object)
 {
   Say("I Love PSTricks!");

   Say("I Love PSTricks!");

   Say("I Love PSTricks!");

   Say("I Love PSTricks!");
 }

 public static void LaTeX(this object)
 {
   Say("I Love LaTeX too!");

   Say("I Love LaTeX too!");

   Say("I Love LaTeX too!");

   Say("I Love LaTeX too!");
 }

 public void Say(System.String message)
 {
   System.Text.StringBuilder sb=new System.Text.StringBuilder();

   sb.Append(messange);

   Console.WriteLine(sb);
 }
}
\end{lstlisting}
\end{frame}
\end{document}

Edit 1:

This bad behavior only happens for the inline inclusion.

The inclusion from an external file works well as shown in the following figure.

alt text

Best Answer

That cannot work, because fragile material has to written into an external temporary file and then read back by beamer. The tabs get lost here. Use it this way:

\documentclass[dvipsnames,cmyk]{beamer}

\usepackage{listings}

\begin{document}

\defverbatim[colored]\Lst{%
\begin{lstlisting}[tabsize=2,showtabs,frame=single]
using System;
                publid delegate void Foo(object o);
\end{lstlisting}}

\begin{frame}[allowframebreaks]{MyListing}
\Lst
\end{frame}

\end{document}

and by the way: please provide minimal examples ... ;-)

Related Question