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.

2
  • 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
    Commented Dec 14, 2012 at 5:26
  • 1
    another alternative might be minted,you can search here by the tags listings and minted to narrow down search results Commented Dec 14, 2012 at 5:51

1 Answer 1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .