I'm trying to renew a command in terms of its old definition... something similar to this:
\renewcommand{\vec}[1]{\vec{\mathbf{#1}}}
But this seems to send the interpreter in an infinite loop. How do I do this properly?
I'm trying to renew a command in terms of its old definition... something similar to this:
\renewcommand{\vec}[1]{\vec{\mathbf{#1}}}
But this seems to send the interpreter in an infinite loop. How do I do this properly?
You can use the \let command to help you as follows
\let\oldvec\vec
\renewcommand{\vec}[1]{\oldvec{\mathbf{#1}}}
Here's a complete MWE
% arara: pdflatex
\documentclass{article}
\let\oldvec\vec
\renewcommand{\vec}[1]{\oldvec{\mathbf{#1}}}
\begin{document}
$\vec{x}+\vec{y}$
\end{document}
You can avoid defining an "intermediate" command in some cases; consider the following code:
\documentclass{article}
\newcommand{\foo}[1]{\mbox{#1}}
\show\foo
\begingroup\def\temp{\renewcommand\foo[1]}
\expandafter\expandafter\expandafter\endgroup
\expandafter\temp\expandafter{\foo{\textbf{#1}}}
\show\foo
\show\temp
The log file will show
> \foo=\long macro:
#1->\mbox {#1}.
l.3 \show\foo
?
> \foo=\long macro:
#1->\mbox {\textbf {#1}}.
l.8 \show\foo
?
> \temp=undefined.
l.9 \show\temp
so you can see that \foo has been redefined as the original \foo, but with the argument printed boldface, whereas \temp is still undefined. So we actually use an intermediate command, but its definition is forgotten as soon as the redefinition is performed.
This works with \vec; however \vec is not a macro with argument, as \show\vec will show:
> \vec=macro:
->\mathaccent "017E\relax .
where \mathaccent is a TeX primitive. Yes, this works:
\begingroup\def\temp{\renewcommand\vec[1]}
\expandafter\expandafter\expandafter\endgroup
\expandafter\temp\expandafter{\vec{\textbf{#1}}}
I leave to your judgement as a programmer if this is clearer than
\let\LaTeXvec\vec
\renewcommand\vec[1]{\LaTeXvec{\mathbf{#1}}}
TeX is different from other programming languages, because it works mainly by macro expansion and a macro can have just one definition at a given time.
However, one has to take much care not only to check whether \LaTeXvec is defined (using a complicated prefix rather than old is usually safe). There are big problems if the command we want to save is not defined simply with \newcommand (or \def). See When to use \LetLtxMacro? for some of them. In those cases the \expandafter path shown above will definitely not work.
\let or, better, \LetLtxMacro road is surely preferable.
– egreg
Mar 09 '13 at 22:01
oldvec. Is there any way to avoid that (and also not rely onoldvecbeing undefined in the beginning)? – user541686 Mar 09 '13 at 19:46oldvecshould not be noticeable. And often one might be forced to use the other definition (take for example\varphiand\phiwhich I often change the order of by\let\ophi\phi\let\ovarphi\varphi\let\phi\ovarphi\let\varphi\ophi, etc. It doesn't really matter in terms of efficiency, if thats what you are worried about? – nickpapior Mar 09 '13 at 20:28\expandafter). – Charles Stewart Mar 09 '13 at 21:31