4

I am trying to simply center a listing typeset with listings in a Beamer slide. There is some strange behaviour I cannot understand.

I have been testing 3 methods of centering:

  • the center environment
  • the \centering command
  • the centering environment

None of these produce a centered listing.

Additionally, using the center environment causes the listing to contain an extra small vertical space just before the first code line.

If I wrap the listing in a tikz node, all the methods work, although the extra vertical space with center remains. I cannot wrap the listing with a \makebox because it messes with the parsing.

The question is: Why is this happening and how can I fix it?

MWE:

\documentclass{beamer}

\usepackage{listings}
\usepackage{tikz}
\lstset{basicstyle=\ttfamily}

\begin{document}

% REGULAR, EXPECTED BEHAVIOUR
\begin{frame}[fragile]{No centering}
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
  \par Bla\par
\end{frame}

% NO CENTERING ACHIEVED, STRANGE EXTRA VERTICAL SPACE
\begin{frame}[fragile]{Center Env}
\begin{center}%
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}%
  \par Bla\par
\end{center}
\end{frame}

\begin{frame}[fragile]{Centering}
  \centering%
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
  \par Bla\par
\end{frame}

\begin{frame}[fragile]{Centering Env}
  \begin{centering}%
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
  \par Bla\par
  \end{centering}
\end{frame}

\begin{frame}[fragile]{Center Env + Tikz node}
  \begin{center}%
  \tikz\node{%
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
  };
  \par Bla\par
  \end{center}
\end{frame}

\begin{frame}[fragile]{Centering + Tikz node}
  \centering%
  \tikz\node{%
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
  };
  \par Bla\par
\end{frame}

%%% GENERATES AN ERROR
% \begin{frame}[fragile]{Error}
%   \begin{center}%
%   \makebox{%
%   \begin{lstlisting}[gobble=4]
%     hello := "world";
%     there := 1;
%   \end{lstlisting}%
%   }
%   \end{center}
% \end{frame}

\end{document}

EDIT

Thanks to this answer I worked out a solution with saveboxes:

\newsavebox{\codebox}
\begin{lrbox}{\codebox}
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
\end{lrbox}

\begin{frame}[fragile]{Centering}
  \centering%
  \usebox{\codebox}
  \par Bla\par
\end{frame}

\begin{frame}[fragile]{Centering Env}
  \begin{centering}%
  \usebox{\codebox}
  \par Bla\par
  \end{centering}
\end{frame}

The question remains: Why is this happening (the extra vspace is especially mysterious) and is there a better way?

Bordaigorl
  • 15,135

1 Answers1

6

Workaround:

If you artificially make the linewidth smaller (e.g. by wrapping the listing in a minipage), the whole listing can be centred, however the text will still be left aligned with respect to the minipage.

\documentclass{beamer}

\usepackage{listings}
\usepackage{tikz}
\lstset{basicstyle=\ttfamily}

\begin{document}


\begin{frame}[fragile]{No centering}
    \centering
    \begin{minipage}{.381\textwidth}
  \begin{lstlisting}[gobble=4]
    hello := "world";
    there := 1;
  \end{lstlisting}
    \end{minipage}
  \par Bla\par
\end{frame}




\end{document}

enter image description here