In my beamer presentations, I often face the situation where I have a long piece of tikz code embedded in \only, in the following schematic structure:

\foreach\i in {1,...,100} {
        [long piece of code]
\only<101>{ some text }

When some text appears, the previous [long piece of code] disappears. To make it stay, I can do:

\foreach\i in {1,...,99} {
        [long piece of code]
\uncover<100-> { [long piece of code] }
\only<101> { some text }

but this forces me to copy the long piece of code twice, which is unelegant and unconvenient. I tried using the \if command:

 \foreach \i in {1,...,100} {
       [long piece of code]
    \only<101>{ some text }

but it does produce the sought result.

How can I make a command such that it keeps only the last element?

  • Can't you use \alt to solve your issue?
    – user94293
    Sep 6, 2016 at 1:35
  • @user94293 I tried \alt<100>{\uncover<100->}{\only<\i>}{% but it does not do what I'd like. Is it what you were thinking about?
    – anderstood
    Sep 6, 2016 at 1:53
  • Rather than posting code fragments it is better to give a full minimal working example. This makes it much easier for people to help you, and much more likely that they will, because they can start with your working code and modify it rather than having to spend time splicing your code snippets together to get a working example. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem.
    – user30471
    Sep 6, 2016 at 2:32
  • If I understand what you want then your approach using \if\i=100 will work if you replace \uncover<10> with \uncover<100->.
    – user30471
    Sep 6, 2016 at 2:39

1 Answer 1


You can store the conditional overlay specification in a macro, and then execute this macro. Below I create \doit to be either \only or \uncover, and then \doit{<long piece of code>}:

enter image description here




  \frametitle{Frame title}

  \foreach\i in {1,...,10} {
    \doit{[long piece of code \i]}
  \only<11>{ some text }

  • Do you know why it failed in my attempt? Possibly something related to space between command and bracket?
    – anderstood
    Sep 6, 2016 at 14:53
  • @anderstood: This may be how the overlay specification processes its arguments. For example, under the true branch in your case, \only<\i> should be applied to {[ long piece of code ]}, but instead it might be applied to nothing, or \else, or something else altogether. In my approach, I collect the true/false branch information in \doit, and only execute it outside the condition.
    – Werner
    Sep 6, 2016 at 20:33
  • I see. You can maybe add the following command: \newcommand\myonly[3]{\ifnum\i<#2% \def\doit{\only<#1>}% \else% \def\doit{\uncover<#2->}% \fi% \doit{#3}}% to be used as \myonly{\i}{10}{[long piece of code]}, tested and works. Of course the % have to be followed by a line break, which does not show in comments.
    – anderstood
    Sep 7, 2016 at 1:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .