[Tex/LaTex] center the listings title above the code

listings

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

\documentclass[8pt,blue]{beamer}

\setbeamertemplate{navigation symbols}{}
\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},
      commentstyle=\color[rgb]{0.133,0.545,0.133},
      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}
int main(void) {
    printf("Left\n");
    return 0;
}

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

\begin{lstlisting}
int main(void) {
    printf("Right\n");
    return 0;

}
\end{lstlisting}
\end{minipage}

\end{frame}


\end{document}

Here is a screenshot of what I receive:

enter image description here

I want the title to be centered above the code.

Best Answer

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:

enter image description here

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

\documentclass[blue]{beamer}
\setbeamertemplate{navigation symbols}{}
%\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},
      commentstyle=\color[rgb]{0.133,0.545,0.133},
      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}