Redefine macro without using tmp variable
This of course cannot work, because it will leave the effect of \begingroup
hanging until you call \abc
, which will probably happen in undesirable places (see your other question).
To me it seems you want to do
\def\abc{abc}
\expandafter\def\expandafter\abc\expandafter{\abc def}
but this is clumsy and limited to only allow appending.
Without reinventing the wheel, you can use etoolbox
:
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\abc}{abc}
\begin{document}
The macro \verb|\abc| expands to ``\abc''.
\appto\abc{def}
The macro \verb|\abc| expands to ``\abc''.
\preto\abc{X}\appto\abc{X}
The macro \verb|\abc| expands to ``\abc''.
\end{document}
This will print
The macro
\abc
expands to “abc”.
The macro\abc
expands to “abcdef”.
The macro\abc
expands to “XabcdefX”.
Reorganized and corrected my answer
The cause why \orgabc
is still available is that the group is actually never closed, it causes warnings about semi groups
.
Now, egreg has used \expandafter\def\....
already.
It is possible to use \xdef\foo{\foo other stuff}
to use a shorter version.
As a third variant, apply \g@addto@macro
from the LaTeX kernel, which stores everything expanded in a token first and redefines the macro with \xdef
, in order to expand the token.
\documentclass{article}
\def\abc{abc}
\def\abcother{abc}
\def\abcyetanother{abc}
\expandafter\def\expandafter\abc\expandafter{\abc\ defappended which works}
\xdef\abcother{\abcother\ defappended works as well}
\makeatletter
\g@addto@macro\abcyetanother{\ and here again}
\makeatother
\begin{document}
First version: \abc
Second version: \abcother
Third version: \abcyetanother
\end{document}