Using the answer from Push/Pop or save a length/dimension?, I have implemented a simple stack with a push and a pop function.
I would like to use the pop function in conjunction with newfile's addtostream so that I can export the latest item on the stack onto a file.
I am however unable to figure out how to make this work. The stack functions work fine in the text, but when inside addtostream everythings goes to hell...
Here is a MWE:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{newfile}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Stack functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% push #1 onto #2
\newcommand{\push}[2]{%
\begingroup\toks0={{#1}}
\edef\XXX{\endgroup\global#2={\the\toks0 \the#2}}\XXX
}
% pop from #1
\newcommand{\pop}[1]{%
\begingroup\edef\XXX{\endgroup\noexpand\splitList\the#1(tail)#1}\XXX
}
\def\splitList#1#2(tail)#3{%
\ifx#1\empty Can't pop an empty stack.\else#1\global#3={#2}\fi
}
% creation of #1
\newcommand{\newstack}[1]{
\newtoks#1
#1={\empty}
}
\begin{document}
\section*{Testing the stack function}
\newstack{\stackb}
\push{{HHHH}}{\stackb}%
\push{{XXXX}}{\stackb}%
\push{{WWWW}}{\stackb}%
if this: \pop{\stackb} shows "WWW", then it worked.
\section*{Testing the stack function popping to a file}
\newoutputstream{AStream}
\openoutputfile{criss.dta}{AStream}
\newcommand{\bibi}{A simple macro can be sent to a file}
\addtostream{AStream}{\bibi}
%\addtostream{AStream}{\pop{\stackb}} % This one not working...
\closeoutputstream{AStream}
\end{document}
Thanks for the help.