10

As the title says. I use, among other things, the following in my lstset:

framerule=1pt,
frame=tblr

Some places a page break happens in the middle of my \lstlisting. Fine. But sometimes the whole code of the listing appears on one page, but the bottom frame appears on the top of the next page!

Imgur

I know that I can float a particular listing, but I have a long document with many listings, and I don't want to float all of them; Most of them also need to be exactly where they are declared. That they are broken over a page doesn't bother me that much.

Is there something that can stop this from happening, without me needing to adjust each individual listing?

This guy had the same problem, but fixed it by removing spaces in front of keywords in a custom language definition. I too have a custom language definition, but his solution doesn't help me. I managed to recreate the situation without a custom language.

Edit and MWE

I managed to reproduce it in a minimal working example. Here's a pastebin, if that's helpfull.

\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}

%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000   \hbadness = \tolerance

%% Smaller font
\usepackage{mathptmx}

%% LISTINGS
\usepackage[final]{listings}
\lstset{
    xleftmargin=1.5em,
    framexleftmargin=1em,
    framextopmargin=0.5ex,
%%  THE FRAME:
    framerule=1pt,
    frame=tblr
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{lipsum}     % for demo
\begin{document}
\subsubsection{Lorem}   % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum} 
\lipsum[1] and then some text

\begin{lstlisting}
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{lstlisting}

Can you believe it..?!
\end{document}

This MWE is rather long. My document inputs most of the listings stuff from files written by others, so I'm not sure what all of it does. If anyone sees some lines that they are sure is irrelevant, feel free to take it out of the MWE.

"Fun fact"

An equally silly situation occurs if you remove \usepackage{mathptmx}: The whole code listing will then appear on the new page, except for the top frame which is left on the bottom of the first page!

5

2 Answers 2

6

Frames with listings package are known to be problematic.

I've created a new environment mylisting with tcolorbox that reproduces your \lstset but has not those problems.

MWE

\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}

%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000   \hbadness = \tolerance

%% Smaller font
\usepackage{mathptmx}

%% LISTINGS
\usepackage[final]{listings}

\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}
\newtcblisting{mylisting}{
      arc=0pt,
      top=0mm,
      bottom=0mm,
      left=0mm,
      right=0mm,
      colback=white,
      breakable,
      boxrule=1pt,
      listing only,
      listing options={xleftmargin=1.5em,framexleftmargin=1em,framextopmargin=0.5ex}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{lipsum}     % for demo
\begin{document}
\subsubsection{Lorem}   % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum}
\lipsum[1]

\begin{mylisting}
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{mylisting}

Can you believe it..?!
\end{document} 

enter image description here

4

You can use the very powerful tcolorbox package that supports listings.

Here's an example, the frame can be customized in various ways. I used the optional argument to the danlisting environment just to show that you use it for passing options to lstlisting. The environment is breakable across pages.

\documentclass[UKenglish,a4paper,11pt]{report}
\usepackage{babel}

%% Adjustments
\addtolength{\topmargin}{-1\baselineskip}
\addtolength{\textheight}{4\baselineskip}
\pretolerance = 2000
\tolerance = 5000   \hbadness = \tolerance

%% Smaller font
\usepackage{mathptmx}

%% LISTINGS
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}

\newtcblisting{danlisting}[1][]{%
  breakable,
  listing only,
  listing options={#1},
}

%\lstset{
%    xleftmargin=1.5em,
%}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{lipsum}     % for demo
\begin{document}
\subsubsection{Lorem}   % some fiddling to get the "wrong spacing just right":
\lipsum[1-3]
\subsubsection{Ipsum} 
\lipsum[1] and then some text

\begin{danlisting}[xleftmargin=1.5em,basicstyle=\ttfamily]
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{danlisting}

Can you believe it..?!

\begin{danlisting}[xleftmargin=1.5em,basicstyle=\ttfamily]
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
Here is a listing with
three lines of code;
The bottom border is placed on the next page!
\end{danlisting}

\end{document}

enter image description here

You must log in to answer this question.

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