I was recently looking for the creation of commands that use the name of another command and I recently discovered \expandafter and \csname instructions. I manage to create simple one using these instructions but I have problems for more complex commands.

Here is a minimal example of my code :



    \expandafter\let\csname  old#1\expandafter\endcsname\csname #1\endcsname
    \expandafter\RenewDocumentCommand\csname #1\endcsname{m o m}{\IfValueTF{##2}{\underset{\substack{##1}}{\overset{##2}{\csname old#1\endcsname}} ##3 }{\underset{\substack{##1}}{\csname old#1\endcsname}{##3}}}



In this code, \DisplayEtype is not working correctly. For \sum, it creates \oldsum and redefine \sum without troubles (\sum is redefine as a sum with range indice and exposant). However, in the case of \test, it creates a fatal error (the last commented line).

I think it is because of my \expandafter use here. I have been searching a related topic but so far, I have not found any solution.

Could you tell me what I am missing?

Thank you for your help.

PS : By the way, do you know a simple rule to know where to use \expandafter instruction ?

  • 1
    apart from the expandafter question beware white space at ends of lines you have lots of missing % – David Carlisle Oct 13 '17 at 12:59
  • 1
    unrelated to the expandafter question but \begin{minipage}{\widthof{\begin{huge}#2\end{huge}}}\begin{huge}#2\end{huge}\end{minipage} is a rather complicated way of getting a huge text, \vcenter{\hbox{\huge #2}} would produce a centred huge text without setting it twice – David Carlisle Oct 13 '17 at 13:24

If you add \show you see

> \oldtest=macro:
->\protect \test  

so because test was originally a robust macro defined as


(but with a space not a _ ) then your \let did not save the real underlying meaning and the test_ macro gets used in the wrong place in the redefinition.

  • Ok, I think I understand the problem with your explanation @DavidCarlisle, but I am not able to correct it because I'm a beginner with the \expandafter synthax. What is the correct way to redefine \test well in this precise case ? – Adrien Meilac Oct 13 '17 at 18:16

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.