5

I'm still trying to get my head around how expansion works in TeX. One thing I can't figure out is how to expand a macro argument once without having to use a scratch register as an intermediate.

As an example, consider the following code, which defines an alternative to \meaning with a few bells and whistles:

\long\def\printmeaning#1%
{%
    \def\@tempa{#1}%
    {\tt\expandafter\string\@tempa} is defined as\par
    {\tt\meaning#1}%
}
\printmeaning\centerline

\bye

The output (produced with pdftex) is

enter image description here

So far, so good. However, I'm wondering whether I really have to use a scratch register (such as \@tempa), here. Is there a way to print the name of the control sequence passed to \printmeaning without using a scratch register (or some other temporary macro)?

  • \def\printmeaning #1{{\tt\meaning#1}} should work. Or you can aslo do \def\printmeaningwithname #1{{\tt\expandafter\meaning\csname #1\endcsname}} and then \printmeaningwithname {centerline} (advantageous in latex when not wanting to always do \makeatletter etc...). – user4686 Nov 6 '13 at 18:51
  • @jfbu Thanks, but I don't think that answers my question. In my example, I use a scratch register in order to print the name of the control sequence passed as argument to \printmeaning. My question is: can I print that control-sequence name without having to use a scratch register? (I'll clarify my question.) – jub0bs Nov 6 '13 at 18:54
  • 1
    ah sorry you wanted \def\printmeaning #1{{\tt\string#1} is defined as:\endgraf {\tt\meaning#1}} (I think) – user4686 Nov 6 '13 at 18:54
6

You can directly use \string#1 if #1 is a control sequence:

\def\printmeaning #1{{\tt\string#1} is defined as:\endgraf {\tt\meaning#1}}

(with perhaps an additional \endgraf at the end. Or \par. I tend to use \endgraf for no reason outside of real typography).

  • but \printmeaning\par will not work, as the definition was not prefixed with \long... \printmeaning\endgraf does work :) – user4686 Nov 6 '13 at 19:03
  • Ah thanks. I don't think I had even tried \string#1. – jub0bs Nov 6 '13 at 19:03
  • 1
    you should think of #1 as really exactly as if it was replaced by the macro argument, as if you had typed it in directly. – user4686 Nov 6 '13 at 19:04

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.