LaTeX source code listings

2017/01/15

For our project work at my university we have to include some source code in our LaTeX documentation. Since this is not an very easy part I googled. And found this: LaTeX source code listing like in professional books.

Oh boy does this look good!

That’s why, to save it for eternity (if by chance stackoverflow should go down a day or something), I’ll copy it here:

% http://stackoverflow.com/questions/741985/latex-source-code-listing-like-in-professional-books

\usepackage{listings}
\usepackage{courier}
\lstset{
  basicstyle=\footnotesize\ttfamily, % Standardschrift
  %numbers=left, % Ort der Zeilennummern
  numberstyle=\tiny, % Stil der Zeilennummern
  %stepnumber=2, % Abstand zwischen den Zeilennummern
  numbersep=5pt, % Abstand der Nummern zum Text
  tabsize=2, % Groesse von Tabs
  extendedchars=true, %
  breaklines=true, % Zeilen werden Umgebrochen
  keywordstyle=\color{red},
    frame=b,
  % keywordstyle=[1]\textbf, % Stil der Keywords
  % keywordstyle=[2]\textbf, %
  % keywordstyle=[3]\textbf, %
  % keywordstyle=[4]\textbf, \sqrt{\sqrt{}} %
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false, % Leerzeichen anzeigen ?
  showtabs=false, % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=5pt,
  framexbottommargin=4pt,
  %backgroundcolor=\color{lightgray},
  showstringspaces=false % Leerzeichen in Strings anzeigen ?
}

\lstloadlanguages{% Check Dokumentation for further languages ...
  %[Visual]Basic
  %Pascal
  %C
  %C++
  %XML
  %HTML
  Java
}

  %\DeclareCaptionFont{blue}{\color{blue}}
  %\captionsetup[lstlisting]{singlelinecheck=false, \
    labelfont={blue}, textfont={blue}}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk] \
  {0.43, 0.35, 0.35,0.01}{\parbox{\textwidth} \
   {\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white, \
  textfont=white,singlelinecheck=false, margin=0pt, \
   font={bf,footnotesize}}

And then in the LaTeX file use this

\lstinputlisting[label=samplecode,caption=Description]{code/foo.java}