37

I use a custom Beamer theme for my presentations. In my presentations I'd like to use blocks (\begin{block}…\end{block}). These blocks are rendered slightly too wide to fit into my theme. Is there a way how I can set the default width of blocks, e.g. to .9\textwidth?

lockstep
  • 250,273
tobyS
  • 473

3 Answers3

43

You could define your own block environment with an optional parameter for its width and provide a default value such as .9\textwidth.

Here's an example:

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[english]{babel}

\newenvironment<>{varblock}[2][.9\textwidth]{%
  \setlength{\textwidth}{#1}
  \begin{actionenv}#3%
    \def\insertblocktitle{#2}%
    \par%
    \usebeamertemplate{block begin}}
  {\par%
    \usebeamertemplate{block end}%
  \end{actionenv}}

\begin{document}

\begin{frame}
\begin{block}{Standard}
  Normal block
\end{block}
\begin{varblock}[4cm]{New block}
  Variable width, here 4cm
\end{varblock}
\begin{varblock}{New block}
  If no width was given, .9\textbackslash textwidth will be used
\end{varblock}
\end{frame}

\end{document}

variable blocks example

I posted this example here in 2008.

Stefan Kottwitz
  • 231,401
  • 1
    (+1) I initially provided a link to this thread. Definitively better that you answered yourself. – chl Mar 03 '11 at 12:56
  • @chl: thanks for the link! I noticed your comment right after posting. I just remembered that earlier thread. In addition to the older post I took the chance to create and post a screenshot. – Stefan Kottwitz Mar 03 '11 at 13:12
  • Thanks for your answer! I'll definitely remember it for when I need something like this. However, the answer which I accepted does exactly what I desired right now. – tobyS Mar 03 '11 at 14:18
  • 6
    For the sake of completeness, in order to center the shortened block, one has to surround it with \begin{cenetr}\begin{minipage}{block_width}<shortened block> \end{minipage}\end{center} – Dror Jan 27 '12 at 09:38
  • @Dror: Well, that way one can even use the regular block environment ... –  Jan 31 '13 at 09:13
  • Thats great, but can one also do it with an alertblock? I couldn't change the code, so it fit with alertblock... – Nina May 07 '14 at 08:03
  • 1
    I wonder, what is the purpose of <>in \newenvironment<>{varblock}[2][.9\textwidth]{...}? – Gilles Bonnet Sep 27 '17 at 09:35
  • @GillesBonnet It's been 6 years but I was also curious. It's probably beamer-specific thing: See the documentation' s 'beameruserguide.pdf', it 'declares a new environment that is overlay specification-aware'. – Yosh May 28 '23 at 15:09
28

If you do not require a new block environment with customizable width, but simply want to change the width of the original block environments, add the following to your preamble:

\addtobeamertemplate{block begin}{%
  \setlength{\textwidth}{0.9\textwidth}%
}{}

\addtobeamertemplate{block alerted begin}{%
  \setlength{\textwidth}{0.9\textwidth}%
}{}

\addtobeamertemplate{block example begin}{%
  \setlength{\textwidth}{0.9\textwidth}%
}{}
lockstep
  • 250,273
  • 11
    Is there a way to do this but to make the block centered? For me it sticks to the left of the page. – mrbrich Aug 26 '14 at 14:31
  • @mrbrich, try wrapping your block in a minipage environment, as suggested on https://tex.stackexchange.com/a/180462/12065. As a matter of fact, once you have your block in a minipage, it will take the size of that minipage, so you can control block size there instead of using this answer. – Waldir Leoncio Jun 16 '17 at 12:38
2

Using the code from Stefan Kottwitz and doing minor changes, I have this in my preamble.

% Variable width block
\newenvironment<>{varblock}[2][0.9\textwidth]{%
    \setlength{\textwidth}{#1}%
    \setlength{\linewidth}{\textwidth}% required to itemize respect the width of block
  \begin{actionenv}#3%
    \def\insertblocktitle{#2}%
    \par%
    \usebeamertemplate{block begin}}
  {\par%
  \usebeamertemplate{block end}%
  \end{actionenv}}

% Variable width example block
\newenvironment<>{varexampleblock}[2][0.9\textwidth]{%
    \setlength{\textwidth}{#1}%
    \setlength{\linewidth}{\textwidth}%
  \begin{actionenv}#3%
    \def\insertblocktitle{#2}%
    \par%
    \setbeamercolor{local structure}{parent=example text}%
    \usebeamertemplate{block example begin}}
  {\par%
  \usebeamertemplate{block example end}%
    \end{actionenv}}

% Variable width alert block
\newenvironment<>{varalertblock}[2][0.9\textwidth]{%
    \setlength{\textwidth}{#1}%
    \setlength{\linewidth}{\textwidth}%
  \begin{actionenv}#3%
    \def\insertblocktitle{#2}%
    \par%
    \setbeamercolor{local structure}{parent=alerted text}%
    \usebeamertemplate{block alerted begin}}
  {\par%
  \usebeamertemplate{block alerted end}%
    \end{actionenv}}
cacamailg
  • 8,405