# Center listing horizontally in Beamer

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?

• \leavevmode\hbox{...} works as well. Beamer does funny things with centering. If takes two \hfils to center left justified text. – John Kormylo Oct 25 '18 at 21:36
• I now get why \leavevmode\hbox{...} works: it's not Beamer's fault, it's listings who only boxes the listing if in horizontal mode! – Bordaigorl Oct 26 '18 at 0:49
• The \hbox is for listings, but the \leavevmode is for beamer. – John Kormylo Oct 26 '18 at 14:40

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}


• I don't think that's the case: the tikz node is correctly sized, and the lrbox method also does not need an explicit width... – Bordaigorl Oct 25 '18 at 22:55
• Also, the same centering methods in the article class work with listings – Bordaigorl Oct 25 '18 at 22:55
• AH! This answer explains what happens: > The lstlisting package only boxes the contents if it starts in horizontal mode – Bordaigorl Oct 26 '18 at 0:47