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 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
3  
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
1  
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

1 Answer 1

up vote 4 down vote accepted

You can use the listings package like this sample:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{_myperl.pl}
#!/usr/bin/perl
while (<@ARGV>)
  { $sum += 1/($_*$_); }
print sqrt(1/$sum), "\n";
\end{filecontents*}

\usepackage{listings}
\begin{document}
\section{listings}
\lstset{language=Perl}
\lstinputlisting{_myperl.pl}
\end{document}

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

output

share|improve this answer
4  
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

 
discard

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.