1

Yesterday, I provided an (incorrect) answer to this question, and would like to check my understanding of why it was wrong.

The answer I provided produced (almost) the correct result in the output document, but for very wrong reasons, and I would like a deeper understanding of what my answer is actually doing (as opposed to what I thought it was doing).

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \pgfkeys{/a/.code=(a)}
  \pgfkeys{/a}
  \def\b{\pgfkeys{/a}}
  \b
  \expandafter\def\c{\pgfkeys{/a}}
  \c
\end{document}

seems to produce the correct output of (a) (a) (a) (with an additional space between the first and second (a)). I didn't use \show to see what each of \a, \b, and \c were, though, so didn't actually answer the question as asked.

I assumed that \expandafter\def\c{\pgfkeys{/a}} would expand the pgfkeys part, and assign it to \c, and so do what the original questioner wanted.

From @DavidCarlisle's comment, this only works because \c is already defined, and so this re-defines the expansion of \c (in the comment \T1-cmd, in my case \OT1-cmd) to be \pgfkeys{/a} (not expanded), and so is the same as \b. The use of \c then expands, and produces the same as the expansion of \b (additionally, breaking any use of accents at any point further in the document).

Is my understanding of what went wrong correct? If not, what does this actually do? Bonus question: is there a number of \expandafter here that would make this actually work the way I thought?

2
  • \expandafter always jumps over one token, so to get at the \pgfkeys you would need \expandafter\def\expandafter\c\expandafter{\pgfkeys{/a}}. But it only expand once, it doesn't expand fully. So \c would then have the definition \expandafter \pgfkeys@@set \expandafter {\pgfkeysdefaultpath }{/a} Commented Nov 1, 2022 at 23:16
  • 1
    a side note: I believe the culprit with some difficulties involving \expandafter is that we tend to see { and } as "mere group delimiters that don't really count", but TeX sees each one of them as one token (unless the standard plain/LaTeX catcodes have been changed), so they do count for \expandafter Commented Nov 2, 2022 at 0:03

1 Answer 1

2

\expandafter\def\tmp{abc}

  • would expand \tmp so give an undefined command error in most cases.

    (In your example this expanded the accent command \c which avoided a error but produced a totally unwanted result as shown below)

  • If \tmp was defined via \def\tmp{x} the above would be

    \def x{abc}

    and give an error that there was a missing command as you can not define x

  • If \tmp was defined via \def\tmp{\y{x}} the above would be

    \def\y{x}{abc}

    so it would define \y to be x and typeset abc.

  • If \tmp was defined via \def\tmp{\xxa\xxb\xxc} (your case, with internals of the accent mechanism) the above would be

    \def\xxa\xxb\xxc{abc}

    which would define \xxa to be a macro that must be followed by \xxb\xxc and if so expands to abc

    So this would break most uses of \xxa but

    \tmp has not been redefined so \tmp expands to \xxa\xxb\xxc as before which now expands to abc which is why your final \c appeared to produce the intended output even though \c had not been redefined.


Answer to the bonus question is no, no \expandafter will work.

If \def\qqq{x} you can fully expand \qqq in one step then

\expandafter\def\expandafter\tmp\expandafter{\qqq}

is essentially what you intended and is

\def\tmp{x}

so does define \tmp.

However \pgfkeys is not expandable it does many assignments so you can not force a final value by applying \expandafter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .