Is it possible to have a macro check, if it is followed by a given pattern of tokens and produce different expansions depending on it?
So far I have tried using code such as
\def\conditionalAction#1#2{
\def\ca@tokens{{#1}{#2}}
\def\ca@match{{\begin}{someenv}}
\ifx\ca@tokens\ca@match
Matched.
\else
Not matched.
\fi
#1#2%
}
but this has several severe limitations:
- If used near the end of a file, it will result in a
File ended while scanning use of \conditionalActionerror. This is important, when using such a pattern to iterate over files. - It actually breaks, if either
#1or#2is a token group. E.g. it will transform\begin{stuff}into\begin stuff, which is wrong.
Workarounds are partly possible but impractical. E.g. it is possible to fix (1) by requiring, that each file ends with some end-marker, and (2) isn't relevant if the arguments compared against are discarded.
Current usecase
While the question is more general for future reference, my current use-case is iterating over an input file, while discarding anything that is not inside a given environment. The idea is to provide a mechanism, that allows delaying the output of content to a later point (like the apxproof package, but without breaking synctex). Right now I am using a construct
\expandafter\SomeProcessFileMacro\@@input\InputFileNameAsMacro
where \SomeProcessFileMacro skips (using a pattern like \conditionalAction above) until it finds a sequence \begin{apxenv}, and is invoked again by \end{apxenv}. This mostly works and preserves synctex as intended, but I lack a mechanism for stopping at the end of the file.
The questions SyncTeX with endnotes and apxproof packages? and Synctex: Variables for file name and line number? are related, but about other angles from which I tried to approach the problem.
\futureletfor a one-token lookahead and packagesuffix. But lookaheads with token lists in curly braces are cumbersome. Maybe, you can describe the problem for which you need the lookahead. – Heiko Oberdiek May 24 '18 at 15:44\begin{foo}and\end{foo}neither\futureletnor\WithSuffixare applicable. – kdb May 24 '18 at 16:54