0

I want to to build flexible beamer boxes so that I can change the width and alignment within the frame. Passing the options width= and alignment=, whereas width is a valid value and alignment can be left, center and right.

I have the folowing MWE, but it is not working.

\documentclass{beamer}

\usepackage{xstring}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\processlist}{ m m }
    {%
        \clist_map_inline:nn { #1 } {%
            \IfSubStr{##1}{#2}{%
                \StrBehind{##1}{=}
            }{}
        }
    }

\ExplSyntaxOff

\setbeamercolor{block title alerted}{fg=blue,bg=green}
\setbeamercolor{block body alerted}{bg=red}

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

\newcommand{\cvaralertblock}[3][0.9\textwidth]{%
    \hfill{}\vbox{%
        \begin{varalertblockA}[#1]{#2}%
            #3%
        \end{varalertblockA}%
    }\hfill{}%
}

\newenvironment<>{varalertblockB}[2][]{%
    \ifnum\pdfstrcmp{\processlist{#1}{align}}{center}=0%
    \hfill{}%
    \else%
    \fi%
    \setbox0=\hbox{\processlist{#1}{width}}\ifdim\wd0=0pt%
    \else%
        \setlength{\textwidth}{\processlist{#1}{width}}%
    \fi%
    \hfill{}%
  \begin{actionenv}#3%
    \def\insertblocktitle{#2}%
    \par%
    \setbeamercolor{local structure}{parent=alerted text}%
    \usebeamertemplate{block alerted begin}}
  {\par%
  \usebeamertemplate{block alerted end}%
  \end{actionenv}
    \ifnum\pdfstrcmp{\processlist{#1}{align}}{center}=0%
        \hfill{}%
    \else%
    \fi%
    }


\begin{document}
%\processlist{align=right,a,b,c, d z}{align}

% No working
\begin{varalertblockB}[width=2cm,align=center]{Test}
    Test
\end{varalertblockB}{Test}

\cvaralertblock[0.5\textwidth]{Test}{Test}

\begin{varalertblockA}{Test}
    Test
\end{varalertblockA}

\end{document}

1 Answer 1

3

Why reinventing the wheel? tcolorbox already provides all these features!

\documentclass{beamer}
\usepackage[most]{tcolorbox}

\newtcolorbox{myblock}[2][]{%
    beamer,breakable,colback=blue!10!white,colframe=blue!50!black,#1,title=#2
}%

\begin{document}


\begin{frame}

\begin{myblock}{test}
    bla
\end{myblock}

\begin{myblock}[width=3cm]{test}
    bla
\end{myblock}

\begin{myblock}[halign=center]{test}
    bla
\end{myblock}

\end{frame}

\end{document}

enter image description here

With the tcolorbox inner theme you can keep the normal block environment syntax:

\documentclass{beamer}
\usetheme{Warsaw}

\useinnertheme{tcolorbox}

\begin{document}
\begin{frame}

\begin{block}{test}
    bla
\end{block}

{
\tcbset{width=4cm}
\begin{block}{test}
    bla
\end{block}
}

{
\tcbset{halign=center}
\begin{block}{test}
    bla
\end{block}
}
\end{frame}
\end{document}

enter image description here

1
  • I know tcolorbox, but I wanted to use the the native blocks from beamer beamer.
    – cacamailg
    Nov 10, 2016 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .