Rmano
Hi all,
this is my first question here... I hope I am in the right place. So, this is a quite esoteric question so the example is a bit convoluted (and verbose, because I think it's easier to understand like this).
The idea for the code is to have a command like `\appearingbox<2->{name}{content}`. This will typeset the content more or less "plain" in the first overlay and then boxed in the second one. Moreover, I will have the `name` available to link things with drawings.
I will dump the code here, will explain the problem after it.
```latex
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand\insideappinbox[4]{%
\begin{tikzpicture}[remember picture,baseline=(#2-inner.base)]
\node[draw=none] (#2-inner) {#3};
\only#4{
\node[overlay, draw=#1, fit=(#2-inner)](#2) {};
}
\end{tikzpicture}%
}
\newcommand<>{\appearingbox}[3][red]{\relax
% we make a tigtht inner box here
% notice that #4 is the overlay spec with <>
\ifmmode
\mathchoice{%
\appearingboxD#4[#1]{#2}{#3}%
}%
{%
\appearingboxT#4[#1]{#2}{#3}%
}%
{%
\appearingboxS#4[#1]{#2}{#3}%
}%
{%
\appearingboxSS#4[#1]{#2}{#3}%
}%
\else
\appearingboxP#4[#1]{#2}{#3}%
\fi
}
\newcommand<>{\appearingboxD}[3][red]{\relax
\insideappinbox{#1}{#2}{$\displaystyle #3$}{#4}%
}
\newcommand<>{\appearingboxT}[3][red]{\relax
\insideappinbox{#1}{#2}{$\textstyle #3$}{#4}%
}
\newcommand<>{\appearingboxS}[3][red]{\relax
\insideappinbox{#1}{#2}{$\scriptstyle #3$}{#4}%
}
\newcommand<>{\appearingboxSS}[3][red]{\relax
\insideappinbox{#1}{#2}{$\scriptscriptstyle #3$}{#4}%
}
\newcommand<>{\appearingboxP}[3][red]{\relax
\insideappinbox{#1}{#2}{$\displaystyle #3$}{#4}%
}
\begin{document}
\begin{frame}
\frametitle{F1: Test with specific ones}
\appearingboxP{textmode}{textmode}
\[ \appearingboxD<2->{dstyle}{\sum^1 A_D} \]
$\appearingboxT<2->[blue]{tstyle}{\sum_0^1 A_T}$
\only<2->{\tikz[remember picture, overlay] \draw[->] (textmode) -- (dstyle) -- (tstyle);}
\end{frame}
\begin{frame}
\frametitle{F2: Test with mathchoice}
\appearingbox{textmode}{textmode}
\[ \appearingbox<2->{dstyle}{\sum^1 A_D} \]
$\appearingbox<2->[blue]{tstyle}{\sum_0^1 A_T}$
\only<2->{\tikz[remember picture, overlay] \draw[->] (textmode) -- (dstyle) -- (tstyle);}
\end{frame}
\end{document}
```
This is the output:
![ksnip_20200902-155136.png](/image?hash=594a4f8519e3f0bd318b7db7975cd45f167002c1954dd063095d6eabf66ec106)
Notice that when I use manually the versions of the `\appearingbox` macro for each style (non-math, displaystyle, etc.) like in the first frame (first row in the image) all is ok. The nodes are usable outside the command and, after a couple of runs, they work beautifully.
On the other hand, if I try to automatically select the appropriate function by way of `\ifmmode` and `mathchoice` I have a very puzzling (at leat for me) result. The appropriate macro is invoked but, alas, the nodes are in the wrong site.
I tried with `\expandafter` trickery, with older and newer TeXLives, by dancing around the computer... nothing works. I can survive with the manual method, but now I lost two hours trying to understand what happens and I'd like to understand it. Although probably the problem is that I misused `\mathchoice` (although I have a very similar construct in my package `circledsteps`).
Thanks!
Top Answer
user 3.14159
This `\mathchoice` stuff is tricky, but the good news is that, if I understand correctly what you are up to, this is already taken care of by the `tikzmark` library.
```
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary{fit,tikzmark}
\newcommand<>{\appearingbox}[3][red]{\relax
\tikzmarknode{#2-inner}{#3}%
\begin{tikzpicture}[overlay,remember picture]
\only#4{
\node[overlay, draw=#1, fit=(#2-inner)](#2) {};
}
\end{tikzpicture}%
}
\begin{document}
\begin{frame}
\frametitle{Using \texttt{tikzmark}}
\appearingbox{textmode}{textmode}
\[ \appearingbox<2->{dstyle}{\sum^1 A_D} \]
$\appearingbox<2->[blue]{tstyle}{\sum_0^1 A_T}$
\only<2->{\tikz[remember picture, overlay] \draw[->] (textmode) -- (dstyle) -- (tstyle);}
\end{frame}
\end{document}
```
![ani.gif](/image?hash=26e41de75db3f11fbbaf39e6a5568733fdc513476aec33cc05a3b2593f3f854f)