# Commands in frame labels, generating frame labels automatically

beamer experts,

I would kindly like to know if there is a way to add "commands" in the label option of frames and in \includeonlyframes and get the expected results, where you are expecting that the "text value" of the commands would actually be substituted? I have spent ("wasted" may be used, but i learned from these efforts as well, so "wasted" is not appropriate) so much time figuring it out. now, I know I need to seek expert opinion. I have shown here several MWEs to convey what I mean.

MWE 1:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{label-1,label-2}
\begin{document}
\begin{frame}[label=label-1]{Frame 1}
$E=mc^2$
\end{frame}
\begin{frame}[label=label-2]{Frame 2}
\end{frame}
\end{document}


MWE 2:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{\MyLabelForFrameOne,label-2}
\begin{document}
\begin{frame}[label=label-1]{Frame 1}
$E=mc^2$
\end{frame}
\begin{frame}[label=label-2]{Frame 2}
\end{frame}
\end{document}


MWE 3:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{label-1,label-2}
\begin{document}
\begin{frame}[label=\MyLabelForFrameOne]{Frame 1}
$E=mc^2$
\end{frame}
\begin{frame}[label=label-2]{Frame 2}
\end{frame}
\end{document}


MWE 4:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{\MyLabelForFrameOne,label-2}
\begin{document}
\begin{frame}[label=\MyLabelForFrameOne]{Frame 1}
$E=mc^2$
\end{frame}
\begin{frame}[label=label-2]{Frame 2}
\end{frame}
\end{document}


MWE1 gives the output that I expect.

In MWE2, in \includeonlyframes, I replaced label-1 with \MyLabelForFrameOne (which has been defined before as the same as label-1). Now, MWE2 does not give the output that I expect, which is the output in MWE1.

In MWE3, in the label option of {Frame 1}, I replaced label-1 with \MyLabelForFrameOne (which has been defined before as the same as label-1). Now, MWE3 does not give the output that I expect, which is the output in MWE1.

In MWE4, in \includeonlyframes and in the label option of {Frame 1}, I replaced label-1 with \MyLabelForFrameOne (which has been defined before as the same as label-1). Now, MWE4 does not give the output that I expect, which is the output in MWE1.

Note that MWEs 2 and 3 do not produce the same output as in MWEs 1 and 4. Also, MWE 4 has the same output as MWE 1.

It seems that the use of commands in frame labels and in \includeonlyframes does not always give expected results, that is, the "text value" of the command is not always "substituted" into the frame labels and in \includeonlyframes.

Actually, what I would desire is much more than the preceding MWEs. I'm hoping to find a way to generate automatically the frame labels, to ensure that they would be unique. The following MWEs would illustrate my point.

MWE5:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameZero}{label-0}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{label-0,label-1,label-2}
\setcounter{section}{0}
\begin{document}
\begin{frame}[label=label-0]{Outline}
\arabic{section}
\\
\tableofcontents
\end{frame}
\section{Frame 1}
\begin{frame}[label=label-1]{Frame 1}
\arabic{section}
\\
$E=mc^2$
\end{frame}
\section{Frame 2}
\begin{frame}[label=label-2]{Frame 2}
\arabic{section}
\\
\end{frame}
\end{document}


MWE 6:

\documentclass{beamer}
\newcommand*{\MyLabelForFrameZero}{label-0}
\newcommand*{\MyLabelForFrameOne}{label-1}
\newcommand*{\MyLabelForFrameTwo}{label-2}
\includeonlyframes{label-0,label-1,label-2}
\setcounter{section}{0}
\begin{document}
\begin{frame}[label=label-\arabic{section}]{Outline}
\arabic{section}
\\
\tableofcontents
\end{frame}
\section{Frame 1}
\begin{frame}[label=label-\arabic{section}]{Frame 1}
\arabic{section}
\\
$E=mc^2$
\end{frame}
\section{Frame 2}
\begin{frame}[label=label-\arabic{section}]{Frame 2}
\arabic{section}
\\
\end{frame}
\end{document}


MWE5 gives the output that I expect.

In MWE6, in the label option of {Frame 1}, I replaced label-1 with label-\arabic{section}, where \arabic{section} is obviously equal to 1. In the label option of {Frame 2}, I replaced label-2 with label-\arabic{section}, where \arabic{section} is obviously equal to 2 (the counter section has been automatically incremented by 1 since then). Now, MWE6 does not give the output that I expect, which is the output in MWE5.

