TransWikia.com

Avoid parentheses when optional header material is not visible? (theorem environment for ams style)

TeX - LaTeX Asked on June 18, 2021

I am looking for an extension to the solved question in Avoid parentheses around optional header material in theorem environment for ams style?.

I would like to have optional header text for a theorem environment on some but not on all slides. Here is what I tried:

documentclass{beamer}

setbeamertemplate{theorems}[ams style]

newtheorem*{thm*}{Theorem}

begin{document}

begin{frame}
    begin{thm*}[only<beamer:2->{additional information}]
        My Theorem  
    end{thm*}
end{frame}

end{document}

In the above example, slide 2 looks as expected, but slide 1 prints Theorem (). Is there a way to adapt the solution here to take the overlay into account?

EDIT

I have also tried

documentclass{beamer}

setbeamertemplate{theorems}[ams style]

newtheorem*{thm*}{Theorem}

begin{document}

begin{frame}
    begin{thm*}only<beamer:2->{[additional information]}
        My Theorem  
    end{thm*}
end{frame}

end{document}

which is worse; LaTeX does not see additional information as optional header text, but as content of the environment itself.

EDIT 2

Like suggested below, I had considered deleting the parentheses from the command, then hard-coding them in each instance of an optional header of an environment. I would like to avoid doing that, since I may have many instances of such optional headers.

One Answer

Workaround: remove the parenthesis with the solution from the other question, then add them manually in the theorem (together with a space).

documentclass{beamer}

setbeamertemplate{theorems}[ams style]
makeatletter
setbeamertemplate{theorem begin}
{%
  begin{inserttheoremblockenv}
    {%
      inserttheoremheadfont%
      inserttheoremname%
      inserttheoremnumber%
      ifxinserttheoremaddition@emptyelseinserttheoremadditionfi%
      inserttheorempunctuation%
    }%
}
makeatother

newtheorem*{thm*}{Theorem}

begin{document}

begin{frame}
    begin{thm*}[only<beamer:2->{ (additional information)}]
        My Theorem  
    end{thm*}
end{frame}

end{document}

enter image description here


Edit: you can also apply the parentheses automatically using some string manipulation with the xstring package. However, this is probably less robust, so I would advise to use the manual solution above. But in case you want to try here a version using the following steps:

  1. Check if the optional argument contains only. If yes:
  2. Count the number of syntax units in inserttheoremaddition. The text of the optional argument is the last syntax unit (here: {additional information})
  3. Store that syntax unit in a temporary macro
  4. Make xstring look inside syntax units
  5. Count the number of characters in the text of the argument
  6. Extract this number of characters from the text. This effectively strips the surrounding {}.
  7. Stop looking inside the syntax units
  8. Substitute the text unit for the stripped unit with a space and parentheses added

MWE:

documentclass{beamer}
usepackage{xstring}
setbeamertemplate{theorems}[ams style]
makeatletter
setbeamertemplate{theorem begin}
{%
  begin{inserttheoremblockenv}
    {%
      inserttheoremheadfont%
      inserttheoremname%
      inserttheoremnumber%
      ifxinserttheoremaddition@emptyelse%
      IfBeginWith{inserttheoremaddition}{only}{%
      StrLen{inserttheoremaddition}[grouplen]%
      StrChar{inserttheoremaddition}{grouplen}[myarg]%
      exploregroups%
      StrLen{myarg}[arglen]%
      StrLeft{myarg}{arglen}[newarg]%
      noexploregroups%
      StrSubstitute{inserttheoremaddition}{myarg}{{ (newarg)}}%
      }%
      { (inserttheoremaddition)}fi%
      inserttheorempunctuation%
    }%
}
makeatother

newtheorem*{thm*}{Theorem}

begin{document}

begin{frame}
    begin{thm*}[only<beamer:2->{additional information}]
        My Theorem  
    end{thm*}
    begin{thm*}[Header]
        My Theorem  
    end{thm*}

end{frame}

end{document}

Result:

enter image description here

Correct answer by Marijn on June 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP