Skip to content

\expandafter

yex.keyword.Expandafter(parser) #

Process the argument, then give the result to the previous control.

For example, in

\def\spong{spong}
\uppercase{\spong}

firstly \uppercase runs on "\spong", which gives "\spong" (since it's a control, not a series of letters). Then \spong is run, so we end up with "spong".

But in

\def\spong{spong}
\uppercase\expandafter{\spong}

\expandafter runs "\spong" and gets "spong", then passes it to \uppercase. So we end up with "SPONG".

Source code in yex/keyword/other.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
@yex.decorator.control(
        expandable = True,
        even_if_not_expanding = True,
        )
def Expandafter(parser):
    r"""
    Process the argument, then give the result to the previous control.

    For example, in

        \def\spong{spong}
        \uppercase{\spong}

    firstly \uppercase runs on "\spong", which gives "\spong" (since it's a
    control, not a series of letters). Then \spong is run, so we end up
    with "spong".

    But in

        \def\spong{spong}
        \uppercase\expandafter{\spong}

    \expandafter runs "\spong" and gets "spong", then passes it to
    \uppercase. So we end up with "SPONG".
    """

    opening = parser.next(
            level = 'deep',
            on_eof = 'raise',
            )

    # Right, let's read our argument.

    argument = [token for token in parser.another(
        level = 'executing',
        bounded ='single',
        on_eof = 'exhaust',
        )]

    rerun_parser = [t for t in parser.another(
            source = argument,
            on_eof = 'exhaust',
            )]

    parser.push(rerun_parser)
    parser.push(opening)