LaTeX forum ⇒ 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?

Postby pebo » Mon Aug 25, 2008 8:19 pm

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:
  1. \begin{columns}
  2. \column{7em}
  3. \begin{block}{Title}Text
  4. \end{block}
  5. \end{columns}

Any hint is appreciated :)

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7801
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Tue Aug 26, 2008 8:15 pm

Hi Pebo,

you could define your own block environment with an optional parameter for its width. Have a look at this example:
  1. \documentclass{beamer}
  2. \usetheme{Warsaw}
  3. \usepackage[english]{babel}
  4. \title{Variable block demo}
  5. \author{Stefan Kottwitz}
  6.  
  7. \newenvironment<>{varblock}[2][\textwidth]{%
  8. \setlength{\textwidth}{#1}
  9. \begin{actionenv}#3%
  10. \def\insertblocktitle{#2}%
  11. \par%
  12. \usebeamertemplate{block begin}}
  13. {\par%
  14. \usebeamertemplate{block end}%
  15. \end{actionenv}}
  16.  
  17. \begin{document}
  18.  
  19. \begin{frame}
  20. \begin{block}{Standard}
  21. Normal block
  22. \end{block}
  23. \begin{varblock}[4cm]{New block}
  24. Variable width, here 4cm
  25. \end{varblock}
  26. \begin{varblock}{New block}
  27. If no width was given, \textbackslash textwidth will be used
  28. \end{varblock}
  29. \end{frame}
  30.  
  31. \end{document}

Stefan
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links

mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm

Postby mathfeel » Mon Jun 29, 2009 9:17 am

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:
  1. \documentclass{beamer}
  2. \usetheme{Warsaw}
  3. \usepackage[english]{babel}
  4. \title{Variable block demo}
  5. \author{Stefan Kottwitz}
  6.  
  7. \newenvironment<>{varblock}[2][\textwidth]{%
  8. \setlength{\textwidth}{#1}
  9. \begin{actionenv}#3%
  10. \def\insertblocktitle{#2}%
  11. \par%
  12. \usebeamertemplate{block begin}}
  13. {\par%
  14. \usebeamertemplate{block end}%
  15. \end{actionenv}}
  16.  
  17. \begin{document}
  18.  
  19. \begin{frame}
  20. \begin{block}{Standard}
  21. Normal block
  22. \end{block}
  23. \begin{varblock}[4cm]{New block}
  24. Variable width, here 4cm
  25. \end{varblock}
  26. \begin{varblock}{New block}
  27. If no width was given, \textbackslash textwidth will be used
  28. \end{varblock}
  29. \end{frame}
  30.  
  31. \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?

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7801
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Mon Jun 29, 2009 8:51 pm

You could center the box by using a center environment and a minipage environment, if needed:
  1. \begin{center}
  2. \begin{minipage}{4cm}
  3. \begin{varblock}[4cm]{title}
  4. text
  5. \end{varblock}
  6. \end{minipage}
  7. \end{center}

Stefan
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links

mathfeel
Posts: 11
Joined: Fri Jun 13, 2008 1:17 pm

Postby mathfeel » Mon Jun 29, 2009 11:04 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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
fabriciomarques1
Posts: 1
Joined: Wed Mar 09, 2011 12:42 pm
Location: São Paulo, Brazil

Postby fabriciomarques1 » Wed Mar 09, 2011 12:53 pm

Hi,
I achieve what you were trying to do by changing the Stefan's code as:
  1.  
  2. \newenvironment<>{varblock}[2][\textwidth]{
  3. \begin{center}
  4. \begin{minipage}{#1}
  5. \setlength{\textwidth}{#1}
  6. \begin{actionenv}#3
  7. \def\insertblocktitle{#2}
  8. \par
  9. \usebeamertemplate{block begin}}
  10. {\par
  11. \usebeamertemplate{block end}
  12. \end{actionenv}
  13. \end{minipage}
  14. \end{center}}
Fabricio

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby tom75 » Fri Jan 02, 2015 10:31 pm

Hi Stefan,

can you please explain me the environment

  1. \begin{actionenv}#3%
  2. \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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7801
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Jan 04, 2015 2:07 pm

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
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby tom75 » Thu Jan 08, 2015 12:13 pm

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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7801
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Jan 08, 2015 12:23 pm

Hi Tom,

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

  1. \newenvironment<>{block}[1]{%
  2. \begin{actionenv}#2%
  3. \def\insertblocktitle{#1}%
  4. \par%
  5. \usebeamertemplate{block begin}}
  6. {\par%
  7. \usebeamertemplate{block end}%
  8. \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
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Document Classes”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest