# How to center blocks and columns in beamer

On a slide with two blocks aligned using columns, the spacing on the left and the right is not equal, ie the pair of columns in not centered on the page as in this example. I've tried enclosing the columns in \begin{centering} but that doesn't seem to work. How can I make the pair of columns centered?

% Preview source code

%% Do not edit unless you really know what you are doing.
\documentclass[english]{beamer}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
% this default might be overridden by plain title style
\newcommand\makebeamertitle{\frame{\maketitle}}%
\AtBeginDocument{
\let\origtableofcontents=\tableofcontents
\def\tableofcontents{\@ifnextchar[{\origtableofcontents}{\gobbletableofcontents}}
\def\gobbletableofcontents#1{\origtableofcontents}
}
\long\def\lyxframe#1{\@lyxframe#1\@lyxframestop}%
\def\@lyxframe{\@ifnextchar<{\@@lyxframe}{\@@lyxframe<*>}}%
\def\@@lyxframe<#1>{\@ifnextchar[{\@@@lyxframe<#1>}{\@@@lyxframe<#1>[]}}
\def\@@@lyxframe<#1>[{\@ifnextchar<{\@@@@@lyxframe<#1>[}{\@@@@lyxframe<#1>[<*>][}}
\def\@@@@@lyxframe<#1>[#2]{\@ifnextchar[{\@@@@lyxframe<#1>[#2]}{\@@@@lyxframe<#1>[#2][]}}
\long\def\@@@@lyxframe<#1>[#2][#3]#4\@lyxframestop#5\lyxframeend{%
\frame<#1>[#2][#3]{\frametitle{#4}#5}}
\newenvironment{topcolumns}{\begin{columns}[t]}{\end{columns}}
\def\lyxframeend{} % In case there is a superfluous frame end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usecolortheme{rose}
\useinnertheme{circles}

\makeatother

\usepackage{babel}
\begin{document}

\lyxframeend{}\lyxframe{My slide}
\begin{center}
\begin{topcolumns}%{}
\noindent

\column{0.5\textwidth}
\begin{exampleblock}
{A short list\inputencoding{latin1}{\phantom{g}}\inputencoding{latin9}}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{exampleblock}

\column{0.5\textwidth}
\begin{exampleblock}
{A long list}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\item Item 4
\item Item 5
\item Item 6
\end{itemize}
\end{exampleblock}
\end{topcolumns}%{}
\end{center}
\lyxframeend{}
\end{document}


-
You stated the \begin{centering}, it should be either \begin{center} or \centering. –  Rick de Groot Jun 15 '12 at 12:51
Fixed, thanks. Doesn't solve the issue though. –  user8786 Jun 15 '12 at 15:44

I think this is a problem of the theme. In the answer I will demonstrate that with the default theme you get a different behavior with respect to another theme.

For example:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usetheme{CambridgeUS}

\usecolortheme{rose}
\useinnertheme{circles}

\begin{document}

\begin{frame}{My slide}

\begin{columns}[T]

\begin{column}{0.45\textwidth}
\begin{exampleblock}
{A short list}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{exampleblock}
\end{column}

\begin{column}{0.45\textwidth}
\begin{exampleblock}
{A long list}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\item Item 4
\item Item 5
\item Item 6
\end{itemize}
\end{exampleblock}
\end{column}
\end{columns}

\end{frame}

\end{document}


and there is no horizontal alignment, but selecting the CambridgeUS theme with

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usetheme{CambridgeUS}
....


the result is a frame with block centered (left space before the first block is the same of the right space after the second block):

-
Thanks, that is helpful. With a bit of trial and error I found that \useinnertheme{rounded} works, but {rectangles}, {circles} and {default} don't. Will investigate futher... –  user8786 Jun 15 '12 at 16:08
I'm guessing the reason this works for {rounded} and not {default} is to be found comparing the block definitions for rounded (in beamerbaseauxtemplates.sty) and {default} (in beamerinnerthemedefault.sty). Default uses beamercolorboxes, whilst rounded uses beamerboxesrounded, there are a couple of \raggedrights in rounded but not in default and a few other differences, but I've no idea where the significant difference comes from. –  user8786 Jun 15 '12 at 16:48
Yes, I've the same opinion. Indeed if you try to adopt \setbeamertemplate{blocks}[rounded] in the default theme you will end up with blocks horizontally aligned. –  Claudio Fiandrino Jun 16 '12 at 12:35
One may define their own block structure with flushright for beamercolorbox in columns to achieve centering without using rounded theme. \setbeamertemplate{block begin}{ \begin{flushright}\begin{beamercolorbox}[leftskip=0.5cm,colsep*=.75ex]{block title}% \usebeamerfont*{block title}\insertblocktitle \end{beamercolorbox}% \usebeamerfont{block body}% \begin{beamercolorbox}[colsep*=.75ex,sep=.5ex,vmode]{block body}% \setbeamertemplate{block end}{ \end{beamercolorbox}\end{flushright} } –  Mobius Pizza Sep 21 '12 at 10:35
Did someone find a solution? I just wanted to ask exactly the same question when i found out that it would be a duplicate. Using rounded corners always centers two blocks perfectly, using the default makes them misaligned (left and right outer gap). –  bioslime Mar 18 '13 at 14:32

Well, I guess you just need to tell columns to center columns, as reported in the beamer documentation.

Instead of \begin{topcolumns} use:

\begin{columns}[onlytextwidth]
...
\end{columns}


and specify a little less width for your columns, like \columns{0.45\textwidth}

-
I think the OP means horizontally centered in the page. –  Claudio Fiandrino Jun 15 '12 at 12:13
Hopefully fixed, but I'm not sure if the space at left and right is exactly the same still ... –  EEva Jun 15 '12 at 12:16
Yes: they are still different. See my answer! :) –  Claudio Fiandrino Jun 15 '12 at 12:44

In the default template blocks are based on the beamercolorbox environment, whereas the rounded template re-defines all blocks based on the beamerboxesrounded environment. One difference between these two box environments is the way their width is defined. In case of beamercolorbox the width of the box is equal to its visual width. On the other hand, for beamerboxesrounded the width is equal to the text width inside of the box, i.e., the padding/margin between the box border and the text is not added to the total width:

\documentclass[english]{beamer}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usecolortheme{rose}
\useinnertheme{circles}
\newlength{\mylen}
\begin{document}
\begin{frame}
\begin{columns}[t]
\column{0.25\textwidth}
\column{0.5\textwidth}
\settowidth{\mylen}{%
\begin{beamercolorbox}[colsep*=.75ex]{block title example}
\end{beamercolorbox}}
\begin{beamercolorbox}[colsep*=.75ex]{block title example}
beamercolorbox
\end{beamercolorbox}{\parskip0pt\par\nointerlineskip\vskip-0.5pt}%
\begin{beamercolorbox}[colsep*=.75ex]{block body example}
width = \the\mylen
\end{beamercolorbox}
\rule[0.3\baselineskip]{\textwidth}{2pt}
\settowidth{\mylen}{%
\begin{beamerboxesrounded}[upper=block title,lower=block body]{beamerboxesrounded}
\end{beamerboxesrounded}}
\begin{beamerboxesrounded}[upper=block title example,%
lower=block body example]{beamerboxesrounded}
width = \the\mylen
\end{beamerboxesrounded}
\column{0.25\textwidth}
\end{columns}
\end{frame}
\end{document}


In the example above the width of the black rule is the columns's text width. The visual width of both example blocks is wider than the text width, but only in the case of beamercolorbox the actual width is. Exactly this is what leads to the misalignment.

Since beamercolorbox is used at many places, I would not recommend to change the behavior of that environment. You can, however, change the definition of the exampleblock (and other such blocks you use):

\documentclass[english]{beamer}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[width=14cm,height=11cm, landscape, frame, center]{crop}
\usecolortheme{rose}
\useinnertheme{circles}
% redefinition of 'example block'
\newsavebox{\squaredblocktext}
\defbeamertemplate*{block example begin}{squared}
{
\par\vskip\medskipamount%
\makebox[\dimexpr\textwidth-1.5ex\relax][l]{%
\begin{beamercolorbox}[colsep*=.75ex]{block title example}
\usebeamerfont*{block title example}\insertblocktitle%
\end{beamercolorbox}}%
\begin{lrbox}{\squaredblocktext}\begin{minipage}[t]{\textwidth}%
\ifbeamercolorempty[bg]{block body example}{\vskip-.25ex}{\vskip-.75ex}\vbox{}%
}
\defbeamertemplate*{block example end}{squared}
{
\end{minipage}\end{lrbox}%
{\parskip0pt\par}%
\ifbeamercolorempty[bg]{block title example}
{}
{\ifbeamercolorempty[bg]{block body example}{}{\nointerlineskip\vskip-0.5pt}}%
\usebeamerfont{block body example}%
\makebox[\dimexpr\textwidth-1.5ex\relax][l]{%
\begin{beamercolorbox}[colsep*=.75ex,vmode]{block body example}%
\usebox{\squaredblocktext}
\end{beamercolorbox}%
}\vskip\smallskipamount%
}
% end redefinition of 'example block'
\begin{document}
\begin{frame}{Alignment of squared example blocks}
\begin{columns}[t]
\column{0.5\textwidth}
\begin{exampleblock}{A very short list}
\begin{itemize}
\item Item 1
\end{itemize}
\end{exampleblock}
\column{0.5\textwidth}
\begin{exampleblock}{A longer list}
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
\end{exampleblock}
\end{columns}
\end{frame}
\end{document}


Note that the crop package is only loaded to draw a border around the page (so that the alignment is easier to see).

Such re-definitions are also needed for the other blocks, if you are using them.

In my opnion, a preferable solution would be to define a new environment beamerboxessquared in analogy to beamerboxesrounded to base the default blocks on. Unfortunately, my knowledge of PGF is not good enough to do that. Maybe someone else could jump in here?

-