Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The question is, how to horizontally center a beamercolorbox on the page?

I put it in the center environment, but that doesn't work.

\setbeamercolor{bgcolor}{fg=black,bg=blue}
\begin{center}
    \begin{beamercolorbox}[rounded=true, center=true, shadow=true, wd=6cm]{bgcolor}
        Some text goes here...
    \end{beamercolorbox}
\end{center}
share|improve this question
1  
Glad you liked my answer. Next time it might be better to hold off clicking the check mark in case someone else comes by with a better one. I usually give it a day so I can determine which of several answers is best. –  Matthew Leingang Dec 2 '10 at 1:06
    
@Matthew: I was so excited with you clever and succinct answer that I couldn't help but to accept it! Yet your advice is wise and I'll stick to it next time. –  Sadeq Dousti Dec 2 '10 at 1:10

3 Answers 3

up vote 8 down vote accepted

wd=<dimen> not only sets the width of the beamercolorbox to <dimen>, it sets the \hsize to <dimen>. So you're centering a box in a line which has the same width as the box, resulting in nothing.

So to force centering, use some \hfills:

\documentclass{beamer}
\setbeamercolor{bgcolor}{fg=black,bg=blue}

\begin{document}

\begin{frame}
\hfill
    \begin{beamercolorbox}[rounded=true, center, shadow=true,wd=6cm]{bgcolor}
        Some text goes here...
    \end{beamercolorbox}
\hfill\hfill
\end{frame}

\end{document}

I'm not sure why you need two at the end—it seems like one at the front and back would do the centering. But instead you just get a box flush right. So an extra one does the trick.

share|improve this answer
    
Well, you are more creative than me. I used one \hfill before and another after beamercolorbox, and got the flush right effect. I'd never imagine of using two \hfill! –  Sadeq Dousti Dec 2 '10 at 1:05
3  
@Sadeq @Matthew: \hspace*{\fill} at the end of the frame forces centering. –  Stefan Kottwitz Dec 2 '10 at 1:11
    
@Stefan: Thanks! –  Sadeq Dousti Dec 2 '10 at 1:19
1  
\hfill followed by \par (or equivalent) does nothing. Doubling the \hfill means that the first is no longer followed by \par so it does something. The second does nothing as it is followed by (something equivalent to) \par. So \hfill{} would work just as well. –  Loop Space Dec 20 '10 at 10:55
1  
That ought to be a slogan for something! It's almost worth setting up a site just so that the tag-line could be "\par eats glue". –  Loop Space Dec 20 '10 at 14:18

Here's an answer that keeps the center environment:

\documentclass{beamer}
\setbeamercolor{bgcolor}{fg=black,bg=blue}

\begin{document}

\begin{frame}

\begin{center}
~
    \begin{beamercolorbox}[rounded=true, center, shadow=true,wd=6cm]{bgcolor}
        Some text goes here...
    \end{beamercolorbox}
~
\end{center}
\end{frame}

\end{document}

Matthew's answer gave me the hint. The trick is to ensure that the colour box is not the only thing on the line that is being centred. Then it isn't treated as a box by itself, but as a component of something larger. So then the search is on for something to put on the line that won't be seen in the final output. If it has non-zero width, then it needs to be put before and after to ensure that the centring is still central. A non-breaking space (~) seemed the simplest thing to do.

share|improve this answer

A simple solution, which is slightly less messy for my taste, is to create a single column, which is automatically centered:

\begin{frame}
\begin{columns}
\column{.7\linewidth}
  \begin{beamercolorbox}[rounded=true, center, shadow=true]{bgcolor}
    Some text goes here...
  \end{beamercolorbox}
\end{columns}
\end{frame}

Experience shows that one needs at least one more column eventually, anyway ;)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.