I need to add a (general) listings code in a plot caption. For some reason listings (inline) doesn't interact well with \addlegendentry, what can I do to make the same code work outside and inside an \addlegendentry environment?
Note that it even gives an strange result (see image), where the phrase is in the wrong order.
\documentclass[]{article}
\usepackage{pgfplots}
\usepackage[]{listings}
\begin{document}
\lstinline[]|(double x){bbbb}| %ok
\begin{tikzpicture}
\begin{axis}[]
\addlegendentry{\lstinline[]|(double x){bbbb;}|}; %not ok, garbage
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {
( 1, 2 )
( 3, 4)
};
\end{axis}
\end{tikzpicture}
\end{document}

One workaround I found is to put \{...\} in the \legendentry case, but doing so doesn't do the same thing outside the pgfplot and I want to use the same code regardless if possible. Maybe the solution is an option that I have to pass to the tikzpicture environment, or to the tikz package or to the listing.
(These verb-like environments always give me headaches.)
EDIT: Thanks to Gonzalo's clarification, I made this code that allows to use the same code inside and outside the caption, it uses a hack (\textrm, other solutions are welcomed):
\documentclass[]{article}
\usepackage{pgfplots}
\usepackage[]{listings}
\begin{document}
\textrm{\lstinline[]|(double x)\{bbbb\}|} %ok
\begin{tikzpicture}
\begin{axis}[small]
\addlegendentry{\textrm{\lstinline[]|(double x)\{bbbb\}|}}; %now ok
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {( 1, 2 )};
\addlegendentry{\lstinline[]|(double x){bbbb;}|}; % not ok, garbage
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {( 1, 2 )};
\addlegendentry{\lstinline[]|(double x)\{bbbb\}|}; % not what one expects
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {( 1, 2 )};
\end{axis}
\end{tikzpicture}
\end{document}


\texttt{...}in this case? – Herbert Aug 29 '12 at 5:53\texttt{...}for not so long examples which are not longer than one line. – Herbert Aug 29 '12 at 8:48