latexbeamer

How to make a self-made environment in a beamer presentation that has its own counter?


I want to make a new environment called 'Opgave' ('Opgave' is Danish for 'Exercise') in a beamer presentation that has its own formatting. It has to have the same formatting as other environments like Definition, Example etc. except that the block header should have another colour. In the code below, the block header colour is okay, but the counter is not. I want this environment to have its own counter that follows the section numbers. Is there a way to do this?

\documentclass[9pt,trans,xcolor={table},envcountsect]{beamer} % Plain

\usefonttheme[onlymath]{serif}
\usetheme[shownavsym,right]{Aalborg}

\setbeamertemplate{section in toc}[sections numbered] % Automatic enumeration of sections
\setbeamertemplate{blocks}[rounded][shadow=true] 

\definecolor{dgreen}{rgb}{0.,0.6,0.} 
\definecolor{aaublue}{RGB}{33,26,82}% dark blue

\usepackage{etoolbox} % In order to still use an outdated beamer version

\AtBeginEnvironment{theorem}{%
\setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
\setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
\setbeamercolor{itemize item}{fg=dgreen!100!white}%
\setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
\setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
\setbeamercolor{enumerate item}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
}

\AtBeginEnvironment{lemma}{%
\setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
\setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
\setbeamercolor{itemize item}{fg=dgreen!100!white}%
\setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
\setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
\setbeamercolor{enumerate item}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
}

\AtBeginEnvironment{corollary}{%
\setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
\setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
\setbeamercolor{itemize item}{fg=dgreen!100!white}%
\setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
\setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
\setbeamercolor{enumerate item}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
\setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
}

\AtBeginEnvironment{definition}{%
\setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
\setbeamercolor{block title}{bg=blue,fg=white}%
\setbeamercolor{itemize item}{fg=blue}%
\setbeamercolor{itemize subitem}{fg=blue}%
\setbeamercolor{itemize subsubitem}{fg=blue}%
\setbeamercolor{enumerate item}{fg=blue}%
\setbeamercolor{enumerate subitem}{fg=blue}%
\setbeamercolor{enumerate subsubitem}{fg=blue}%
}

\def \exampleblockcolour {cyan}
\AtBeginEnvironment{example}{%
\setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
\setbeamercolor{block title}{bg=\exampleblockcolour,fg=white}%
\setbeamercolor{itemize item}{fg=\exampleblockcolour}%
\setbeamercolor{itemize subitem}{fg=\exampleblockcolour}%
\setbeamercolor{itemize subsubitem}{fg=\exampleblockcolour}%
\setbeamercolor{enumerate item}{fg=\exampleblockcolour}%
\setbeamercolor{enumerate subitem}{fg=\exampleblockcolour}%
\setbeamercolor{enumerate subsubitem}{fg=\exampleblockcolour}%
}

