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