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:

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}

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.
