17

I have two kinds of examples in my document. I want to use different colors, say, red and blue. This is what I am doing now.

\documentclass[]{beamer}
\usetheme{Madrid}
\begin{document}

\begin{frame}
\frametitle{Test}
\setbeamercolor{block title}{fg=white,bg=red!75!black}
\begin{block}{First Kind Example}
This is example of the first kind.
\end{block}
\setbeamercolor{block title}{fg=white,bg=blue!75!black}
\begin{block}{Second Kind Example}
This is example of the second kind.
\end{block}
\end{frame}

\end{document}

I know this is kind of stupid. I want to define two new block environments, one for each color, so I don't need to use setbeamercolor every time I use them.

\begin{block1}{First Kind Example}
...
\end{block1}
\begin{block2}{Second Kind Example}
...
\end{block2}

I know I need to define these two new environments in the preamble, but don't know how. Thanks for the help.

By the way, what is a good reference on these kinds of tricks in LaTeX/beamer? Any recommendations?

enter image description here

JACKY88
  • 2,197
  • 2
    Beamer itself defines three types of blocks: block, alertblock and exampleblock. You can customize them separately. – Claudio Fiandrino Jan 05 '14 at 18:07
  • 1
    Here is the beamer user guide http://mirrors.ctan.org/macros/latex/contrib/beamer/doc/beameruserguide.pdf – Sigur Jan 05 '14 at 18:08

3 Answers3

28

If you want to present them as examples and mark them up as such using beamer's example environment (rather than just using the generic block) you might try something like this:

\documentclass[]{beamer}
\usetheme{Madrid}

\newtheorem{examplefirst}{Example}
\newtheorem{examplesecond}{Example}
\newenvironment<>{examplefirst}[1][]{%
  \setbeamercolor{block title example}{fg=white,bg=red!75!black}%
  \begin{example}#2[#1]}{\end{example}}
\newenvironment<>{examplesecond}[1][]{%
  \setbeamercolor{block title example}{fg=white,bg=blue!75!black}%
  \begin{example}#2[#1]}{\end{example}}

\begin{document}

\begin{frame}
    \begin{examplefirst}[An example of the first kind]
        Something about this example of the first kind.
    \end{examplefirst}
    \begin{examplesecond}[An example of the second kind]
        Something about this example of the second kind.
    \end{examplesecond}
    \begin{example}[A regular example]
        Something about the regular example.
    \end{example}
\end{frame}

\end{document}

which produces:

3 kinds of example environment

If you prefer to stick to the generic block environment perhaps something like this might work:

\documentclass[]{beamer}
\usetheme{Madrid}

\newenvironment<>{examplefirst}[1]{%
  \setbeamercolor{block title}{fg=white,bg=red!75!black}%
  \begin{block}#2{#1}}{\end{block}}
\newenvironment<>{examplesecond}[1]{%
  \setbeamercolor{block title}{fg=white,bg=blue!75!black}%
  \begin{block}#2{#1}}{\end{block}}

\begin{document}

\begin{frame}
    \begin{examplefirst}{An example of the first kind}
        Something about this example of the first kind.
    \end{examplefirst}
    \begin{examplesecond}{An example of the second kind}
        Something about this example of the second kind.
    \end{examplesecond}
    \begin{block}{A regular block}
        Something in the regular block format.
    \end{block}
\end{frame}

\end{document}

enter image description here

cfr
  • 198,882
  • @cfr, Could you explain you command for new environment? For example, what are <> for? – Royi Jun 04 '17 at 22:27
  • @Royi It makes the environment overlay-specification-aware. See Beamer's manual for details - it changes \newcommand and \newenvironment and friends in this way. – cfr Jun 05 '17 at 01:02
  • 2
    May I also ask how come you use #2 while you define only 1 parameter for the environment? Thank You. – Royi Jun 05 '17 at 18:07
  • @Royi That's for the overlay specification. – cfr Jun 05 '17 at 21:12
  • What is the difference between the both solutions? – user1 Nov 05 '17 at 20:08
  • 2
    @Ben Please read my answer. I explained the difference already. – cfr Nov 07 '17 at 04:41
  • The output is the same, isn't it? Which solution should be preferred? What does "mark them up as examples" do for me? – user1 Nov 09 '17 at 16:30
  • @Ben The output is not generally the same. See the output I posted above. Of course, you may be using a theme which merges the two, so that the output is the same in your case. But that's not the default and not true for most themes. Again, look at the output I posted: it is clearly different. Exactly how the output differs depends on your choice of theme(s). – cfr Nov 11 '17 at 23:53
6

Here you have the standard code, using different colors

\documentclass{beamer}

\usecolortheme{whale}
\usecolortheme{orchid}

\begin{document}

\begin{frame}{A frame}

\begin{block}{A block environment}
Some text.
\end{block}

\begin{alertblock}{An alertblock environment}
Some text.
\end{alertblock}

\begin{exampleblock}{An exampleblock environment}
Some text.
\end{exampleblock}

\end{frame}

\end{document}

example

  • 2
    You can add the code needed for customization: it would make your answer more complete. – Claudio Fiandrino Jan 05 '14 at 18:09
  • @ClaudioFiandrino yes, but I just know their is a standard code giving the answer to his problem. But I will look further, because a this moment, I don't know yet how to change it. – Arne Timperman Jan 05 '14 at 18:11
  • 2
    Excellent! You can start with the manual, looking section 12.3 Block Environments and how to change the templates' aspect via \setbeamercolor. +1 meanwhile :) – Claudio Fiandrino Jan 05 '14 at 18:19
1

Extended versuion of @cfr's answer:

\newenvironment<>{definition}[2][DEFINITION]{%
    \setbeamercolor{block title}{fg=white,bg= green}%
    \setbeamerfont{block title example}{family=\ssfamily}
    \begin{block}{#1}#3{#2}}{\end{block}}

and use it as

\begin{definition}
some text
\end{definition}

that its title by default is: DEFINITION

or you can give your title:

\begin{definition}[YOUR TITLE]
    some text
\end{definition}

enter image description here

C.F.G
  • 552