3

I have two examples in Beamer, Example 1 and Example 2. I then would like to have Example 1 (ctd) after Example 2. The following is not working:

\documentclass[notheorems]{beamer}
\usetheme{Pittsburgh}
\setbeamertemplate{theorem}[ams style]
\setbeamertemplate{theorems}[numbered]

\makeatletter
\ifbeamer@countsect
  \newtheorem{theorem}{\translate{Theorem}}[section]
\else
  \newtheorem{theorem}{\translate{Theorem}}
\fi 

\theoremstyle{example}
\newtheorem{example}{\translate{Example}}  

\setbeamertemplate{theorem begin}{%
\begin{center}
\begin{minipage}{0.9\textwidth}
\begin{\inserttheoremblockenv}
{%
  \inserttheoremname
  \inserttheoremnumber
  \ifx\inserttheoremaddition\@empty\else\ (\inserttheoremaddition)\fi%
}%
}

\setbeamertemplate{theorem end}{%
\end{\inserttheoremblockenv}
\end{minipage}
\end{center}
}
\makeatother

\addtobeamertemplate{theorem begin}{%
\setbeamercolor{block title}{fg=green}%
\setbeamercolor{block body}{bg=gray}%
\setbeamerfont{block title}{size=\small,series=\bfseries}%
}{}

