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
Code: Select all
Listing 2: The extracted method
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}
Regards
Jannetta