The idea is to replace each occurrence of \thiscsname
by the current \name
in the macro body when the macro is defined by \mynewcommand
. This means, that
\mynewcommand \mynameofmacro {... My name is \string\thiscsname ...}
would be replaced by
\newcommand \mynameofmacro {... My name is \string\mynameofmacro ...}
We can define \mynewcommand such a way:
\newtoks\macrobody
\let\newcommandOri=\newcommand
\def\mynewcommand#1#2#{%
\def\macroname{#1}\def\macronameN{\noexpand#1}\def\newcommandparams{#2}%
\afterassignment\mynewcommandA \macrobody
}
\def\mynewcommandA{%
\replacemacrobody
\edef\x{\noexpand\newcommandOri\macronameN\newcommandparams{\the\macrobody}}\x
}
The name of the macro is saved in \macroname
, the parameters are saved in \newcommandparams
and the macro body is saved in \macrobody
toks register. The macro \replacemacrobody
replaces in this toks register the occurrences of \thiscsname
tokens to the contents of the \macroname
. Finally, the \newcommad
with the original LaTeX meaning is used via expanded \x
. You can do \let\newcommand=\mynewcommand
if you have bravery.
The core of this idea is the replacement macro which operates with \macrobody
toks register. May be, you can use some LaTeX package to do this, but I don't know LaTeX, so I have to make this macro myself. This macro have to stay intact all tokens (spaces, \bgroup
, {
, }
, #
, etc.), only does \thiscsname -> \name
. I am sorry, my implementation of this macro is somewhat complicated, but this works.
The whole example follows:
\documentclass{article}
%% The \replacemacrobody definition:
\newtoks\macrobody
\def\replacemacrobody{%
\let\bgroup=\relax\macrobody\expandafter{\expandafter}\expandafter
\replacemacrobodyA\the\macrobody\endmacrobody}
\def\replacemacrobodyA{\futurelet\tmp\replacemacrobodyB}
\def\replacemacrobodyB{%
\let\next=\replacemacrobodyD
\ifx\tmp\spacetoken \let\next=\replacemacrobodyC \addtomacrobody{ }\fi
\ifx\tmp\thiscsname \let\next=\replacemacrobodyC
\expandafter\addtomacrobody\macroname \fi
\ifx\tmp\endmacrobody \let\next=\replacemacrobodyE \fi
\ifx\tmp\bgroupOri \let\next=\replacemacrobodyF \fi
\next}
\def\replacemacrobodyC{\afterassignment\replacemacrobodyA \let\next= }
\long\def\replacemacrobodyD#1{\addtomacrobody#1\replacemacrobodyA}
\def\replacemacrobodyE{\ifx\IamInGroup\relax
\expandafter\endgroup \expandafter\macrobody\expandafter\expandafter\expandafter
{\expandafter\the\expandafter\macrobody\expandafter{\the\macrobody}}%
\afterassignment\replacemacrobodyA
\else \let\bgroup=\bgroupOri \fi
\let\next=}
\def\replacemacrobodyF{\begingroup\let\IamInGroup=\relax
\afterassignment\replacemacrobody \macrobody=}
\long\def\addtomacrobody#1{\macrobody\expandafter{\the\macrobody#1}}
\let\bgroupOri=\bgroup
\def\tmp/{\let\spacetoken= }\tmp/ %
\def\thiscsname{^\thiscsname^}
\def\endmacrobody{^\endmacrobody^}
%% The \mynewcommand definition:
\let\newcommandOri=\newcommand
\def\mynewcommand#1#2#{%
\def\macroname{#1}\def\macronameN{\noexpand#1}\def\newcommandparams{#2}%
\afterassignment\mynewcommandA \macrobody
}
\def\mynewcommandA{%
\replacemacrobody
\edef\x{\noexpand\newcommandOri\macronameN\newcommandparams{\the\macrobody}}\x
}
\let\newcommand=\mynewcommand % I have bravery
%% The usage of redefined \newcommand:
\newcommand\testmacro
{this is test\GenericWarning{}{Warning from \string\thiscsname}}
\newcommand\withparams [2]
{this is \textbf{macro} with params: #1 and #2
\GenericWarning{}{Warning from \string\thiscsname}}
%% The test:
\begin{document}
\testmacro,
Macro \withparams {first}{second}
\end{document}
\FooBar
is executed'.\newcommand
command, and add some sort of assignment to\CurrentMacroName
at the beginning (you loose expandability).expanded/executed
... I know the difference...