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.

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!

share|improve this question
    
Could you please edit your post and add fully compilable (minimum working) example illustrating your situation? That would help solvers tremendously as it is a starting point for them. –  Malipivo Apr 11 at 20:17
    
@Malipivo I will try. However, it will probably be hard to reproduce the broken frame when reducing it to a MWE. My document is currently 67 pages long, so posting the whole thing is not an option either. –  daniero Apr 11 at 20:21
    
@Malipivo There, I managed to reproduce it in a MWE –  daniero Apr 11 at 21:37
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Apr 16 at 10:05

2 Answers 2

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

share|improve this answer

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

share|improve this answer

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.