# center the listings title above the code

I can't manage to center the title "Listing:1" over the listing.

\documentclass[8pt,blue]{beamer}

\usepackage{subfigure}

\usepackage{fontspec}
\usepackage{xunicode}
\setsansfont{DejaVuSans}
\usepackage{polyglossia}
\setmainlanguage{greek}
\setotherlanguage{english}

\usepackage{url}
\usepackage{indentfirst}
\usepackage{float} % for images
\usepackage{color}
\usepackage{listings}
\usepackage{caption,subcaption}

\definecolor{darkgreen}{RGB}{0,163,82}
\definecolor{structure}{rgb}{0.2,0.2,0.7}
\usepackage{wrapfig}
\usepackage{listings}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{1,1,1}

\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
rulecolor=,
language=c++,
basicstyle=\scriptsize,
upquote=true,
aboveskip={1.5\baselineskip},
columns=flexible,
showstringspaces=false,
extendedchars=true,
breaklines=true,
numbers=none,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
%frame=false,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color[rgb]{0.627,0.126,0.941},
}

\newcommand{\EN}[1]{\foreignlanguage{english}{#1}}
\mode<presentation>

\begin{document}

\begin{frame}[fragile]

\frametitle{intro}
\setbeamercovered{transparent}

\noindent\begin{minipage}{.5\textwidth}
\begin{lstlisting}[caption= code 1]
int main(void) {
printf("Left\n");
return 0;
}

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.5\textwidth}

\begin{lstlisting}[caption= code 2]
int main(void) {
printf("Right\n");
return 0;

}
\end{lstlisting}
\end{minipage}

\end{frame}

\end{document}


Here is a screenshot of what I receive:

I want the title to be centered above the code.

-
Actually, it is centred. You just want it to be above the listing so that it looks like it's centred. –  Werner Sep 8 '12 at 16:49
@Werner Or maybe he wants to center the listing inside the minipage? –  Thorsten Sep 8 '12 at 16:52

You use a rather small default font size (8pt), maximize the width of the minipages (0.5\textwidth), and have fairly short line lengths for the code snippets inside the listings. These factors, combined, give the impression that the captions aren't centered when they are, in fact, perfectly centered over the respective minipages -- as @Werner has already pointed out in a comment.

The easiest fix would be to use a somewhat narrower width of, say, 0.3\textwidth for the two minipage environments. If you can make do with the normal default font size, instead of 8pt, you should probably select something like 0.35\textwidth for the widths of the two environments in order to avoid creating some unnecessary line breaks.

Addendum: Without the 8pt document class option and setting the widths of the minipages to 0.35\textwidth, and (for good measure) commenting out all package-loading code that's not needed for the example at hand, the following look results:

Hopefully, this is closer to what you're trying to achieve.

\documentclass[blue]{beamer}
%\usepackage{subfigure}

\usepackage{fontspec}
\usepackage{xunicode}
\setsansfont{DejaVuSans}
%\usepackage{polyglossia}
%\setmainlanguage{greek}
%\setotherlanguage{english}

%\usepackage{url}
%\usepackage{indentfirst}
%\usepackage{float} % for images
%\usepackage{color}
%\usepackage{listings}
%\usepackage{caption,subcaption}

\definecolor{darkgreen}{RGB}{0,163,82}
\definecolor{structure}{rgb}{0.2,0.2,0.7}
%\usepackage{wrapfig}
\usepackage{listings}
%\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{1,1,1}

\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
rulecolor=,
language=c++,
basicstyle=\scriptsize,
upquote=true,
aboveskip={1.5\baselineskip},
columns=flexible,
showstringspaces=false,
extendedchars=true,
breaklines=true,
numbers=none,
prebreak = \raisebox{0ex}[0ex][0ex]
{\ensuremath{\hookleftarrow}},
%frame=false,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color[rgb]{0.627,0.126,0.941},
}

%\newcommand{\EN}[1]{\foreignlanguage{english}{#1}}
\mode<presentation>

\begin{document}

\begin{frame}[fragile]

\frametitle{intro}
\setbeamercovered{transparent}

\noindent
\begin{minipage}{.35\textwidth}
\begin{lstlisting}[caption= code 1]
int main(void) {
printf("Left\n");
return 0;
}
\end{lstlisting}
\end{minipage}
\hfill
\begin{minipage}{.35\textwidth}
\begin{lstlisting}[caption= code 2]
int main(void) {
printf("Right\n");
return 0;

}
\end{lstlisting}
\end{minipage}
\end{frame}
\end{document}

-
:Hello , this didn't fix at all the problem. (i already tried changing the 0.5\textwidth ,but nothing works.I changed now and the font size but still the same) –  George Sep 8 '12 at 17:25
@George -- There's a separate issue with your MWE: you shouldn't be using the subfigure and subcaption packages simultaneously. (The MWE won't compile under XeLaTeX if both packages are loaded, at least not under MacTeX2012, which is what I'm running.) Have you tried dropping, say, the subcaption package? –  Mico Sep 8 '12 at 17:30
:I tested it now,but still the same.. –  George Sep 8 '12 at 17:32
@George: I've posted an image file and the associated code to illustrate the changes that result from implementing my suggestions. It's difficult for me to understand that it's "still the same" (as what you posted initially). –  Mico Sep 8 '12 at 18:04
@ Mico:It seems the removal of 8pt from \documentclass[blue]{beamer} ,does the job.But , then the entire document goes messed up.. –  George Sep 8 '12 at 18:29