Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.