11

I have a listing of 6 lines. The listing box does not seem to fit on one page (it is on the bottom of the page) so the rest of the box is printed on the next page. There is nothing actually in the box on the next page. How can I avoid this? Can I tell the listing to put at least 3 lines on the next page?

5
  • Welcome to TeX.SX!
    – Ludovic C.
    Sep 30, 2013 at 7:28
  • Clearly I can put the whole thing in a box. Or just add empty lines. But that would just be a fix for a particular listing. I'd like a somewhat "global" fix.
    – Grey
    Sep 30, 2013 at 7:51
  • 1
    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.
    – jub0bs
    Mar 1, 2014 at 22:00
  • You can either try making the basicstyle smaller or, preferably, split up your code samples—nobody likes reading a page (or pages) full of code. (I mean, unless you're a textbook giving a solution.) Apr 28, 2014 at 13:20
  • related: tex.stackexchange.com/questions/172976/…
    – jub0bs
    Apr 28, 2014 at 15:11

4 Answers 4

3

use

\begin{lstlisting}[float,...]

then it cannot have a pagebreak inside

1
  • Well, yes, but it will not flow with text as the non-float would.
    – alfC
    Feb 23, 2021 at 21:23
1

may be you can use the samepage environment. A very small example

\begin{samepage}
\begin{verbatim}
line 1
line 2
line 3
line 4
line 5
line 6
\end{verbatim}
\end{samepage}
2
  • 1
    Would you like to improve the answer perhaps showing how the samepage environment works? Sep 30, 2013 at 7:30
  • This solution does not work for me. I added \begin{samepage}...\end{samepage} around my lstlisting and there is still one orphan line on the next page.
    – bortzmeyer
    Oct 15, 2013 at 9:15
1

I had a similar problem where a the caption and maybe one line of code would appear at the end of one page and the rest of the code on the next one. What helped me was the needspace package. Its not quite perfect, as I have to set the command in front of every \lstlisting that is broken at the wrong place but its all right for a quick n dirty solution.

% preamble
\usepackage{needspace}

% document
\needspace{5\baselineskip} % reserve at least 5 lines, if there is not enough
                           % space, insert a page break
\begin{lstlisting}[...]
0

You could use the minipage environment, which ensures that everything contained in itself is held together, and, if there is no more room for it, in its entirety, to fit, then it moves itself to the next page where it should have ample room to display (it's only six lines, so it shouldn't have to span multiple pages).

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}


\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna 
aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit 
esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.
\begin{minipage}{\textwidth}
line #1
line #2
line #3
line #4
line #5
line #6
\end{minipage}

\end{document}

You must log in to answer this question.

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