TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a paper and would like to include some of my "real-world" Java code. What is the best way to accomplish this? I would like to see two templates:

  1. My explanation to the right of my Java Code in a column fashion
  2. All of my Java code pasted in one block and then a description afterwards

I think 2 is probably the best way to go for simplicity.

share|improve this question
Have a look at the listings package. Also useful to search here for [listings] (could possibly include the word java in your query, but answers for other languages will probably also be useful). – Vivi Dec 14 '12 at 5:26
another alternative might be minted,you can search here by the tags listings and minted to narrow down search results – texenthusiast Dec 14 '12 at 5:51
up vote 4 down vote accepted

You can use the listings package like this sample:


while (<@ARGV>)
  { $sum += 1/($_*$_); }
print sqrt(1/$sum), "\n";


Here I have included the sample java code in the document, but you will probably want to keep it in a separate file.


share|improve this answer
Um, that's Perl :-) (Same idea applies of course, but might slightly confuse people.) – Joseph Wright Dec 14 '12 at 7:03
I'm trying this and my code is spilling over the page? – Magpie Dec 19 '12 at 8:07

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.