Presentations and PostersCustomized frame title with examples and how to reference those examples?

Beamer, Powerdot and KOMA-Script presentations, Conference posters (a0poster, baposter, tikzposter)
Post Reply
CMP
Posts: 13
Joined: Thu Aug 22, 2019 8:33 pm

Customized frame title with examples and how to reference those examples?

Post by CMP »

I am trying to create examples on the LaTeX Beamer frame title, count them and reference them in the other frame title. By googling, I was able to count the examples but not able to reference it in the next slide in the title.

So, I am wondering if there is any sort of idea to achieve this goal.

Here is the minimal working example and the issue is mentioned on the last slide, too.

I also post the same question here,
https://tex.stackexchange.com/questions ... e-examples.

\documentclass[xcolor=dvipsnames]{beamer}
\usepackage[T1]{fontenc}
\usetheme{AnnArbor}
\usecolortheme{whale}
\setbeamercolor{title}{bg=RoyalPurple}
\setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}

\usepackage{amsmath}

\newcounter{exm}
\newcommand\ExmT{%
\frametitle{\refstepcounter{exm}{Example}~\theexm}}
\resetcounteronoverlays{exm}

\begin{document}

\frame{
\ExmT
David Hilbert is a great Mathematician!
}

\frame{
\ExmT
Bernhard Riemann is another greatest mathematician in the history!

}

\frame{{How do you define Mathematics?}

Mathematics is a language of sciences. Moreover, $\cdots$

}

\frame{{Rmarks on Example 1}

{\color{red}\textbf{I am writing Remarks on Example 1 (on the title of this frame) manually but is there a way to reference Example 1 automatically?}} Not the frame number.
}


\end{document}


Thanks to the LaTeX community.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Customized frame title with examples and how to reference those examples?

Post by Ijon Tichy »

To reference the example number you need to put a label inside the group of the corresponding \refstepcounter. This can be done using an optional argument to \ExmT. To reference not only the number but the title, you need to setup \@currentlabelname before the \label.
\documentclass[xcolor=dvipsnames]{beamer}
\usepackage[T1]{fontenc}
\usetheme{AnnArbor}
\usecolortheme{whale}
\setbeamercolor{title}{bg=RoyalPurple}
\setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}

\usepackage{amsmath}

\newcounter{exm}
\makeatletter
\newcommand\ExmT[1][]{%
  \frametitle{%
    \refstepcounter{exm}%
    \ifx\relax#1\relax\else
      \edef\@currentlabelname{Example~\theexm}%
      \label{#1}%
    \fi
    Example~\theexm}%
}
\makeatother
\resetcounteronoverlays{exm}

\begin{document}

\frame{%
  \frametitle{Testframe}
  To avoid that exmaples and frames have the same number.
}

\frame{
\ExmT[ex:hilbert]%
David Hilbert is a great Mathematician!
}

\frame{
\ExmT[ex:riemann]%
Bernhard Riemann is another greatest mathematician in the history!

}

\frame{{How do you define Mathematics?}

Mathematics is a language of sciences. Moreover, $\cdots$

}

\frame{{Rmarks on \nameref{ex:hilbert}}

{\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
    page~\pageref{ex:hilbert} (on the title of this frame) manually but is there a way to reference \nameref{ex:hilbert} automatically?}} Not the frame number.
}


\end{document}
But IMHO it would be better to do the \refstepcounter outside \frametitle. In this case, you can add the \label behind the \ExmT command as usual:
\documentclass[xcolor=dvipsnames]{beamer}
\usepackage[T1]{fontenc}
\usetheme{AnnArbor}
\usecolortheme{whale}
\setbeamercolor{title}{bg=RoyalPurple}
\setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}

\usepackage{amsmath}

\newcounter{exm}
\makeatletter
\newcommand\ExmT{%
  \refstepcounter{exm}%
  \protected@edef\@currentlabelname{Example\protect~\theexm}%
  \frametitle{%
    Example~\theexm}%
}
\makeatother
\resetcounteronoverlays{exm}

