[Tex/LaTex] Verbatim in Marginnote doesn’t work

marginparverbatim

I have a really sort quastion.
The following Code is not working:

\documentclass{memoir}
\usepackage{verbatim}
\usepackage{marginnote}

\begin{document}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et
dolore gubergren. \marginnote{\verb?\documentclass{memoir}?}%%% --- does not work!!!

\verb?\documentclass{memoir}?%%% it is working, looks pretty

\end{document}

I would like to write something like a package description, a manual that's why I would like to create marginnotes.

The Problem what I get is the following:

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.8 

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! LaTeX Error: \verb illegal in command argument.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.8 

UPDATE

First of all, Thanks for the quick answers 😉

I tested both packages. I find really good the fancyvbr package, it can give me much more as I taught. The cprotect package works also good, BUT
If I leave a bracket in the Verbatim text — like here:

Lorem ipsum dolor sit amet \cprotect\marginnote{\verb?}?}

— then it does not work, I get the message too many {'…
Otherwise it's working in the normal text, but not with marginnote

SaveVerb can be a solution, it's working, but I don't want 300 saved Verbatims 🙂

So I don't really know what to do…

Some idea?

Best Answer

The fancyvrb package defines the \SaveVerb command and SaveVerbatim environment that can be used for that purpose. In fact, an example in the manual for that package talks about that very situation:

\DefineShortVerb{\|}
\SaveVerb{Verb}|_OK^|
\marginpar{\UseVerb{Verb}}
Related Question