LaTeX forum ⇒ 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).
jannetta
Posts: 46
Joined: Mon Dec 22, 2008 12:59 pm

Keeping code listing on one page

Postby jannetta » Sun Feb 22, 2009 6:57 pm

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:
  1. Listing 2:
  2. The
  3. ex-
  4. tracted
  5. method


in stead of

  1. 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:

  1. \documentclass[a4paper,12pt]{report}
  2. \usepackage[paper=a4paper,left=30mm,width=150mm,top=25mm,bottom=25mm]{geometry}
  3. \usepackage{listings} % For code listings
  4. \usepackage{xcolor} % Driver-independent color extensions for LaTeX and pdfLaTeX.
  5. \usepackage{setspace}
  6. \doublespacing
  7. \lstset{tabsize=2,keywordstyle=\color{red}\bfseries,commentstyle=\color{green}\emph}
  8. \begin{document}
  9. 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.
  10.  
  11. \begin{minipage}
  12. \singlespacing
  13. \lstset{language=Perl}
  14. \begin{lstlisting}[numbers=left, caption=Perl script to confirm returned data length]
  15. my $url =
  16. 'http://192.168.0.7:8080/POG/getINDI.jsp?zarefno=a1.';
  17. use LWP::Simple;
  18. my $content = get $url;
  19. die "Couldn't get $url" unless defined $content;
  20. print $content;
  21. print "\n";
  22. print "Length " + length($content)
  23. \end{lstlisting}
  24. \doublespacing
  25. \end{minipage}
  26.  
  27. 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.
  28. 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.
  29.  
  30. \singlespacing
  31. \begin{minipage}{}
  32. \lstset{language=Java}
  33. \begin{lstlisting}[caption=The extracted method, numbers=left]
  34. private String retrieveRecord(String sql) {
  35. String ret = "0";
  36. try {
  37. stmt = con.createStatement();
  38. stmt.executeQuery(sql);
  39. ResultSet rs = stmt.getResultSet();
  40. if (rs.next()) {
  41. ret += " @" + rs.getString("PersoonID") + "@ INDI\n";
  42. ret += "1 NAME " + rs.getString("voorname");
  43. ret += " /" + rs.getString("van") + "/\n";
  44. ret += "1 SEX "
  45. + (rs.getString("geslag")).replace('V', 'F')
  46. + "\n";
  47. ret += "1 BIRT \n";
  48. ret += "2 DATE "
  49. + ((rs.getString("geboortedag").equals("0")) ? ""
  50. : rs.getString("geboortedag")
  51. + " ")
  52. + ((rs.getString("EngAfk").equals("0")) ? ""
  53. : rs.getString("EngAfk")
  54. + " ")
  55. + ((rs.getString("geboortejaar").equals("0")) ? ""
  56. : rs.getString("geboortejaar"));
  57. ret += "\n";
  58. ret += "2 PLAC " + rs.getString("dorp");
  59. }
  60. return ret;
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. return ret;
  64. }
  65. }
  66.  
  67. \end{lstlisting}
  68. \doublespacing
  69. \end{minipage}
  70. \end{document}


Can anyone perhaps help?

Regards
Jannetta

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

Postby localghost » Mon Feb 23, 2009 12:17 am

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¹
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

jannetta
Posts: 46
Joined: Mon Dec 22, 2008 12:59 pm

Postby jannetta » Mon Feb 23, 2009 12:26 am

Thanks Thorsten. That sorted it out.


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 6 guests