2

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.

  • 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 05: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 05:51

1 Answers1

4

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