34

I find that I much prefer \varphi to \phi and generally would rather use the \varphi than the \phi. But it's three more characters to type \varphi instead of \phi, which is most troublesome. So I would like to swap the two commands. I tried a couple of things on my own, but one resulted in an infinite loop in compilation and the other led to \varphi taking over all of the \phi commands I made. What would be the correct way to do this?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
JSchlather
  • 821
  • 8
  • 16

3 Answers3

40

Try the following:

$\phi\varphi$
\let\temp\phi
\let\phi\varphi
\let\varphi\temp
$\phi\varphi$
André
  • 2,345
  • 2
    Okay, so my issue before is that I was using \renewcommand instead of \let. Could you elaborate on the differences between the two commands? – JSchlather Apr 01 '12 at 18:49
  • 9
    \let\a\b makes \a have the definition that \b has so \let\a\a is a no-op \newcommand\a{\b} makes a expand to \b so \newcommand\a{\a} makes a expand to itself and loop forever – David Carlisle Apr 01 '12 at 18:57
  • 5
    @DavidCarlisle Okay so it's essentially hard linking versus symbolic linking. Which is what I suspected the issue was. Thanks. – JSchlather Apr 01 '12 at 19:04
40

The usual way to exchange two values is to use a temporary command name to store one of them while swapping, but if, for no particular reason, you want to avoid the extra command name then:

\documentclass{article}

\begin{document}

$\phi\varphi$


\expandafter\mathchardef\expandafter\varphi\number\expandafter\phi\expandafter\relax
\expandafter\mathchardef\expandafter\phi\number\varphi

$\phi\varphi$


\end{document}
David Carlisle
  • 757,742
17

For non-complex macros, using an interim macro to swap definitions is sufficient. However, if the macros (say, funcA and funcB) takes optional arguments, you need to use a different approach via letltxmacro's macro \LetLtxMacro{<new macro>}{<old macro>}:

\usepackage{letltxmacro}% http://ctan.org/pkg/letltxmacro
%...
\LetLtxMacro{\temp}{\funcA}
\LetLtxMacro{\funcA}{\funcB}
\LetLtxMacro{\funcB}{\temp}
Werner
  • 603,163