[Tex/LaTex] Redefine marginpar with \renewcommand

macrosmarginpar

So I was trying to modify the default marginpar command by making the margin text appear in italic. Now the following alone didn't work:

\renewcommand{\marginpar}[1]{\marginpar{\textit{#1}}}

Which to me would seem straight forward. Renew the \marginpar{}command with the same \marginpar{} that is defined by default and expand it by the italic part with the argument. As I mentioned, that didn't work.

This however did work:

\let\oldmarginpar\marginpar
\renewcommand{\marginpar}[1]{\oldmarginpar{\textit{{#1}}}}

So my question is, why is this step necessary to define a command that refers to marginpar and then redefine that alternate command with renewcommand. This process makes no sense to me.

Best Answer

To make sense of the way TeX works you have to remember that it is essentially a macro expansion language, your world view that has the notion "with the same \marginpar{}that is defined by default " doesn't correspond to TeX's world view at all.

With the definition you have, given

\marginpar{123}

TeX sees the first token and replaces it by its definition so

\marginpar{\textit{123}}

TeX then starts processing the stream again, sees the first token is \marginpar so replaces it by its definition

\marginpar{\textit{\marginpar{\textit{123}}}}

and you are in a never ending loop. If you want to save the old definition, you need to do that explicitly, as you found.

Related Question