LaTeX forum ⇒ Presentations and PostersCustomized frame title with examples and how to reference those examples? Topic is solved

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

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

Postby CMP » Thu Aug 22, 2019 9:30 pm

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.

User avatar
Ijon Tichy
Posts: 138
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Fri Aug 23, 2019 7:36 am

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.
Code: [Select all] [Expand/Collapse] [Download] (optlabel-example.tex)
  1. \documentclass[xcolor=dvipsnames]{beamer}
  2. \usepackage[T1]{fontenc}
  3. \usetheme{AnnArbor}
  4. \usecolortheme{whale}
  5. \setbeamercolor{title}{bg=RoyalPurple}
  6. \setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}
  7.  
  8. \usepackage{amsmath}
  9.  
  10. \newcounter{exm}
  11. \makeatletter
  12. \newcommand\ExmT[1][]{%
  13. \frametitle{%
  14. \refstepcounter{exm}%
  15. \ifx\relax#1\relax\else
  16. \edef\@currentlabelname{Example~\theexm}%
  17. \label{#1}%
  18. \fi
  19. Example~\theexm}%
  20. }
  21. \makeatother
  22. \resetcounteronoverlays{exm}
  23.  
  24. \begin{document}
  25.  
  26. \frame{%
  27. \frametitle{Testframe}
  28. To avoid that exmaples and frames have the same number.
  29. }
  30.  
  31. \frame{
  32. \ExmT[ex:hilbert]%
  33. David Hilbert is a great Mathematician!
  34. }
  35.  
  36. \frame{
  37. \ExmT[ex:riemann]%
  38. Bernhard Riemann is another greatest mathematician in the history!
  39.  
  40. }
  41.  
  42. \frame{{How do you define Mathematics?}
  43.  
  44. Mathematics is a language of sciences. Moreover, $\cdots$
  45.  
  46. }
  47.  
  48. \frame{{Rmarks on \nameref{ex:hilbert}}
  49.  
  50. {\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
  51. 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.
  52. }
  53.  
  54.  
  55. \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:
Code: [Select all] [Expand/Collapse] [Download] (cleantitle-example.tex)
  1. \documentclass[xcolor=dvipsnames]{beamer}
  2. \usepackage[T1]{fontenc}
  3. \usetheme{AnnArbor}
  4. \usecolortheme{whale}
  5. \setbeamercolor{title}{bg=RoyalPurple}
  6. \setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}
  7.  
  8. \usepackage{amsmath}
  9.  
  10. \newcounter{exm}
  11. \makeatletter
  12. \newcommand\ExmT{%
  13. \refstepcounter{exm}%
  14. \protected@edef\@currentlabelname{Example\protect~\theexm}%
  15. \frametitle{%
  16. Example~\theexm}%
  17. }
  18. \makeatother
  19. \resetcounteronoverlays{exm}
  20.  
  21. \begin{document}
  22.  
  23. \frame{%
  24. \frametitle{Testframe}
  25. To avoid that exmaples and frames have the same number.
  26. }
  27.  
  28. \frame{
  29. \ExmT\label{ex:hilbert}%
  30. David Hilbert is a great Mathematician!
  31. }
  32.  
  33. \frame{
  34. \ExmT\label{ex:riemann}%
  35. Bernhard Riemann is another greatest mathematician in the history!
  36.  
  37. }
  38.  
  39. \frame{{How do you define Mathematics?}
  40.  
  41. Mathematics is a language of sciences. Moreover, $\cdots$
  42.  
  43. }
  44.  
  45. \frame{{Rmarks on \nameref{ex:hilbert}}
  46.  
  47. {\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
  48. 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.
  49. }
  50.  
  51.  
  52. \end{document}

CMP
Posts: 6
Joined: Thu Aug 22, 2019 8:33 pm

Postby CMP » Fri Aug 23, 2019 4:03 pm

It works perfectly well to my need.

Thank you very much, Ijon Tichy!

CMP
Posts: 6
Joined: Thu Aug 22, 2019 8:33 pm

Postby CMP » Fri Aug 23, 2019 4:03 pm

It works perfectly well to my need.

Thank you very much, Ijon Tichy!

CMP
Posts: 6
Joined: Thu Aug 22, 2019 8:33 pm

Postby CMP » Fri Aug 23, 2019 4:43 pm

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

Thank you!

CMP
Posts: 6
Joined: Thu Aug 22, 2019 8:33 pm

Postby CMP » Thu Sep 12, 2019 7:22 pm

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: 138
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Fri Sep 13, 2019 8:00 am

In this case you have to define your command with an argument, that is passed to \frametitle, e.g.:
Code: [Select all] [Expand/Collapse] [Download] (examplewithtitle.tex)
  1. \documentclass[xcolor=dvipsnames]{beamer}
  2. \usepackage[T1]{fontenc}
  3. \usetheme{AnnArbor}
  4. \usecolortheme{whale}
  5. \setbeamercolor{title}{bg=RoyalPurple}
  6. \setbeamercolor{frametitle}{fg=white,bg=RoyalPurple}
  7.  
  8. \usepackage{amsmath}
  9.  
  10. \newcounter{exm}
  11. \makeatletter
  12. \newcommand*{\ExmT}[1]{%
  13. \refstepcounter{exm}%
  14. \protected@edef\@currentlabelname{Example\protect~\theexm: #1}%
  15. \frametitle{%
  16. Example~\theexm: #1}%
  17. }
  18. \makeatother
  19. \resetcounteronoverlays{exm}
  20.  
  21. \begin{document}
  22.  
  23. \frame{%
  24. \frametitle{Testframe}
  25. To avoid that exmaples and frames have the same number.
  26. }
  27.  
  28. \frame{
  29. \ExmT{Who is Hilbert}\label{ex:hilbert}%
  30. David Hilbert is a great Mathematician!
  31. }
  32.  
  33. \frame{
  34. \ExmT{Who is Riemann}\label{ex:riemann}%
  35. Bernhard Riemann is another greatest mathematician in the history!
  36.  
  37. }
  38.  
  39. \frame{{How do you define Mathematics?}
  40.  
  41. Mathematics is a language of sciences. Moreover, $\cdots$
  42.  
  43. }
  44.  
  45. \frame{{Rmarks on \nameref{ex:hilbert}}
  46.  
  47. {\color{red}\textbf{I am writing Remarks on Example~\ref{ex:hilbert} on
  48. 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.
  49. }
  50.  
  51.  
  52. \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).


Return to “Presentations and Posters”

Who is online

Users browsing this forum: No registered users and 1 guest