# Verbatim in Marginnote doesn't work

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

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?

-
@user3097: I think your update really should be a new question since the original question was solved. After posting a new question (and linking to this one), you should make a rollback here. (By the way, may I ask what a "really sort quastion" is? :-)) – Hendrik Vogt Jan 24 '11 at 19:41
@user3097: The shortcoming of cprotect that you describe is documented (otherwise, I need to add it to the documentation): when reading \cprotect\marginnote{\verb?}?}, the \cprotect command thinks that the argument is \verb?. It has no way of knowing that the closing brace is to be skipped before actually executing \verb. And then it is too late. Two workarounds: \cprotect\marginnote{\iffalse{\fi\verb?}?} or \cprotect\marginnote{%{ <newline> \verb?}?}. – Bruno Le Floch Jan 24 '11 at 22:08
You could also decide to use both: SaveVerb the pieces that have non-balanced braces, and simply cprotect the rest. – Bruno Le Floch Jan 24 '11 at 22:09
@user3097: I still think you should follow my advice above. Then you'll have a chance of getting the question in your update answered. It would also make your question appear less cluttered. – Hendrik Vogt Feb 13 '11 at 14:57
Great that you changed your user name. Maybe you clean this post up a bit if I keep nagging? :-) – Hendrik Vogt Feb 14 '11 at 9:34

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}}

-

You can for example use the package cprotect, which provides the command \cprotect for precisely this type of use. In your case, use

\cprotect\marginnote{\verb?\documentclass{memoir}?}


Namely,

\documentclass{memoir}
\usepackage{verbatim}
\usepackage{marginnote}
\usepackage{cprotect}
\begin{document}

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

\end{document}

-
First of all Thanks for reply! – Daniel Jan 24 '11 at 18:43

You can now use the new version of my newverbs package to define a verbatim margin note macro. It gives you the same interface as \verb<char>...<char> but also {...}.

\documentclass{memoir}
\usepackage{marginnote}
\usepackage{newverbs}[2011/07/23]
\newcommand*{\verbmarginnote}{\collectverb{\marginnote}}

\usepackage{lipsum}
\begin{document}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et
dolore gubergren. \verbmarginnote|\documentclass{memoir}|
\lipsum

\end{document}

-
I tried the code, and it's working well. What do you think, how could I use instead \marginnote the \sidepar command? It works with memoir class better. Otherwise I found commands in the memoir class like \cs{} and \oarg and \marg. These commands are specially for documentations. They base on the command \char'. In this case, it is also a solution, but not for verbatims with more rows. – Daniel Aug 1 '11 at 16:36
You could write \texttt{\textbackslash documentclass\{memoir\}} instead to avoid the issue with verbatim and place it in any other macro you want. That is what \cs etc. basically does. – Martin Scharrer Aug 1 '11 at 16:53