\newtheoremstyle{examplec}
{\topsep} {\topsep}%
{\upshape}% Body font
{}% Indent amount (empty = no indent, \parindent = para indent)
{\bfseries\scshape}% Thm head font
{.}% Punctuation after thm head
{1em}% Space after thm head (\newline = linebreak)
{\thmname{#1} \thmnumber{ #2}\thmnote{#3} (ctd)}% Thm head spec
\theoremstyle{examplec}
\newtheorem*{examplec}{\translate{Example}}

\begin{document}
\begin{frame}
\begin{example}
This is the first example.\label{myexample}
\end{example}

\begin{example}
 This is the second example.
\end{example}

\begin{examplec}[\ref{myexample}]
 Continuing first example.
\end{examplec}

\end{frame}
\end{document}

This will produce the following:

enter image description here

What I want is: instead of Example (1), having Example 1 (ctd).

I noticed the solution at Continuing example Counters in Beamer but it does not seem to work if I keep my configurations in the preamble (which is why MWE is a bit long) -- I want to keep the (ctd) part.

Your help is much appreciated. Thanks!

  • I didn't understand what you want, can you add little freehand diagrams that illustrate what you want to get? – AndréC Aug 25 '18 at 15:50
  • Hi @AndréC, thanks for the reply. I have added more details in the question. Please let me know if it is still unclear. – Daryl Aug 25 '18 at 15:58
  • @Daryl Which of the answers in the linked thread did you use? – user36296 Aug 25 '18 at 16:32
  • If I understand correctly, you want your counter to have an optional argument from which the numbering continues. Here you want the counter to count from the number displayed at the {myexample} label? – AndréC Aug 25 '18 at 16:43
  • @samcarter There are three answers there. I tried the second one. I want to be able to continue the example anywhere in the text not just right after the example to be continued, so the first answer does not seem to be suitable. The third one configures both the continuing example and the example to be continued which I personally don't like -- I would like to have the optional of writing examples and then continue these examples only if later on deemed necessary. – Daryl Aug 25 '18 at 16:57
1

Instead of fiddling with the appearance of your new theorem, it is maybe easier to define a new environment which adjusts the counter and uses the existing example.

\documentclass[notheorems]{beamer}
\usetheme{Pittsburgh}

\setbeamertemplate{theorem}[ams style]
\setbeamertemplate{theorems}[numbered]

\makeatletter
\ifbeamer@countsect
  \newtheorem{theorem}{\translate{Theorem}}[section]
\else
  \newtheorem{theorem}{\translate{Theorem}}
\fi 

\theoremstyle{example}
\newtheorem{example}{\translate{Example}}  

\setbeamertemplate{theorem begin}{%
\begin{center}
\begin{minipage}{0.9\textwidth}
\begin{\inserttheoremblockenv}
{%
  \inserttheoremname
  \inserttheoremnumber
  \ifx\inserttheoremaddition\@empty\else\ (\inserttheoremaddition)\fi%
}%
}

\setbeamertemplate{theorem end}{%
\end{\inserttheoremblockenv}
\end{minipage}
\end{center}
}
\makeatother

\addtobeamertemplate{theorem begin}{%
\setbeamercolor{block title}{fg=green}%
\setbeamercolor{block body}{bg=gray}%
\setbeamerfont{block title}{size=\small,series=\bfseries}%
}{}

\usepackage{refcount}
\setrefcountdefault{1}

\newcounter{saveexample}

\newenvironment{examplec}[1][]{%
    \setcounter{saveexample}{\value{example}}%
    \setcounterref{example}{#1}%
    \addtocounter{example}{-1}%
    \begin{example}[cont.]}{\end{example}%
    \setcounter{example}{\thesaveexample}%
}


\begin{document}
\begin{frame}
\begin{example}
This is the first example.\label{myexample}
\end{example}

\begin{example}
 This is the second example.
\end{example}
\begin{example}
 This is the second example.
\end{example}

\begin{examplec}[myexample]
 Continuing first example.
\end{examplec}

\begin{example}
 This is the second example.
\end{example}

\end{frame}
\end{document}

enter image description here

  • It works! Thanks!! One minor thing though: when writing the continuing example, instead making reference by "[myexample]", it is possible to change it to "[\ref{myexample}]". That way, when I type the first few letters of the label, WinEdt will suggest a list of labels to choose, making things a little easier especially when working with long beamers. Thank you:-) – Daryl Aug 25 '18 at 18:01
  • @Daryl I don't think this is a good idea, it would break as soon as you activate your section-wise numbering. Better configure you editor to give you the label list also for the examplec environment. – user36296 Aug 25 '18 at 18:09
  • Thank you! Do you happen to know how to do the configuration in WinEdt as well? And btw, I don't use section-wise numbering. – Daryl Aug 25 '18 at 18:10
  • @Daryl No, but for texstudio I could tell you :) – user36296 Aug 25 '18 at 18:11
  • @Daryl I assumed from the \ifbeamer@countsect \newtheorem{theorem}{\translate{Theorem}}[section] \else \newtheorem{theorem}{\translate{Theorem}} \fi that section-wise numbering should be an option. – user36296 Aug 25 '18 at 18:12
1

As "adjusting counters" is dangerous, I do not recommend doing so. ("adjusting counters" may lead to breaking hyperref-features in subtle ways which have to do with not having ensured uniqueness of anchor names any more.)

Be aware that

  • your examplec-theorem-environment is not numbered.

  • \inserttheoremname denotes the name of the environment;
    \inserttheoremaddition denotes the optional argument of the environment;
    \inserttheoremnumber for numbered theorem-environments denotes the number of the theorem in question, and for unnumbered theorems is always empty.

Let's look in your theorem begin-template at the lines:

\inserttheoremname
\inserttheoremnumber
\ifx\inserttheoremaddition\@empty\else\ (\inserttheoremaddition)\fi%
  • with the unnumbered examplec-theorem-environment, \inserttheoremnumber is always empty.
  • with the unnumbered examplec-theorem-environment, \inserttheoremaddition denotes the environment's optional argument, which is the \ref-command for referencing the label of the environment that is to be continued, provided without the (ctd)-phrase.

Thus these lines should be something like:

\inserttheoremname~%
\ifx\inserttheoremnumber\@empty
   \inserttheoremaddition~(ctd)%
\else
  \inserttheoremnumber~\inserttheoremaddition
\fi

Caveat with this approach:

If you provide \ref{myexample} as optional argument to the examplec-environment, \inserttheoremaddition will expand to exactly this token-sequence.

The \ref within that token-sequence will yield a hyperlink and your examplec-headline will be of pattern
Example ⟨hyperlink; target:=Example 1; text-phrase:=1(ctd)

I think, the pattern
⟨hyperlink; target:=Example 1; text-phrase:=Example 1 (ctd)
is preferable.

Therefore in the example below I have inserted a bunch of expandable code for checking whether the optional argument is of pattern \ref{<name of label>}.

If so, <name of label> is extracted and the hyperlink is created according to the preferable pattern.

This pattern-checking does also address the requirement of marking references via the \ref-command stated by you in your subsequent question of subject Making reference using \ref{name} rather than directly name.

Another question is:

What behavior do you wish in case of continuing not the example but the continuation thereof? (Continuation of continuation of...)

Currently each continuation takes the number provided via the \ref-command in the optional argument as \@currentlabel and places its own \phantomtarget.

Thus each continuation gets the number of the thing that was continued and its heading will link to the \phantomtarget placed by the thing that was continued.

\documentclass[notheorems]{beamer}
\usepackage{refcount}%
\usetheme{Pittsburgh}
\setbeamertemplate{theorem}[ams style]
\setbeamertemplate{theorems}[numbered]

\makeatletter

%%=============================================================================
%%    \UD@firstoftwo, \UD@secondoftwo, \UD@Exchange, \UD@gobbletwo
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@Exchange[2]{#2#1}%
\newcommand\UD@gobbletwo[2]{}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether brace-balanced argument starts with the 
%% control word token \ref
%%.............................................................................
%% \UD@CheckWhetherLeadingRef{<Argument which is to be checked>}%
%%                           {<Tokens to be delivered in case <argument
%%                             which is to be checked>'s 1st token is 
%%                             the control word token \ref >}%
%%                           {<Tokens to be delivered in case <argument
%%                             which is to be checked>'s 1st token is not
%%                             the control word token \ref >}%
\newcommand\UD@CheckWhetherLeadingRef[1]{%
  \romannumeral0\UD@CheckWhetherNull{#1}%
  {\expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo}%
  {\expandafter\UD@secondoftwo\string{\UD@CheckWhetherLeadingRefB.#1\ref}{}}%
}%
\newcommand\UD@CheckWhetherLeadingRefB{}%
\long\def\UD@CheckWhetherLeadingRefB#1\ref{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@secondoftwo#1{}}%
  {\UD@Exchange{\UD@firstoftwo}}{\UD@Exchange{\UD@secondoftwo}}%
  {\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
   \expandafter\expandafter\expandafter}\expandafter\expandafter
   \expandafter}\expandafter\UD@secondoftwo\expandafter{\string}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument's first token is a catcode-1-character
%%.............................................................................
%% \UD@CheckWhetherBrace{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has leading
%%                        catcode-1-token>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has no leading
%%                        catcode-1-token>}%
\newcommand\UD@CheckWhetherBrace[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\expandafter{\expandafter{%
  \string#1.}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@firstoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether brace-balanced argument is of pattern \ref{}
%%-----------------------------------------------------------------------------
\newcommand\UD@CheckWhetherrefpattern[1]{%
  \romannumeral0%
  \UD@CheckWhetherLeadingRef{#1}{%
    \expandafter\UD@CheckWhetherBrace\expandafter{\UD@gobbletwo{}#1}{%
      \expandafter\UD@CheckWhetherNull\expandafter{\UD@gobbletwo#1}{%
        \expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo
      }{%
        \expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo
      }%
    }{%
      \expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo
    }%
  }{%
    \expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo
  }%
}%
%%=============================================================================
\newcounter{phantomtarget}%
\renewcommand*{\thephantomtarget}{phantom.\the\value{phantomtarget}}%
\newcommand*{\phantomtarget}{%
  \stepcounter{phantomtarget}%
  \hypertarget{\thephantomtarget}{}%
  \edef\@currentHref{\thephantomtarget}%
}%
%%=============================================================================


\ifbeamer@countsect
  \newtheorem{theorem}{\translate{Theorem}}[section]%
\else
  \newtheorem{theorem}{\translate{Theorem}}%
\fi 

\theoremstyle{example}
\newtheorem{example}{\translate{Example}}  

\setbeamertemplate{theorem begin}{%
  \begin{center}%
  \begin{minipage}{0.9\textwidth}%
  \expandafter\UD@CheckWhetherNull\expandafter{\inserttheoremnumber}{%
     \expandafter\UD@CheckWhetherrefpattern\expandafter{\inserttheoremaddition}{%
       \edef\@currentlabel{%
         \getrefbykeydefault{\expandafter\UD@secondoftwo\inserttheoremaddition}%
                            {}%
                            {??}%
       }%
       \refused{\expandafter\UD@secondoftwo\inserttheoremaddition}%
       \phantomtarget%\phantomsection
     }{}%
  }{}%
  \begin{\inserttheoremblockenv}{%
    % Your examplec-theorem is not numbered, thus \inserttheoremnumber
    % will be empty
    \expandafter\UD@CheckWhetherNull\expandafter{\inserttheoremnumber}{%
       \expandafter\UD@CheckWhetherrefpattern\expandafter{\inserttheoremaddition}{%
           \hyperref[{\expandafter\UD@secondoftwo\inserttheoremaddition}]{%
             \inserttheoremname~\ref*{\expandafter\UD@secondoftwo\inserttheoremaddition}~(ctd)%
           }%
       }{%
        \inserttheoremname~\inserttheoremaddition~(ctd)%
      }%
    }{%
      \inserttheoremname~\inserttheoremnumber~\inserttheoremaddition
    }%
  }%
}%

\setbeamertemplate{theorem end}{%
  \end{\inserttheoremblockenv}%
  \end{minipage}%
  \end{center}%
}%
\makeatother

\addtobeamertemplate{theorem begin}{%
  %% Do this in case you wish examples to be of equal color as examples:
  %\setbeamercolor{block title}{parent=example text}%
  %% Do this in case you wish examples to be of some light green:
  \setbeamercolor{block title}{fg=green}%
  %%
  \setbeamercolor{block body}{bg=gray}%
  \setbeamerfont{block title}{size=\small,series=\bfseries}%
}{}

\newtheoremstyle{examplec}
{\topsep} {\topsep}%
{\upshape}% Body font
{}% Indent amount (empty = no indent, \parindent = para indent)
{\bfseries\scshape}% Thm head font
{.}% Punctuation after thm head
{1em}% Space after thm head (\newline = linebreak)
{\thmname{#1} \thmnumber{#2}\thmnote{#3} (ctd)}% Thm head spec
\theoremstyle{examplec}
\newtheorem*{examplec}{\translate{Example}}

\begin{document}
\begin{frame}
\begin{example}
This is the first example.\label{example1}
\end{example}

\begin{example}
 This is the second example.\label{example2}
\end{example}
\end{frame}

\begin{frame}
\begin{examplec}[\ref{example1}]
 Continuing first example.\label{example1c}
\end{examplec}
\end{frame}

\begin{frame}
\begin{examplec}[\ref{example1c}]
 Continuing first example.\label{example1c1}
\end{examplec}
\end{frame}


\end{document}

Page 1:

enter image description here

Page 2:

enter image description here

Page 3:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.