2

I am trying to define a block with customizable colors. I want to put this in a file of common definitions (say common_defs.tex) and include it in each presentation and set its title and text background and foreground colors based on the theme of that presentation. Using the suggestion here I define the following in the common_defs.tex file

\newenvironment{varblock}[3]{%
  \setbeamercolor{block title}{#2}
  \setbeamercolor{block body}{#3}
  \begin{block}{#1}}{\end{block}}

Now in the main beamer presentation I have the following:

\documentclass{beamer}

\setbeamercolor{varblock text}{bg=red!20, fg=black}
\setbeamercolor{varblock title}{bg=red, fg=white}

\begin{document}

\begin{frame}
   \frametitle{Test}

\begin{varblock}{Block Title}
Some block text
\end{varblock}

\end{frame}
\end{document}

This, understandably gives an error since it expects three arguments to \begin{varblock}. How can I change the definition to make this work?

1 Answer 1

7
+50

Here is a solution using a macro \prevarblock that you can overwrite.

main.tex

\documentclass{beamer}
\input{common}
\renewcommand{\prevarblock}{% overwrite the maco as needed
  \setbeamercolor{block title}{bg=red!20, fg=black}%
  \setbeamercolor{block body}{bg=red, fg=white}%
}
\begin{document}
  \begin{frame}\frametitle{Test}
    \begin{varblock}{Block Title} % demo varblock
      Some block text
    \end{varblock}
  \end{frame}
\end{document}

common.tex

\let\prevarblock\empty % dummy macro to overwrite
\newenvironment<>{varblock}[1]{%
  \prevarblock% call the macro within every varblock
  \begin{block}#2{#1}}{\end{block}}
4
  • Perfect -- many thanks. Worked like a charm. Learn something new every day.
    – ozsu
    Commented Apr 5, 2021 at 17:45
  • Small suggestion: with \newenvironment<>{varblock}[1]{% \prevarblock% call the macro within every varblock \begin{block}#2{#1}}{\end{block}} you would retain the ability to use overlays for the blocks. Commented Jul 15 at 11:25
  • Thanks! Now I learned something new today :) Commented Jul 15 at 11:35
  • @jessexknight You're welcome! Commented Jul 15 at 11:38

You must log in to answer this question.

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