4

I am having some issue when it comes to use a frame around my listings. The usage of underbrace breaks the frame.

Can anybody help out and let me know how to fix this?

 \documentclass[a4paper]{scrbook}
 \usepackage{amsmath}
 \usepackage{listings}                  
 \lstset{       
      escapechar = \% ,            
      frame = single         
  }

  \begin{document}
  \noindent\begin{minipage}{\textwidth}
  \begin{lstlisting}      
  %$\underbrace{Lorem ipsum}_{ipsum lorem}$% 
  \end{lstlisting}
  \end{minipage} 
  \end{document}

Result:

https://i.sstatic.net/7Hqey.png

Edit:
Problem is not really solved as I rely on minipage in order to prevent my Listings from being split over two pages.
Actually, the frame around the Listings always "dashes" when escaping.
Does anyone know of a solution how I can keep a frame around a Listing that looks like a usual Listing? Especially, I need a caption to be displayed above the frame (and not inside). Here's a snippet to see what I mean:

\documentclass[a4paper]{scrbook}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{amsmath}
\lstset{mathescape=true, frame = single,  escapechar        = \% }

\begin{document}

\noindent\begin{minipage}{\textwidth}
\lstset{caption = {Caption1},label = {label1}}
\begin{lstlisting}
Blabla %bla\_4%
%$\underbrace{BlaBla}_{Blubb}$%
\end{lstlisting}
\end{minipage} 

\lstset{caption = {Caption2},label = {label2}, frame= none}
\begin{mdframed}
\begin{lstlisting}
$\underbrace{Lorem ipsum}_{ipsum lorem}$
\end{lstlisting}
\end{mdframed}

\end{document}

enter image description here

Edit 2:
For now, I've switched from frame = single to frame = lines which circumvents the issue but I don't like the look of it.

1 Answer 1

4

The framed option works assuming that the height and depth of the lines don't exceed the total height plus depth of a strut (which is the baseline skip). They basically draw a visible strut at both ends.

You can use mdframed:

\documentclass[a4paper]{scrbook}
\usepackage{mdframed}
\usepackage{listings}
\lstset{mathescape=true}

\begin{document}
\begin{mdframed}
\begin{lstlisting}
$\underbrace{Lorem ipsum}_{ipsum lorem}$
\end{lstlisting}
\end{mdframed}
\end{document}

If you want to accommodate also the caption, I suggest a workaround

\documentclass[a4paper]{scrbook}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{lipsum}
\lstset{mathescape}

\lstnewenvironment{framedlisting}[1][]
  {\mdframed[innertopmargin=-2\baselineskip,
      skipabove=\bigskipamount,skipbelow=\bigskipamount]
   \lstset{frame=none,belowcaptionskip=\baselineskip,#1}}
  {\endmdframed}

\begin{document}
\lipsum[1]
\begin{framedlisting}[caption = {Caption2},label = {label2}]
$\underbrace{Lorem ipsum}_{ipsum lorem}$
abc
def
ghy
abc
def
ghy
abc
def
ghy
\end{framedlisting}
\lipsum[2]
\end{document}

enter image description here

2
  • I thoguht this was the solution - but unfortunately doesn't work in combination with captions. Or did I get something wrong? Question updated accordingly... Sep 6, 2012 at 10:57
  • 1
    @FloatingPointZilch I've added a workaround
    – egreg
    Sep 6, 2012 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .