# How to use listings inside pgfplots captions?

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}[]
\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]
\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}

-
Why don't you simply use \texttt{...} in this case? –  Herbert Aug 29 '12 at 5:53
It is not in the simple example, but I want to use the fancy formatting that listings provides, besides the possibility to use other special characters like curly braces and underscores. –  alfC Aug 29 '12 at 8:19
all that is possible with \texttt{...} for not so long examples which are not longer than one line. –  Herbert Aug 29 '12 at 8:48

The use of \lstinline inside arguments is experimental; see this subsection in the documentation:

5.1 Listings inside arguments

There are some things to consider if you want to use \lstinline or the listing environment inside arguments. Since TeX reads the argument before the "lst- macro" is executed, this package can’t do anything to preserve the input: spaces shrink to one space, the tabulator and the end of line are converted to spaces, TeX's comment character is not printable, and so on. Hence, you must work a bit more. You have to put a backslash in front of each of the following four characters: \{}%. Moreover you must protect spaces in the same manner if: (i) there are two or more spaces following each other or (ii) the space is the first character in the line. That’s not enough: Each line must be terminated with a "line feed" ^^J. And you can’t escape to LaTeX inside such listings!

I am afraid the only solution is then, as suggested in the documentation, to use \{ and \}:

\documentclass[]{article}
\usepackage{pgfplots}
\usepackage[]{listings}

\begin{document}

\begin{tikzpicture}
\begin{axis}[]
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {
( 1, 2 )
( 3, 4)
};
\end{axis}
\end{tikzpicture}

\end{document}


An alternative would be to use the listings interface to fancyvrb (or to use fancyvrb directly):

\documentclass[]{article}
\usepackage{pgfplots}
\usepackage{fancyvrb}

\fvset{commandchars=\\\{\}}

\begin{document}
\Verb!(double x)\{bbbb;\}!

\begin{tikzpicture}
\begin{axis}[]
\addplot+[mark=none, line join=round, fill opacity = 0.5, ] coordinates {
( 1, 2 )
( 3, 4)
};
\end{axis}
\end{tikzpicture}

\end{document}


-
ok, that clarifies the current state of things. What is most annoying is that the code inside an argument must be different than outside the argument. I can afford the extra complication of the \{ \}. Which brings another question, is there a dummy environment that I can used outside pgfplots? in such a way that the same code will work inside pgfplots and inside the argument. (for example I tried \par{(double x)\{\}} but it didn't work. –  alfC Aug 29 '12 at 3:12
ok, \textrm seems to put both listings in equal footing (see my question, for proposed code.) If you have a better solution please let me know. –  alfC Aug 29 '12 at 3:21
@alfC I don't seem to see, in your example, any difference with or without \textrm; am I missing something? In the meantime, I provided an alternative in my updated answer. –  Gonzalo Medina Aug 29 '12 at 3:33
The idea is that this code \textrm{\lstinline[]|(double x)\{bbbb\}|} can be used in legendentry or outside without modification, giving the same effect. Your solution using fancyverb is an improvement but note that the curly brackets are omitted in the resulting document. –  alfC Aug 29 '12 at 4:39
@alfC my bad. I forgot to add the commandchars option for fancyvrb. I updated my answer. Using fancyvrb and commandchars as defined in the example, you can use the same string both inside \addlegendentry and in regular text. –  Gonzalo Medina Aug 29 '12 at 5:05
show 1 more comment