Producing Quality Hard Copies Of Code With LaTeX

For the last Programming Language Technology assignment, we were told to print out our code and hand it in at reception rather than submit online (And have the lecturer print everything out, presumably). This presented a slight problem. How do you make code look good when printed out? Printing using Notepad or similar gives you each class on a page of its own, this is not good. Also, the font used is horrible, the typesetting is awful and adding page numbering and such is a potential nightmare. So, having had some experience with TeX, I decided that using LaTeX would indeed be the best way of formatting my code in a nice way.

So, to start with I created a new LaTeX document on raptor. The code listing for the first few lines follows.

\documentclass[a4paper,12pt]{article}
\title{CO524 Assessment 3}
\author{Thomas Paul Carlson (tpc4)}
\date{29th November 2006}

Now, that's pretty simple to understand. The first line sets up the type of paper being used and the size of font. Article is the style to be used - other styles generally have the title on a different page to the rest of the content. You can tweak these as you see fit.

\usepackage{fullpage}
\usepackage{listings}
\lstset{language=Java}

The above is what makes this really powerful. We import the "fullpage" package, which does nothing except tell LaTeX to use the whole of the page. Here's a link for the FullPage style. We also import the listings package (Which allows LaTeX to format programming code nicely) and set its default language. We now go ahead and make the title

\begin{document}
\maketitle

Hey, that was easy! Now how about adding some java code?

\section{Assessment3.java}
\lstinputlisting{Assessment3.java}

In the above, the \section bit sets the title of the section. In this case, I wanted it to be the same as the filename for the piece of java code I wanted to add. You can go ahead and repeat the above two lines as many times as you wish and import lots of java. Once you are done...

\end{document}

And that is it. Save what you have produced as "assessmentPrintout.tex" and then you can simply enter the command: "latex assessmentPrintout.tex" and you'll get a lovely DVI document out. If you want it in PDF format instead, just run the DVI through dvipdf, for example "dvipdf assessmentPrintout.dvi assessmentPrintout.pdf". You can then send this to a printer of your choice and have wonderfully laid out, well typeset code. Not bad for a few lines of LaTeX! Note that you may need to jibble the layout of comments if you have any that extend past the borders of the page. LaTeX will warn you about this, and you can see it clearly on the output DVI or PDF files. Note that you can also run your tex document through latex2html and get HTML output :-).

But why go through all this effort? Well, the output is rather cleaner than trying to print using notepad. Typesetting documents nicely is a black art, and LaTeX helps to make the whole process a lot less painful. Remember also that first impressions are rather important, so a nice looking bunch of code is going to make a good impression and a large stack of paper with yucky fonts and generally poor layout is not.

So, the full listing of code for assessmentPrintout.tex...:

\author{Thomas Paul Carlson (tpc4)}
\date{29th November 2006}
\usepackage{fullpage}
\usepackage{listings}
\lstset{language=Java}
\begin{document}
\maketitle
\section{Assessment3.java}
\lstinputlisting{Assessment3.java}
\section{SomethingElse - My magic class!}
\lstinputlisting{SomethingElse.java}
\end{document}

If you have any questions or comments, message "Twigathy" on the internal IRC server (irc.cs.kent.ac.uk) or send an email to T.P.NoSpam.Carlson@kent.ac.uk (Removing the obvious ;)). Best of luck!

Valid XHTML 1.0 StrictValid CSS!