# Formatting within theorem broken when using lstlisting

according to other answers here (e.g. Avoid page breaks in \lstlistings, How can I ensure that a listing is not going to be split?) I have put all my listings in a minipage to avoid unwanted pagebreaks. In general I achieved what I wanted, but now I have troubles if listings (I will use a custom environment "code") are used within theorems:

\documentclass{article}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{listings}

\declaretheorem[qed=$\blacktriangle$]{example}

\lstnewenvironment{code}[1][]%
{%
\noindent%
\minipage{\linewidth}%
\vspace{0.5\baselineskip}%
\medskip%
\lstset{#1}}%
%\qedhere .. would be too easy
{\endminipage}

\begin{document}
\begin{example}
\begin{code}[frame=lines]
this is code
\end{code}
\end{example}
\end{document}


Which yields:

I have two problems here:

1. If I have no text in the beginning (or even a paragraph is needed) the listing is next to the "Example 1." text and goes beyond the right page margins.
2. The qedhere is not placed nicely. (Although its debatable how it should be)

Thank you!

Best regards, Markus

You don't have to use a minipage to make code unbreakable. You can set a boolean of listings.

\documentclass{article}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{listings}

\declaretheorem[qed=$\blacktriangle$]{example}
\lstnewenvironment{code}[1][]%
{\mbox{}\endgraf\nopagebreak%
\csname lst@boxtrue\endcsname %make the listings a box
\lstset{boxpos=t,#1}}%
{}

\begin{document}
blblb
\vspace*{35\baselineskip}
\begin{example}
\begin{code}[frame=lines,linewidth=0.8\linewidth]
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
this is code
\end{code}
\end{example}
\end{document}

• Thanks! Yes, this is an option. For me, however, it still leads to page breaks within listings if the listings are nearly one page long. (and with minipage this does not happen) – markus23 Jul 29 '17 at 7:15

Two other solutions based on ntheorem. Either you define a new example environment of style break, that shares the same counter as the ‘normal’ exmpale environment, to be used in such cases. Or you use a normal example environment, but add a command at the beginning to have a line break.

 \documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{amsmath, amssymb}

\usepackage[thmmarks, amsmath]{ntheorem}
\usepackage{listings}

\theoremstyle{plain}
\theoremseparator{.}
\theorembodyfont{\upshape}
\theoremsymbol{\raisebox{-0.45ex}{$\blacktriangle$}}
\newtheorem{example}{Example}
\theoremstyle{break}
\theoremseparator{.\medskip}
\newtheorem{brexample}[example]{Example}

\lstnewenvironment{code}[1][]%
{%
\noindent%
\minipage{\linewidth}%
\vspace{0.5\baselineskip}%
\medskip%
\lstset{#1}}%
{\endminipage}

\begin{document}
%
\begin{example}\NoEndMark\leavevmode\vspace{-1ex}\par
\begin{code}[frame=lines]
this is code
\end{code} \llap{\exampleSymbol}
\end{example}

\begin{brexample}\NoEndMark
\begin{code}[frame=lines]
this is code
\end{code} \llap{\exampleSymbol}
\end{brexample}

\end{document}


• Thank you, I like the llap solution. How did you came up with the -0.45ex for raisebox? Does this work for thmtools, too? (I had to fiddle around with \llap{\raisebox{-6ex}{\qedhere}}, but the -6ex seems to be dependent on the length of the listing when using thmtools.) – markus23 Jul 29 '17 at 7:28
• Trial and error. I think the symbol is not placed exactly on the base line, but a little higher (not on the math axis either). – Bernard Jul 29 '17 at 10:19
• For theorem tools, I didn't try (it cooperates with ntheorem, but for a few features). I'll give a try later. – Bernard Jul 29 '17 at 10:22