# [NTG-pdftex] \expanded

Joseph Wright joseph.wright at morningstar2.co.uk
Thu May 17 10:35:00 CEST 2018

On 17/05/2018 07:30, Joseph Wright wrote:
> An \expanded primitive makes it much more convenient to create such
> approaches, and matches the expansion carried out by e.g. \pdfstrcmp.
> For the work of the team in expl3, this would significantly aid
> consistency and allow more 'easy' expandable programming. The code
> addition is also small compared with many other 'new' (post-e-TeX)
> primitives, and \expanded is already present in LuaTeX. As such, we hope
> that the request can be actioned: we will make similar requests for
> XeTeX and (u)pTeX, such that all major engines have the functionality.

A 'toy' application is a very simple expandable case changer. I've used
a bit of expl3 here purely to avoid having to re-work string comparison.
The following currently runs only with LuaTeX as it uses \expanded:

\input expl3-generic %
% A string case switcher, for convenience
\ExplSyntaxOn
\cs_set_eq:NN \strcase \str_case:nnF
\ExplSyntaxOff
\def\UpperCase#1{%
\expandafter\UpperCaseAux\expanded{#1}\stop
}
\def\UpperCaseAux#1{%
\ifx\stop#1\else
\strcase{#1}
% No match
{#1}%
\expandafter\UpperCaseAux
\fi
}
\def\LowerCase#1{%
\expandafter\LowerCaseAux\expanded{#1}\stop
}
\def\LowerCaseAux#1{%
\ifx\stop#1\else
\strcase{#1}
% No match
{#1}%
\expandafter\LowerCaseAux
\fi
}
\def\stop{\stop}
\def\foo{a\bar}\def\bar{B\baz}\def\baz{STOP}
\edef\test{\UpperCase{\LowerCase{TOKENS\foo}}}
\show\test
\bye

You *can* do the same without \expanded by arranging that each 'result'
token is shuffled to the end of an internal auxiliary set up, but that
means having to move tokens around. You can also deal with the nested
macros by testing each token for being an expandable macro. But the
result is much more 'pain', and that you have to be sure that everything
is set up correctly. (See \tl_upper_case:n, etc., in expl3, for a real
implementation that does all of these things.)

Having \expanded makes that all a lot easier, before you get on to
places where 'shuffle the tokens' is not realistic.

Joseph