LaTeX – insert code from external file

The listing package allow you to insert code from an external file.

See the official documentation for more information.

Or copy paste the example below.

You can also write code directly in your .tex file.

\documentclass[11pt]{article}
\usepackage{listings}
%those are just lines to make it look nice
 \lstset{frame=tb,
 language=Java,
 aboveskip=3mm,
 belowskip=3mm,
 showstringspaces=false,
 columns=flexible,
 basicstyle={\small\ttfamily},
 numbers=none,
 numberstyle=\tiny\color{gray},
 keywordstyle=\color{blue},
 commentstyle=\color{dkgreen},
 stringstyle=\color{mauve},
 breaklines=true,
 breakatwhitespace=true
 tabsize=3
 }
\usepackage{color} %this is to add some cool colors
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
 
 \begin{document}
\lstinputlisting[language=java, inputencoding=utf8]{getParent.java}
\end{document}
Advertisements

One thought on “LaTeX – insert code from external file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s