0

I've modified answer of this post for new blocks as follow but when I put an displayed equation as first line it produce error Extra }, or forgotten $. ^^I\end{frame} and when I put it after an empty line the error goes away. What exactly is the problem?

What I figured out this problem is related to Copenhagen theme!

P.s. 1 Mathematical align environment produce a different error!

P.s. 2 usual block has no such problem!

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

\newenvironment<>{theorem}[2][Theorem]{% \setbeamercolor{block title}{fg=white,bg= red!90!blue}% \begin{block}{#1}#3{#2}}{\end{block}}

\begin{document}

\begin{frame} \begin{theorem} this is OK! \end{theorem}

\begin{theorem}%&lt;-- error
    \[x^2=y^2+z^2\]
\end{theorem}

\begin{theorem}%&lt;-- OK

    \[x^2=y^2+z^2\]
\end{theorem}

\begin{block}{theorem}%&lt;-- OK
    \[x^2=y^2+z^2\]
\end{block}

\end{frame}

\end{document}


My goal is to have an theorem block with one optional argument when is not empty this become the block title something like as follow:

\begin{theorem}
     \[x^2=y^2+z^2\]
\end{theorem}
\begin{theorem}[MY TITLE]
     \[x^2=y^2+z^2\]
\end{theorem}

enter image description here

C.F.G
  • 552
  • 1
    Doesn't your theorem env take two arguments on one optional and one mandatory. And you are not giving the mandatory one – daleif Mar 21 '23 at 10:14
  • I don't know what mandatory one is. I just copied it. – C.F.G Mar 21 '23 at 10:17
  • \newenvironment<>{theorem}[2][Theorem] the [2] says this takes two arguments. [Theorem] says the first is optional. – daleif Mar 21 '23 at 10:22
  • I know what you say but the linked post accept one argument without my optional one. (see linked post) – C.F.G Mar 21 '23 at 10:23
  • 1
    (1) The Copenhagen theme is not related. (2) if you use \begin{theorem}{test} the it compiles just fine. Your problem with not specifying the mandatory arg is that begin{theorem} then tries to use \[ as its argument and then math fails. You might just want to change [2] to [1] – daleif Mar 21 '23 at 10:24
  • 1
    And note how all those in the linked example used [1], where did that [2] come from? – daleif Mar 21 '23 at 10:25
  • I've added that extra option to be optional. Can one have optional argument without my [2] and just using linked post? – C.F.G Mar 21 '23 at 10:26
  • Not with that syntax it only supports a single optional argument. You should probably start over and rephrase your question to explain what your end goal is. – daleif Mar 21 '23 at 10:46

2 Answers2

3

Instead of reinventing the wheel, simply use the default theorem environment provided by beamer. It will accept an optional title just fine - no changes necessary.

\documentclass{beamer}
\usetheme{Copenhagen}

\begin{document}

\begin{frame} \begin{theorem} this is OK! \end{theorem}

\begin{theorem}
    \[x^2=y^2+z^2\]
\end{theorem}

\begin{theorem}[test]

    \[x^2=y^2+z^2\]
\end{theorem}

\end{frame}

\end{document}

If you don't like that the optional title is shown in (), you can change the theorem begin template:

\documentclass{beamer}
\usetheme{Copenhagen}

\makeatletter \setbeamertemplate{theorem begin} {% \setbeamercolor{block title}{fg=white,bg=red!90!blue}% \begin{\inserttheoremblockenv} {% \ifx\inserttheoremaddition@empty \inserttheoremname \else \inserttheoremaddition \fi% }% } \makeatother

\begin{document}

\begin{frame} \begin{theorem} this is OK! \end{theorem}

\begin{theorem}
    \[x^2=y^2+z^2\]
\end{theorem}

\begin{theorem}[test]

    \[x^2=y^2+z^2\]
\end{theorem}

\begin{block}{title}
content...
\end{block}

\end{frame}

\end{document}

enter image description here

1
\documentclass[]{beamer}
\usetheme{Copenhagen}

\newenvironment<>{theorem}[1][Theorem]{%
    \setbeamercolor{block title}{fg=white,bg= red!90!blue}%
    \begin{block}#2{#1}
}{
    \end{block}
}

\begin{document}

\begin{frame}
    \begin{theorem}<1->
         \[x^2=y^2+z^2\]
    \end{theorem}

    \begin{theorem}<2->[MY TITLE]
         \[x^2=y^2+z^2\]
    \end{theorem}
\end{frame}

\end{document}

Here you define one optional argument which has default value Theorem. If you specify another title by [MY TITLE], this one will be used.

Thanks to your comment, it now obeys overlay specifications.

Result

Οὖτις
  • 2,897
  • 1
  • 5
  • 18