## LaTeX forum ⇒ Document Classes ⇒ Beamer: 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?

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:
\begin{columns}\column{7em}\begin{block}{Title}Text\end{block}\end{columns}

Any hint is appreciated

Tags:

Stefan Kottwitz
Posts: 8074
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
Hi Pebo,

you could define your own block environment with an optional parameter for its width. Have a look at this example:
\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

mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm
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:
\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?

Stefan Kottwitz
Posts: 8074
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
You could center the box by using a center environment and a minipage environment, if needed:
\begin{center}   \begin{minipage}{4cm}     \begin{varblock}[4cm]{title}       text     \end{varblock}   \end{minipage}\end{center}

Stefan

mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm
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.

fabriciomarques1
Posts: 1
Joined: Wed Mar 09, 2011 12:42 pm
Location: São Paulo, Brazil
Hi,
I achieve what you were trying to do by changing the Stefan's code as:
 \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

tom75
Posts: 5
Joined: Sat Dec 27, 2014 11:41 am
Hi Stefan,

can you please explain me the environment

\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

Stefan Kottwitz
Posts: 8074
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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

tom75
Posts: 5
Joined: Sat Dec 27, 2014 11:41 am
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

Stefan Kottwitz
Posts: 8074
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
Hi Tom,

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

\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