4

Say for some reason I want to make a macro with input stream behaviour mimicking that of \afterassignment and \aftergroup. Namely, I want to absorb exactly one token from the input stream following the macro, without expanding it, regardless of whether it would be an unmatched brace, and store it somewhere I could look at it. This is almost like \[future]let but not quite: consider

\def\x{\message{Wrong}}
\afterassignment\x
\def\x{\message{Right}}

\def\save{\let\recall=}
\def\x{\message{Wrong}}
\save\x
\def\x{\message{Right}}
\recall

which outputs “Right Wrong”.

  • I admit I am a bit confused about what you are trying to achieve, and the relevance of your code. It is also a bad code for diagnostic purposes, because it reuses the same strings. I replaced your Wrong, Right, Wrong, Right by AAA, BBB, CCC, and DDD, and found that it outputs BBB CCC. What would you have expected, and why? – Harald Hanche-Olsen Feb 13 '16 at 20:22
  • @Harald If I am (e.g.) to hook \afterassignment in a useful manner, I should be able to define \save so as to get BBB DDD instead. The current behaviour is totally expected, but it is not the desired one. – Alex Shpilkin Feb 13 '16 at 20:26
  • It is exactly the purpose of \futurelet: \futurelet\decide\x will expand \decide, that can do things depending on what's the following token (in this case \x. Such an action might also include removing the token from the input stream, for example if the replacement text of \decide ends with \let\forget= . – egreg Feb 13 '16 at 20:37
  • 1
    you last comment implies the question is not answerable I think, if you want to absorb a left brace you have to use something like \let not \def as you can not have a mis-matched brace in a macro replacement text. – David Carlisle Feb 13 '16 at 21:40
  • 1
    @alex-shpilkin Yes, \next was obviously missing. You're giving contradictory requirements, I'm afraid. – egreg Feb 13 '16 at 22:05
1

Here is a definition of \save that does everything except distinguish explicit and implicit braces. (Which I think is not possible and is not necessary anyway for customizing \afterassignment.) Turns out this is actually quite easy. I use \global because that is how \afterassignment works, but it’s not essential.

\def\save{\futurelet\next\s@ve}
\def\s@ve
 {\ifx\next\bgroup \let\next=\s@veopn
  \else\ifx\next\egroup \let\next=\s@veclo
  \else \let\next=\s@vetok \fi\fi\next}
\def\s@veopn  {\global\def\recall{\iftrue {\else}\fi} \let\next=}
\def\s@veclo  {\global\def\recall{\iffalse{\else}\fi} \let\next=}
\def\s@vetok#1{\global\def\recall{#1}}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.