Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am looking for LaTeX style files for writing a programming language book (OO programming with Java examples). I'd appreciate if anyone can point me to style files that I can use.

share|improve this question
Use listings package and book document class. –  Please don't touch Feb 7 '12 at 22:41
Perhaps you're interested in Print programs with its proper syntax? –  Werner Feb 7 '12 at 22:45

4 Answers 4

I do not have a package to recomment, other than listings which was already suggested, but I would like to offer one general piece of advice: use the power of TeX to make your examples automatically correct.

What I mean by this is that with TeX/LaTeX you can set up an example environment whose body contains your source code. That source can be automatically written to a file (together with some hidden preamble and some hidden postamble as necessary) so that the resulting file forms a complete program that can be externally compiled. With the right kind of system setup you can then ensure that those compilations are automatically done and that the result is available to you again, e.g., in form of a graphic that can be read in by TeX and displayed next to your input. This way you can be sure that what you describe corresponds 100% to the results you show and this is a huge asset.

I can't tell you how many errors we later found in the first edition of the LaTeX Companion (which was proof read by many people, including professional proof readers) and how much headaches were avoided by switching to such a scheme in the second edition.

I don't think there is a package ready for this kind of approach, as it largely depends on the type of language you document as well as on the environment you have available, e.g., the external formatting needs to be done using something like make and some scripting support, but from my experience it is certainly worth investing in this space. And I know that several other authors have applied the same kind of approach in their publications.

share|improve this answer
One approach in this direction (for inspiration) is the bashful package, which provides this feature for shell code. Maybe it can be configured / adapted to run another Java compiler. –  Daniel Feb 8 '12 at 12:58

In addition to the already-mentioned listings package, you may also want to look into the algorithms, algorithm2e, and algorithmicx packages.

share|improve this answer

For nice typesetting of listings and their output I can also recommend the tcolorbox package. While it's main purpose is to typeset beautiful example boxes (PGF-based), it also has built-in support for verbatim material and especially the listings package to externalize code snippets into own files, which could be helpful to implement Frank's approach.

share|improve this answer
@FrankMittelbach: This one could be interesting for you as well. –  Daniel Feb 8 '12 at 13:09
This is a beautiful package and deserves a bit more promotion. Can anyone of our German speakers make a small translation as to how to use it? If you can I can post a relevant question. –  Yiannis Lazarides Feb 8 '12 at 13:38
@YiannisLazarides: I think this comment should be pointed out in the chat. Of course I can give some hints. –  Marco Daniel Feb 8 '12 at 16:19
@YiannisLazarides: Note that besides the German package documentation there also is an English tcolorbox-examples.tex, which demonstrates most of the features and is pretty self-explanatory. However, regarding promotion, a dedicated question might be helpful :-) –  Daniel Feb 9 '12 at 8:23
@YiannisLazarides: Please see: chat.stackexchange.com/transcript/message/3331785#3331785 –  Marco Daniel Feb 9 '12 at 17:47

I also create "lecture notes" for my programming students. I'm very impressed with memoir class and the minted package for pretty-printed (color coded) output of code. Try it out. The output is pretty good.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.