# [NTG-pdftex] \expanded

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

On 17/05/2018 07:30, Joseph Wright wrote:
> On 17/05/2018 00:12, Ross Moore wrote:
>> Hi Karl, and others.
>>
>> On May 17, 2018, at 8:57 AM, Karl Berry
>> <karl at freefriends.org<mailto:karl at freefriends.org>> wrote:
>>
>> Doing a grep on /usr/local/texlive/2018/texmf-dist/tex/, I see \expanded
>> is used in a couple oberdiek packages, context, and mathspec. I did not
>> try to figure out if the mathspec usage is compatible. It might be worth
>> notifying its author at some point as a courtesy. FWIW ...
>>
>>
>> Is  \expanded  really equivalent to that  xelatex  definition below:
>>
>> xelatex/mathspec/mathspec.sty:129:\providecommand\expanded[1]{\edef\@tempa{#1}\@tempa}
>>
>>
>> It seems to me that it should be, but maybe there is actually a slight
>> difference.
>> So it would be nice to see some practical applications;
>> e.g., where a package author had a specific desire, or absolute need (?),
>> to avoid going via a temporary container-like macro such as  \@tempa
>> above.
>>
>> Otherwise, why the need for this as a primitive?
>> Convenience only, is not normally a good enough reason to add a
>> primitive.
>
> Hello Ross,
>
> The primitive definition differs from the \edef version in that
>
> - It does not double # tokens
> - It is expandable
>
> The latter is the big thing, and the reason the team have asked that
> this long-standing suggestion be added to engines. At present, if you
> want to arrange for 'function-like' behaviour, you can use \romannumeral
> to expand material
>
>    \def\foo#1{\expandafter\fooaux\romannumeral-q#1 }
>
> which will work but only up to the first non-expandable item in #1 (and
> also looses any leading spaces). The result is that if you wish to write
> expandable functions which are themselves expandable, you need to be
> careful in how everything is constructed such that nothing is 'output'
> until all processing is done. That often comes with a code complexity
> and performance cost, and is not always achievable. (See the various
> 'f-type expandable' functions in expl3.)

To add that there are places where \romannumeral is exactly what you
want for expansion. It is looking for an <optional space> here, which
means that it will expand \protected macros. Thus the token you are left
with at the start of your expanded material is the next thing TeX would
have to execute/typeset if you'd not forced the expansion. That is
useful in some look-ahead cases: for example, in the experimental
l3galley code this is used to look for upcoming paragraphs.

However, this application is relatively unusual, and in most cases what
you want is to expand material in an \edef-like way, but without the
knock-on of making your code non-expandable.

Joseph
`