# \fbox inside listings

I am trying to highlight a specific line inside a listing with listinline. The code I am currently using looks like this:

\begin{lstlisting}[escapechar=!]
class A {
void m() {
B b = ...
!\fbox{b.n();}!
}
}
\end{lstlisting}


This works somewhat, however the text inside the fbox is obviously not processed by the listings package. Using !fbox{\lstinline{b.n();}}! produces an error.

-

It seems to me that listings doesn't allow any verbatim code between two escape characters. One solution would be to store the \lstinline{b.n();} in a box outside the lstlisting and use the box inside it:

\documentclass{article}

\usepackage{listings}
\usepackage{newverbs}

\newsavebox{\mybox}
\begin{document}
\begin{lrbox}{\mybox}
\lstinline{b.n();}%
\end{lrbox}

\begin{lstlisting}[escapechar=!]
class A {
void m() {
B b = ...
!\fbox{\usebox\mybox}!
}
}
\end{lstlisting}

\end{document}

-
That is a bit odd, because !\verb|...|! is allowed –  daleif Apr 5 '11 at 14:16
@daleif: Yes, it is odd. –  Martin Scharrer Apr 5 '11 at 14:45
@MartinScharrer How can I do this but inside the Verbatim environment, and with an fbox containing several lines? –  mljrg Jan 9 '13 at 23:56
Put the Verbatim environment inside lrbox and a minipage environment and use \fbox{\usebox\mybox} again. You can also use my adjustbox package and wrap Verbatim with \begin{adjustbox}{minipage=\linewidth,fbox} .. \end{adjustbox}. Feel free to open a follow-up question if this doesn't help you enough. –  Martin Scharrer Jan 10 '13 at 7:49