Document ClassesBeamer: Block width?

Information and discussion about specific document classes and how to create your own document classes.
pebo
Posts: 3
Joined: Thu Aug 14, 2008 1:00 pm

Beamer: Block width?

Post by pebo »

Hello,
what's the best way to draw a block with some contents, as with the block environment, that does not span over the whole slide's width?
Currently I use a single column with a specified width, but I find it a little long:

Code: Select all

\begin{columns}
\column{7em}
\begin{block}{Title}Text
\end{block}
\end{columns}
Any hint is appreciated :)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Beamer: Block width?

Post by Stefan Kottwitz »

Hi Pebo,

you could define your own block environment with an optional parameter for its width. Have a look at this example:

Code: Select all

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[english]{babel}
\title{Variable block demo}
\author{Stefan Kottwitz}

\newenvironment<>{varblock}[2][\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, \textbackslash textwidth will be used
\end{varblock}
\end{frame}

\end{document}
Stefan
LaTeX.org admin
mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm

Beamer: Block width?

Post by mathfeel »

Stefan_K wrote:Hi Pebo,

you could define your own block environment with an optional parameter for its width. Have a look at this example:

Code: Select all

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[english]{babel}
\title{Variable block demo}
\author{Stefan Kottwitz}

\newenvironment<>{varblock}[2][\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, \textbackslash textwidth will be used
\end{varblock}
\end{frame}

\end{document}
Stefan
Been looking for this. Thanks. One thing: the resized block is not centered (The box is left-aligned). How do I center the box?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Beamer: Block width?

Post by Stefan Kottwitz »

You could center the box by using a center environment and a minipage environment, if needed:

Code: Select all

\begin{center}
  \begin{minipage}{4cm}
    \begin{varblock}[4cm]{title}
      text
    \end{varblock}
  \end{minipage}
\end{center}
Stefan
LaTeX.org admin
mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm

Re: Beamer: Block width?

Post by mathfeel »

Yes, but this would defeat the purpose of reusing code. Suppose I want some new environment called "cvarbox", I cannot just stick the \begin{center} and \begin{minipage} into the \newenvironment<>, can I?

I am not familiar with TeX, so I am not sure how to do this. Thanks.

[EDIT]:GOT IT. Simply inserting center/minipage in the newenvironment definition worked.
User avatar
fabriciomarques1
Posts: 1
Joined: Wed Mar 09, 2011 12:42 pm

Beamer: Block width?

Post by fabriciomarques1 »

Hi,
I achieve what you were trying to do by changing the Stefan's code as:

Code: Select all

    
\newenvironment<>{varblock}[2][\textwidth]{
    \begin{center}
      \begin{minipage}{#1}
        \setlength{\textwidth}{#1}
          \begin{actionenv}#3
            \def\insertblocktitle{#2}
            \par
            \usebeamertemplate{block begin}}
  {\par
      \usebeamertemplate{block end}
    \end{actionenv}
  \end{minipage}
\end{center}}
Fabricio
guest75

Beamer: Block width?

Post by guest75 »

Hi Stefan,

can you please explain me the environment

Code: Select all

\begin{actionenv}#3%
\end{actionenv}
you used in your code? I neither understand the explanation in the TikZ documentation nor do I see any difference if I use your code without those two lines.

Additionally, is it possible to limit the width of the block automatically to the size of (a longer) text (with some \\ in it)?

Tom
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Beamer: Block width?

Post by Stefan Kottwitz »

Hi Tom,

it's described in the beamer manual, if you follow the link it's in 9.6.3 Action Specifications. And full text search gives examples of use.

Regarding automatically choosen width, perhaps it's possible using the varwidth package.

Stefan
LaTeX.org admin
guest75

Beamer: Block width?

Post by guest75 »

Hi Stefan,

thanks, I think I understood \newenvironement<> a litte bit better.

There are still two thinks I'm not sure about

Why do you have to use \def\insertblocktitle{#2}? I read that one shouldn't use \def but \renewcommand{\insertblocktitle}{#2}.

And I'm not sure why I have to use the \par commands for a new paragraph. The output is the same without them.
Regarding automatically choosen width, perhaps it's possible using the varwidth package.
Ok, I'll try this.

Tom
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Beamer: Block width?

Post by Stefan Kottwitz »

Hi Tom,

I took it from the original block environment code which is:

Code: Select all

\newenvironment<>{block}[1]{%
  \begin{actionenv}#2%
    \def\insertblocktitle{#1}%
    \par%
    \usebeamertemplate{block begin}}
  {\par%
    \usebeamertemplate{block end}%
  \end{actionenv}}
Based on this I created the modified variable environment above. So the only reason for me was to use the same code for consistency.

Yes, LaTeX users should use \renewcommand which is higher level and safer. Package programmers use \def because it's faster. For user code, safety is more important than speed.

Stefan
LaTeX.org admin
Post Reply