Page LayoutKeeping code listing on one page

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
jannetta
Posts: 46
Joined: Mon Dec 22, 2008 12:59 pm

Keeping code listing on one page

Post by jannetta »

Hi folks

I have some code listings in my dissertation. I don't want these listings to be split between pages. How do I keep the complete listing on one page? I tried using minipage and it seems to do the job except that it does something strange with the caption. The caption is printed one word per line eg:

Code: Select all

Listing 2:
The
ex-
tracted
method
in stead of

Code: Select all

Listing 2: The extracted method
Following is an MWE. It produces the listing in the format I require and the two listings have some text in between which, forces the required page breaks:

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[paper=a4paper,left=30mm,width=150mm,top=25mm,bottom=25mm]{geometry} 
\usepackage{listings} % For code listings
\usepackage{xcolor} % Driver-independent color extensions for LaTeX and pdfLaTeX.
\usepackage{setspace}
\doublespacing
\lstset{tabsize=2,keywordstyle=\color{red}\bfseries,commentstyle=\color{green}\emph}
\begin{document}
In issuing this volume of my Mathematical Puzzles, of which some have appeared in periodicals and others are given here for the first time, I must acknowledge the encouragement that I have received from many unknown correspondents, at home and abroad, who have expressed a desire to have the problems in a collected form, with some of the solutions given at greater length than is possible in magazines and newspapers. Though I have included a few old puzzles that have interested the world for generations, where I felt that there was something new to be said about them, the problems are in the main original. It is true that some of these have become widely known through the press, and it is possible that the reader may be glad to know their source.

\begin{minipage}
\singlespacing
\lstset{language=Perl}
\begin{lstlisting}[numbers=left, caption=Perl script to confirm returned data length]
my $url = 
  'http://192.168.0.7:8080/POG/getINDI.jsp?zarefno=a1.';
use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;
print $content;
print "\n";
print "Length " + length($content)
\end{lstlisting}
\doublespacing
\end{minipage}

In issuing this volume of my Mathematical Puzzles, of which some have appeared in periodicals and others are given here for the first time, I must acknowledge the encouragement that I have received from many unknown correspondents, at home and abroad, who have expressed a desire to have the problems in a collected form, with some of the solutions given at greater length than is possible in magazines and newspapers. Though I have included a few old puzzles that have interested the world for generations, where I felt that there was something new to be said about them, the problems are in the main original. It is true that some of these have become widely known through the press, and it is possible that the reader may be glad to know their source.
In issuing this volume of my Mathematical Puzzles, of which some have appeared in periodicals and others are given here for the first time, I must acknowledge the encouragement that I have received from many unknown correspondents, at home and abroad, who have expressed a desire to have the problems in a collected form, with some of the solutions given at greater length than is possible in magazines and newspapers. Though I have included a few old puzzles that have interested the world for generations, where I felt that there was something new to be said about them, the problems are in the main original. It is true that some of these have become widely known through the press, and it is possible that the reader may be glad to know their source.

\singlespacing
\begin{minipage}{}
\lstset{language=Java}
\begin{lstlisting}[caption=The extracted method, numbers=left]
private String retrieveRecord(String sql) {
	String ret = "0";
	try {
		stmt = con.createStatement();
		stmt.executeQuery(sql);
		ResultSet rs = stmt.getResultSet();
		if (rs.next()) {
			ret += " @" + rs.getString("PersoonID") + "@ INDI\n";
			ret += "1 NAME " + rs.getString("voorname");
			ret += " /" + rs.getString("van") + "/\n";
			ret += "1 SEX " 
					+ (rs.getString("geslag")).replace('V', 'F')
					+ "\n";
			ret += "1 BIRT \n";
			ret += "2 DATE "
					+ ((rs.getString("geboortedag").equals("0")) ? "" 
							: rs.getString("geboortedag")
					+ " ")
					+ ((rs.getString("EngAfk").equals("0")) ? "" 
							: rs.getString("EngAfk")
					+ " ")
					+ ((rs.getString("geboortejaar").equals("0")) ? "" 
							: rs.getString("geboortejaar"));
			ret += "\n";
			ret += "2 PLAC " + rs.getString("dorp");
		}
		return ret;
	} catch (SQLException e) {
		e.printStackTrace();
		return ret;
	}
}

\end{lstlisting}
\doublespacing
\end{minipage}
\end{document}
Can anyone perhaps help?

Regards
Jannetta

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Keeping code listing on one page

Post by localghost »

The minipage environment needs a width as mandatory argument. You forgot to specify this length in both cases. The compiler makes you aware of that by giving according error messages.


Best regards
Thorsten¹
jannetta
Posts: 46
Joined: Mon Dec 22, 2008 12:59 pm

Re: Keeping code listing on one page

Post by jannetta »

Thanks Thorsten. That sorted it out.
Post Reply