\def \roed {normal text.bg!10!red}
\def \exerciseblockcolour {\roed}
\newcounter{opgave}[part] 
\renewcommand{\theopgave}{\arabic{opgave}}
\resetcounteronoverlays{opgave}  
\newcommand{\opgave}{\refstepcounter{opgave} {Opgave \theopgave}}
\newenvironment<>{opgave}[1]{
%    \setbeamercolor{block title}{fg=white,bg=normal text.bg!10!red}
\setbeamercolor{block title}{fg=white,bg=\exerciseblockcolour}%{fg=white,bg=red!75!black}
\setbeamertemplate{itemize items}[circle]      
\begin{block}#2{\refstepcounter{opgave}Opgave \theopgave #1}}{\end{block}}
\AtBeginEnvironment{opgave}{\setbeamercolor{itemize item}{fg=\exerciseblockcolour}}         
\AtBeginEnvironment{opgave}{\setbeamercolor{itemize subitem}{fg=\exerciseblockcolour}}      
\AtBeginEnvironment{opgave}{\setbeamercolor{itemize subsubitem}{fg=\exerciseblockcolour}}   
\AtBeginEnvironment{opgave}{\setbeamercolor{enumerate item}{fg=\exerciseblockcolour}}      
\AtBeginEnvironment{opgave}{\setbeamercolor{enumerate subitem}{fg=\exerciseblockcolour}}   
\AtBeginEnvironment{opgave}{\setbeamercolor{enumerate subsubitem}{fg=\exerciseblockcolour}} 

\setbeamercolor*{block body}{bg=black!10!white,fg=black} 
\setbeamercolor*{block body alerted}{bg=normal text.bg!90!black,fg=black} 
\setbeamercolor*{block body example}{bg=normal text.bg!90!black,fg=black} 
\setbeamercolor*{block title}{parent=structure2,bg=normal text.bg!0!blue}
\setbeamercolor*{block title alerted}{use={normal text,alerted text},fg=alerted text.fg!0!white,bg=normal text.bg!10!red}
\setbeamercolor*{block title example}{use={normal text,example text},fg=white,bg=cyan}
\setbeamercolor*{block title theorem}{use={normal text,example text},fg=example text.fg!100!normal text.fg,bg=normal text.bg!0!dgreen}

\makeatletter
\pgfdeclareverticalshading[lower.bg,upper.bg]{bmb@transition}{200cm}{color(0pt)=(lower.bg); color(4pt)=(lower.bg); color(4pt)=(upper.bg)}
\makeatother

\makeatletter
\newcommand{\ShiftSectionNumber}[1]{%
\beamer@tocsectionnumber=\numexpr#1+\beamer@tocsectionnumber}
\makeatother

\setbeamertemplate{theorems}[numbered]
\addtobeamertemplate{theorem begin}{\normalfont}

\let\definition\undefined
\newtheorem{definition}{\translate{Definition}}[section]

\let\example\undefined
\newtheorem{example}{\translate{Example}}[section]

\uselanguage{danish}
\languagepath{danish}
\deftranslation[to=danish]{Example}{Eksempel}
\deftranslation[to=danish]{Theorem}{S\ae tning}
\deftranslation[to=danish]{Corollary}{Korollar}

\begin{document}

\begin{frame}%[allowframebreaks]
    \frametitle{Overview}
    \tableofcontents
\end{frame}

\ShiftSectionNumber{4}
\addtocounter{section}{4} % Enumeration of theorems and examples follows section numbers

\section{Section}
\begin{frame}[<+->]{Enumeration of self-made environment}
    \begin{definition}[Indpendent counter]
        \begin{itemize}
            \item Counter independent of all other counters: Definition 5.1
        \end{itemize}        
    \end{definition}
    \begin{example}[Indpendent counter]
        \begin{itemize}
            \item Counter independent of all other counters: Eksempel 5.1
        \end{itemize}
    \end{example}
    \begin{opgave}[Wanted: Indpendent counter]
        \begin{itemize}
            \item Wanted: Counter independent of all other counters: Opgave 5.1
        \end{itemize}
    \end{opgave}
\end{frame}

\end{document}

Solution

  • You can use \newtheorem to define new theorem-like environments:

    \documentclass[9pt,trans,xcolor={table},envcountsect]{beamer} % Plain
    
    \usefonttheme[onlymath]{serif}
    \usetheme[shownavsym,right]{Aalborg}
    
    \setbeamertemplate{section in toc}[sections numbered] % Automatic enumeration of sections
    \setbeamertemplate{blocks}[rounded][shadow=true] 
    
    \definecolor{dgreen}{rgb}{0.,0.6,0.} 
    \definecolor{aaublue}{RGB}{33,26,82}% dark blue
    
    \usepackage{etoolbox} % In order to still use an outdated beamer version
    
    \AtBeginEnvironment{theorem}{%
    \setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
    \setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
    \setbeamercolor{itemize item}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
    \setbeamercolor{enumerate item}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
    }
    
    \AtBeginEnvironment{lemma}{%
    \setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
    \setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
    \setbeamercolor{itemize item}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
    \setbeamercolor{enumerate item}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
    }
    
    \AtBeginEnvironment{corollary}{%
    \setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
    \setbeamercolor{block title}{bg=dgreen!100!white,fg=white}%
    \setbeamercolor{itemize item}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subitem}{fg=dgreen!100!white}%
    \setbeamercolor{itemize subsubitem}{fg=dgreen!100!white}%
    \setbeamercolor{enumerate item}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subitem}{fg=dgreen!100!black}%
    \setbeamercolor{enumerate subsubitem}{fg=dgreen!100!black}%
    }
    
    \AtBeginEnvironment{definition}{%
    \setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
    \setbeamercolor{block title}{bg=blue,fg=white}%
    \setbeamercolor{itemize item}{fg=blue}%
    \setbeamercolor{itemize subitem}{fg=blue}%
    \setbeamercolor{itemize subsubitem}{fg=blue}%
    \setbeamercolor{enumerate item}{fg=blue}%
    \setbeamercolor{enumerate subitem}{fg=blue}%
    \setbeamercolor{enumerate subsubitem}{fg=blue}%
    }
    
    \def \exampleblockcolour {cyan}
    \AtBeginEnvironment{example}{%
    \setbeamertemplate{itemize items}[circle]                    % Shape of bullet-points
    \setbeamercolor{block title}{bg=\exampleblockcolour,fg=white}%
    \setbeamercolor{itemize item}{fg=\exampleblockcolour}%
    \setbeamercolor{itemize subitem}{fg=\exampleblockcolour}%
    \setbeamercolor{itemize subsubitem}{fg=\exampleblockcolour}%
    \setbeamercolor{enumerate item}{fg=\exampleblockcolour}%
    \setbeamercolor{enumerate subitem}{fg=\exampleblockcolour}%
    \setbeamercolor{enumerate subsubitem}{fg=\exampleblockcolour}%
    }
    
    \def \roed {normal text.bg!10!red}
    \def \exerciseblockcolour {\roed}
    
    
    \newtheorem{opgave}{Opgave}[section]
    
    
    \AtBeginEnvironment{opgave}{
      \setbeamercolor{block title}{fg=white,bg=normal text.bg!10!red}
      \setbeamercolor{block title}{fg=white,bg=\exerciseblockcolour}
      \setbeamertemplate{itemize items}[circle]      
      \setbeamercolor{itemize item}{fg=\exerciseblockcolour}
      \setbeamercolor{itemize subitem}{fg=\exerciseblockcolour}
      \setbeamercolor{itemize subsubitem}{fg=\exerciseblockcolour}
      \setbeamercolor{enumerate item}{fg=\exerciseblockcolour}
      \setbeamercolor{enumerate subitem}{fg=\exerciseblockcolour}
      \setbeamercolor{enumerate subsubitem}{fg=\exerciseblockcolour}
    } 
    
    \setbeamercolor*{block body}{bg=black!10!white,fg=black} 
    \setbeamercolor*{block body alerted}{bg=normal text.bg!90!black,fg=black} 
    \setbeamercolor*{block body example}{bg=normal text.bg!90!black,fg=black} 
    \setbeamercolor*{block title}{parent=structure2,bg=normal text.bg!0!blue}
    \setbeamercolor*{block title alerted}{use={normal text,alerted text},fg=alerted text.fg!0!white,bg=normal text.bg!10!red}
    \setbeamercolor*{block title example}{use={normal text,example text},fg=white,bg=cyan}
    \setbeamercolor*{block title theorem}{use={normal text,example text},fg=example text.fg!100!normal text.fg,bg=normal text.bg!0!dgreen}
    
    \makeatletter
    \pgfdeclareverticalshading[lower.bg,upper.bg]{bmb@transition}{200cm}{color(0pt)=(lower.bg); color(4pt)=(lower.bg); color(4pt)=(upper.bg)}
    \makeatother
    
    \makeatletter
    \newcommand{\ShiftSectionNumber}[1]{%
    \beamer@tocsectionnumber=\numexpr#1+\beamer@tocsectionnumber}
    \makeatother
    
    \setbeamertemplate{theorems}[numbered]
    \addtobeamertemplate{theorem begin}{\normalfont}
    
    \let\definition\undefined
    \newtheorem{definition}{\translate{Definition}}[section]
    
    \let\example\undefined
    \newtheorem{example}{\translate{Example}}[section]
    
    \uselanguage{danish}
    \languagepath{danish}
    \deftranslation[to=danish]{Example}{Eksempel}
    \deftranslation[to=danish]{Theorem}{S\ae tning}
    \deftranslation[to=danish]{Corollary}{Korollar}
    
    \begin{document}
    
    \begin{frame}%[allowframebreaks]
        \frametitle{Overview}
        \tableofcontents
    \end{frame}
    
    \ShiftSectionNumber{4}
    \addtocounter{section}{4} % Enumeration of theorems and examples follows section numbers
    
    \section{Section}
    \begin{frame}[<+->]{Enumeration of self-made environment}
        \begin{definition}[Indpendent counter]
            \begin{itemize}
                \item Counter independent of all other counters: Definition 5.1
            \end{itemize}        
        \end{definition}
        \begin{example}[Indpendent counter]
            \begin{itemize}
                \item Counter independent of all other counters: Eksempel 5.1
            \end{itemize}
        \end{example}
        \begin{opgave}[Wanted: Indpendent counter]
            \begin{itemize}
                \item Wanted: Counter independent of all other counters: Opgave 5.1
            \end{itemize}
        \end{opgave}
    \end{frame}
    
    \end{document}
    

    enter image description here