I'm trying to use a stack data structure (as discussed here: Push/Pop or save a length/dimension?) to store the various names of a set of tikzmark node names (as discussed in tikzmark manual) scatted throughout my text.
Separately the stack implementation (WE1) and \tikzmark (WE2) work perfectly, but their combined behaviours somehow clash with multiples errors.
I can't for the life of me figure out what's going wrong when I try passing the popped stack data elements as node names within a \tikzmark command (MWE below). Is this a problem arising from my LaTeX code not immediately evaluating the \pop commands (I've tried playing around with \expandafter and \edef to no success...)? I've also tried passing \pop{\latexstack} to a \newcommand{}, but I haven't managed to get that method to work either...
MWE [Incorrect combined behaviours...]
\documentclass{article}
\usepackage{blindtext, tikz}
\newtoks\latexstack
\latexstack={\empty}
\def\push#1#2{
\begingroup
\toks0={{#1}}
\edef\act{\endgroup\global#2={\the\toks0 \the#2}}\act
}
\def\pop#1{
\begingroup
\edef\act{\endgroup\noexpand\SplitOff\the#1(tail)#1}\act
}
\def\SplitOff#1#2(tail)#3{
\ifx#1\empty
\errhelp{Attempting to pop empty stack #3.}
\errmessage{You can't pop an empty stack.}
\else
#1\global#3={#2}
\fi
}
\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node(#1) {};}
\begin{document}
\push{A1}{\latexstack}
\push{B2}{\latexstack}
\blindtext \tikzmark{\pop{\latexstack}}\\
\blindtext \tikzmark{\pop{\latexstack}}\\
\begin{tikzpicture}[remember picture,overlay,scale=0.5]
\draw[red] (A) -- (B);
\end{tikzpicture}
\end{document}
WE1 [Correct behaviour of stack data structure: A1, B2, C3, and D4 are pushed and sequentially popped from the data structure]
\documentclass{article}
\newtoks\latexstack
\latexstack={\empty}
\def\push#1#2{
\begingroup
\toks0={{#1}}
\edef\act{\endgroup\global#2={\the\toks0 \the#2}}\act
}
\def\pop#1{
\begingroup
\edef\act{\endgroup\noexpand\SplitOff\the#1(tail)#1}\act
}
\def\SplitOff#1#2(tail)#3{
\ifx#1\empty
\errhelp{Attempting to pop empty stack #3.}
\errmessage{You can't pop an empty stack.}
\else
#1\global#3={#2}
\fi
}
\begin{document}
\push{A1}{\latexstack}
\push{B2}{\latexstack}
\push{C3}{\latexstack}
\push{D4}{\latexstack}
\pop{\latexstack},\\
\pop{\latexstack},\\
\pop{\latexstack},\\
\pop{\latexstack}.
\end{document}
WE2 [Correct behaviour of \tikzmark: Points A and B are marked, and connected together with a red line]
\documentclass{article}
\usepackage{blindtext, tikz}
\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node(#1) {};}
\begin{document}
\blindtext \tikzmark{A}\\
\blindtext \tikzmark{B}
\begin{tikzpicture}[remember picture,overlay,scale=0.5]
\draw[red] (A) -- (B);
\end{tikzpicture}
\end{document}