\begin{document}

\frame{%
  \frametitle{Testframe}
  To avoid that exmaples and frames have the same number.
}

\frame{
\ExmT\label{ex:hilbert}%
David Hilbert is a great Mathematician!
}

\frame{
\ExmT\label{ex:riemann}%
Bernhard Riemann is another greatest mathematician in the history!

}

\frame{{How do you define Mathematics?}

Mathematics is a language of sciences. Moreover, $\cdots$

}

\frame{{Rmarks on \nameref{ex:hilbert}}

{\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
    page~\pageref{ex:hilbert} (on the title of this frame) manually but is there a way to reference \nameref{ex:hilbert} automatically?}} Not the frame number.
}


\end{document}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
CMP
Posts: 13
Joined: Thu Aug 22, 2019 8:33 pm

Customized frame title with examples and how to reference those examples?

Post by CMP »

It works perfectly well to my need.

Thank you very much, Ijon Tichy!
CMP
Posts: 13
Joined: Thu Aug 22, 2019 8:33 pm

Customized frame title with examples and how to reference those examples?

Post by CMP »

It works perfectly well to my need.

Thank you very much, Ijon Tichy!
CMP
Posts: 13
Joined: Thu Aug 22, 2019 8:33 pm

Customized frame title with examples and how to reference those examples?

Post by CMP »

I am also wondering whether we could assign hyperref with some specified color?

Thank you!
CMP
Posts: 13
Joined: Thu Aug 22, 2019 8:33 pm

Customized frame title with examples and how to reference those examples?

Post by CMP »

I am also wondering how to adjust the example with title. For example,

Example 17: Who is Hilbert?

on the frame title.Ijon Tichy
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Customized frame title with examples and how to reference those examples?

Post by Ijon Tichy »

In this case you have to define your command with an argument, that is passed to \frametitle, e.g.:
\documentclass[xcolor=dvipsnames]{beamer}
    \usepackage[T1]{fontenc}
    \usetheme{AnnArbor}
    \usecolortheme{whale}
    \setbeamercolor{title}{bg=RoyalPurple}
    \setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}
     
    \usepackage{amsmath}
     
    \newcounter{exm}
    \makeatletter
    \newcommand*{\ExmT}[1]{%
      \refstepcounter{exm}%
      \protected@edef\@currentlabelname{Example\protect~\theexm: #1}%
      \frametitle{%
        Example~\theexm: #1}%
    }
    \makeatother
    \resetcounteronoverlays{exm}
     
    \begin{document}
     
    \frame{%
      \frametitle{Testframe}
      To avoid that exmaples and frames have the same number.
    }
     
    \frame{
    \ExmT{Who is Hilbert}\label{ex:hilbert}%
    David Hilbert is a great Mathematician!
    }
     
    \frame{
    \ExmT{Who is Riemann}\label{ex:riemann}%
    Bernhard Riemann is another greatest mathematician in the history!
     
    }
     
    \frame{{How do you define Mathematics?}
     
    Mathematics is a language of sciences. Moreover, $\cdots$
     
    }
     
    \frame{{Rmarks on \nameref{ex:hilbert}}
     
    {\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
        page~\pageref{ex:hilbert} (on the title of this frame) manually but is there a way to reference \nameref{ex:hilbert} automatically?}} Not the frame number.
    }
     
     
    \end{document}
If you want some examples with title and some without, you need either two different commands or (once again) an optional argument. See a LaTeX introduction about how to define commands with optional arguments.

BTW: I would recommend to use the frame environment with LaTeX. The command \frame is supported for legacy documents only. See the footnote in section 8.1 of the beamer manual.

Note, I usually do not read answered questions. So if you have a following up question it would be better to ask a new one (with link to the initial question/answer if useful).
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply