# Redefine marginpar with \renewcommand

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.

-

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.

-
I see! Thanks! Will accept the answer when available. –  Philipp Mar 19 '12 at 11:53

It is always best not to rename TeX or LaTeX primitives, try it this way:

\DeclareRobustCommand{\sidepar}[1]{\marginpar{\textit{#1}}}
\sidepar{test it}

-
I think you forgot braces around \textit{#1} but when added it works fine. Thanks :) –  Philipp Mar 19 '12 at 12:05
@Philipp Thanks –  Yiannis Lazarides Mar 19 '12 at 14:12

Because what you are doing here is "text replacement", i.e., your definition goes like...

\marginpar{test} -> \marginpar{\textit{test}} -> \marginpar{\textit{\marginpar{\textit{test}}} -> ...


etc. The \let in your second code excerpt saves the definition of \marginpar into \oldmarginpar. Then, the new \marginpar uses the old definition plus your italic command.

-

The original posters problem looks solved to me. As a minor addition, the following marginpar variation is working well for me: I'm using marginpars to keep track of notes in-text, to mark things that need more work. To be able to find them, I've added indexing, and optionally disabling their output altogether

% Define new command \marpar to replace  \marginpar.
% usage is identical, \marpar will print the marginpar in fnsize
% (footnotesized) text, which makes the sometimes lengthy comments less
% obtrusive and improves legibility.

\newcommand{\marpar}[1]{\marginpar{\fnsize #1}\index{TODO}}


I like to use the opt environment to allow toggling with tags:

\newcommand{\marpar}[1]{\opt{note}{\marginpar{\fnsize #1}\index{TODO}}}


Another way to suppress output of those modified marginpars if the opt environment cannot be used would be to hide the marginpar inside a macro that never gets called.


-