# Compute slide number in beamer

The MWE below is to insert some constant content in all odd slides.

Since the number of even slides could change, how to insert the odd contents automatically?

For example, if the last even slide is \only<10>{This is slide \thepage.} the command for constant content should be automatically computed to \only<1,3,...,11>{This is slide \thepage.}

MWE

\documentclass{beamer}

\begin{document}
\begin{frame}
\only<1,3,5>{This is slide \thepage. ODD.}
\only<2>  {This is slide \thepage.}
\only<4>  {This is slide \thepage.}
\end{frame}
\end{document}

• I'm not sure if you want 'odd in absolute terms' or rather 'relative to a currently even slide', e.g. is the \only<2> line the 'anchor'. I ask as this feels to me like a case where you want relative slide specifiers. – Joseph Wright Jan 29 '18 at 12:14
• @JosephWright, thanks for comment. But I'm afraid I don't understand what you said. For example, what I want is to insert song lyrics, and after each part, show a chorus. I think I will use a single frame, so odd/even works well. But it would be nice to extend and apply the rule for first, first+2, fisrt+4,... where first is the 1st one in the frame. – Sigur Jan 29 '18 at 12:18

\documentclass{beamer}

\makeatletter
\newcommand{\insertonodd}{%
\ifodd\the\beamer@slideinframe
This is slide \insertslideinframe. ODD.
\else
\only<\numexpr\insertslideinframe+1\relax>{}
\fi
}

\newcommand{\insertslideinframe}{\the\beamer@slideinframe}

\makeatother

\begin{document}
\begin{frame}

\insertonodd

\only<2>  {This is slide \insertslideinframe.}
\only<4>  {This is slide \insertslideinframe.}
\end{frame}
\end{document}


With an up-to-date beamer version, this can be simplified to:

\documentclass{beamer}

\newcommand{\insertonodd}{%
\ifodd\insertoverlaynumber
This is slide \insertoverlaynumber. ODD.
\else
\only<\numexpr\insertoverlaynumber+1\relax>{}
\fi
}

\begin{document}
\begin{frame}

\insertonodd

\only<2>  {This is slide \insertoverlaynumber.}
\only<4>  {This is slide \insertoverlaynumber.}
\end{frame}
\end{document}

• Nice. Your code works as expected, but I don't understand the logical rule. Since there is no loop within insertonodd definition, how it inserts content in slide number 3? I suppose it is executed only once at beginning. Could you give me an idea? – Sigur Jan 29 '18 at 12:10
• @Sigur The command is executed once on every overlay – samcarter_is_at_topanswers.xyz Jan 29 '18 at 12:13
• Is the \if else fi responsible for this? – Sigur Jan 29 '18 at 12:22
• @Sigur No, every command in the body of a frame is executed once per overlay. You can test this by replacing \insertonodd with a counter. – samcarter_is_at_topanswers.xyz Jan 29 '18 at 12:34
• Oh, I didn't know that. Nice to know. Thanks. – Sigur Jan 29 '18 at 12:36