I hope I was able to relay my concern. Kindly seeking your wisdom and help. Thank you.

• Welcome to TeX.SX! I have edited your question a bit to highlight source code properly, that makes is easier to read. I have also removed your personal information to protect your privacy. – moewe May 16 '18 at 5:56

The problem here is, that your macros are expanded to late to work as expected. Basically, if you give the macro instead of the label, its expansion is not seen. Instead, the macro name itself is used as label.

Here I defined a new option cmdlabel for frames, which uses a macro as label. That macro is then expanded, before setting the label with \setkeys. Additionally, the macro \includeonlyframescmd is defined to use macros for label names instead of the names itself. It just expands them before passing everything to \includeonlyframes.

The commented out parts implement the option autoframe, which can be used to label frames automatically (as long as the option is given). A prefix can be set with \autolabelprefix, which is appended with a consecutive number, i.e. with a prefix label- you get the labels label-1, label-2, etc.

The code:

\documentclass[compress]{beamer}
\usetheme{AnnArbor}
%\usetheme{Dresden}
\usecolortheme{crane}

\makeatletter

% set label with automatic name
%\newcounter{autolabelcnt}
%\setcounter{autolabelcnt}{0}
%\newcommand*{\autolabelprefix}[1]{%
%    \def\autolabel@prefix{#1}%
%}
%\newcommand*{\autolabel@@setlabel}[1]{%
%    \setkeys{beamerframe}{label=#1}%
%}
%\newcommand*{\autolabel@setlabel}[1]{%
%    \expandafter\autolabel@@setlabel\expandafter{\autolabel@prefix#1}%
%}
%\define@key{beamerframe}{autolabel}[]{%
%    \stepcounter{autolabelcnt}%
%    \expandafter\autolabel@setlabel\expandafter{\the\c@autolabelcnt}%
%}

% set label with command
\newcommand*{\cmdlabel@setlabel}[1]{%
\setkeys{beamerframe}{label=#1}%
}
\define@key{beamerframe}{cmdlabel}{%
\expandafter\cmdlabel@setlabel\expandafter{#1}%
}
\newcommand*{\includeonlyframescmd}[1]{%
\edef\includeonlyframes@cmd{#1}%
\expandafter\includeonlyframes\expandafter{\includeonlyframes@cmd}%
}
\makeatother

% set prefix for automatic name
%\autolabelprefix{label-}

% set commands for labels
\newcommand*{\mylabela}{clabel-1}
\newcommand*{\mylabelb}{clabel-2}
\newcommand*{\mylabelc}{clabel-3}
\newcommand*{\mylabeld}{clabel-4}

% use automatically named labels
%\includeonlyframes{label-1,label-3}

% use commands for labels
\includeonlyframescmd{\mylabela,\mylabelc}

\begin{document}
\section{Section 1}
\subsection{Subsection 1.1}
%\begin{frame}[autolabel]{Frame 1}
\begin{frame}[cmdlabel=\mylabela]{Frame 1}
Frame 1
\end{frame}

\subsection{Subsection 1.2}
%\begin{frame}[autolabel]{Frame 2}
\begin{frame}[cmdlabel=\mylabelb]{Frame 2}
Frame 2
\end{frame}

\section{Section 2}
\subsection{Subsection 2.1}
%\begin{frame}[autolabel]{Frame 3}
\begin{frame}[cmdlabel=\mylabelc]{Frame 3}
Frame 3
\end{frame}

\subsection{Subsection 2.2}
%\begin{frame}[autolabel]{Frame 4}
\begin{frame}[cmdlabel=\mylabeld]{Frame 4}
Frame 4
\end{frame}
\end{document}

• hi Mike, thank you very much for your answer. it has totally solved my problem as regards "set label with command". as regards "set label with automatic name", your "\makeatletter ...\makeatother" macro is very useful, but i'm still checking if i can modify it a little bit so that my automatic label naming is composed of 3 counter -- section, subsection, subsubsection -- instead of just 1 counter autolabelcnt. in the event that i'll have sections, subsections, subsubsections or perhaps parts in my presentation, having more counters to define the label would be more flexible and meaningful. – beethovengg14 May 17 '18 at 6:34
• if i counter an automatic frame label, say frame-2-3-1, then i would understand that it refers to the frame on section 2, subsection 3, subsubsection 1. hence, it becomes more meaningful, at least to me – beethovengg14 May 17 '18 at 6:51