% This file requires Pygments and Python installed on the computer,
% as well as the -shell-escape LaTeX compiler option
%
\PassOptionsToPackage{svgnames}{xcolor}
\documentclass{article}

\usepackage{doc}
\usepackage{url,multicol}
\usepackage{hyperref}
\usepackage{verbments}

\hypersetup{
  pdftitle=The verbments package: Pretty printing source code in LaTeX,
  pdfauthor=Dejan Zivkovic,
  pdfsubject={verbments LaTeX package manual},
  colorlinks, linkcolor={DarkBlue},
  citecolor={black}, urlcolor={black}
}

% Special dashes
\def\thinskip{\hskip 0.16667em\relax}
\def\endash{--}
\def\emdash{\endash-}
\def\d@sh#1#2{\unskip#1\thinskip#2\thinskip\ignorespaces}
\def\dash{\d@sh\nobreak\endash}
\def\Dash{\d@sh\nobreak\emdash}

% A list of options for a package/class
\newenvironment{optlist}{\begin{description}%
  \renewcommand\makelabel[1]{%
    \descriptionlabel{\mdseries\textsf{##1}}}%
  \itemsep0.25\itemsep}%
 {\end{description}}
%
\newenvironment{verlist}{\begin{description}%
  \renewcommand\makelabel[1]{%
    \descriptionlabel{\bfseries{##1}}}%
  \itemsep0.25\itemsep}%
 {\end{description}}
%
\setlength{\parindent}{0pt}
\addtolength{\parskip}{0.3\baselineskip}
\setlength{\columnseprule}{0.6pt}
\newcommand{\vmPackage}{\texttt{verbments}}
\MakeShortVerb{\|}

\begin{document}

\title{The \vmPackage{} package:\\Pretty printing source code in \LaTeX\thanks{This work has been supported by the Serbian Ministry of Education and Science (project III44006).}}
\author{Dejan \v{Z}ivkovi\'{c}\\{\small Singidunum University, Serbia}\\[-1mm]{\footnotesize\href{mailto:dzivkovic@singidunum.ac.rs}
               {\nolinkurl{dzivkovic@singidunum.ac.rs}}}}
\date{Version 1.2\\ August 20, 2011}

\maketitle

\begin{abstract}
\noindent The \vmPackage{} package provides an environment for syntax highlighting of source code in \LaTeX{} documents. The highlighted source code output is formatted via powerful Pygments library of the Python language. 
\end{abstract}

\begin{multicols}{2}
\setlength{\parskip}{0pt}
\tableofcontents
\end{multicols}

\section{Introduction}

The \vmPackage{} package overcomes some deficiencies of two other popular packages for pretty printing source code in \LaTeX{}. Namely, the \texttt{listings} package is relatively old and lacks the utf-8 support. On the other hand, the \texttt{minted} package cannot split the highlighted source code over pages, nor it provides an option for individual highlighting styles.

The \vmPackage{} package uses \texttt{minted} idea to delegate the task of actual syntax markup of the source code to an external software\Dash the Pygments library. Marked code is then typeset using the \texttt{fancyvrb} and \texttt{framed} packages. (The package name \emph{verbments} tries to convey this as being a compound word resembling two words \emph{verbatim} and \emph{pygments}.)

Pygments is written in Python and provides far superior syntax highlighting compared to conventional packages. For example, \texttt{listings} basically only highlights strings, comments, and keywords. Pygments, on the other hand, can be completely customized to markup any token of the source language. Furthermore, the number of supported languages is clearly in favor of Pygments with over 150 different programming and other languages supported. More information on Python and Pygments can be found at \url{http://python.org} and \url{http://pygments.org}.

\section{Installation}

Installation of the \vmPackage{} package itself is simple\Dash the \vmPackage{}.\texttt{sty} file only needs to be on the path where \LaTeX{} can find it. However, \vmPackage{} package additionally requires that the Python language and its Pygments library are installed on the computer. 

Python and Pygments are free software and can be downloaded from their web sites. Bear in mind that Windows support is sketchy at the moment, but instructions to properly configure the software may be found elsewhere on the Internet. (For example, good starting point is the documentation of the \texttt{minted} package.)

\section{Basic usage}

\DescribeEnv{{-}shell-escape}
First of all, don't forget to call the \LaTeX{} complier (\texttt{latex}, \texttt{pdflatex}, or \texttt{xelatex}) by passing it the \texttt{{-}shell-escape} option. 

\DescribeEnv{pyglist}
Using \vmPackage{} in a \LaTeX{} document is straightforward\Dash you simply enclose a source code in the \texttt{pyglist} environment:

\def\lb{\{}
\def\rb{\}}
\begin{Verbatim}[commandchars=+\{\},gobble=1,fontsize=\MacroFont]
\begin+lb{pyglist}+rb[+meta{options}]
   +meta{source code} 
\end+lb{pyglist}+rb
\end{Verbatim}

The \texttt{pyglist} environment accepts a number of options in the \texttt{key=value} notation. For example, to highlight a Java source code, you may use the following \texttt{pyglist} environment:

\begin{verbatim}
\begin{pyglist}[language=java]
// Hello Java program
import java.util.*;
public class Hello {
   public static void main(String[] args) {
      Scanner kb = new Scanner(System.in);
      System.out.print("What is your name? ");
      String name = kb.nextLine();
      System.out.println("Hello " + name + "!");
   }
}
\end{pyglist}
\end{verbatim}

This environment uses the default Pygments style and the Java example code is typeset as follows: 

\bigskip
\begin{pyglist}[language=java,fontsize=\MacroFont]
// Hello Java program
import java.util.*;
public class Hello {
   public static void main(String[] args) {
      Scanner kb = new Scanner(System.in);
      System.out.print("What is your name? ");
      String name = kb.nextLine();
      System.out.println("Hello " + name + "!");
   }
}
\end{pyglist}

If the \texttt{texcl=true}, \texttt{style=vs} and \texttt{bgcolor=Moccasin} options are added to the option list of the previous \texttt{pyglist} environment for a similar Java code, i.e.,

\begin{verbatim}
\begin{pyglist}[language=java,texcl=true,style=vs,bgcolor=Moccasin]
//  \fbox{Hello} Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

then the source code is typeset with the Visual Studio style and Moccasin background color:

\bigskip
\begin{pyglist}[language=java,texcl=true,style=vs,bgcolor=Moccasin,fontsize=\MacroFont]
// \fbox{Hello} Java program
import java.util.*;
public class Hello {
   public static void main(String[] args) {
      Scanner kb = new Scanner(System.in);
      System.out.print("What is your name? ");
      String name = kb.nextLine();
      System.out.println("Hello " + name + "!");
   }
}
\end{pyglist}

\bigskip
\DescribeMacro{\plset}
Since the option list of the \texttt{pyglist} environment may become lengthy, options can be globally specified using the |\plset| command. For example:

\begin{verbatim}
\plset{language=java,texcl=true,style=vs,bgcolor=Moccasin}
\end{verbatim}

With the options globally set in this way, now to get the previous highlighted example code it is enough to specify:

\begin{verbatim}
\begin{pyglist}
// \fbox{Hello} Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

\section{Captions}

Source code listings can containg captions. For example, 

\begin{verbatim}
\begin{pyglist}[language=java,caption={First Java program}]
// Hello Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

produces the following result:

\begin{pyglist}[language=java,caption={First Java program},fontsize=\MacroFont]
// Hello Java program
      .
      .
      .
\end{pyglist}

Caption label used as a prefix to the caption text of a listing is |Listing| by default. This can be changed using the |listingname| option. For example,

\begin{verbatim}
\begin{pyglist}[language=java,caption={First Java program},%
                listingname=\textbf{Program}]
// Hello Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

produces the following result:

\begin{pyglist}[language=java,caption={First Java program},%
                listingname=\textbf{Program},fontsize=\MacroFont]
// Hello Java program
      .
      .
      .
\end{pyglist}

It is also possible to specify the background color of the caption text and label of a listing, as well as their individual font (and possibly other) characteristics. For example,

\begin{verbatim}
\begin{pyglist}[language=java,caption={First Java program},%
       listingnamefont=\sffamily\bfseries\color{yellow},%
       captionfont=\sffamily\color{white},captionbgcolor=gray]
// Hello Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

produces the following result:

\begin{pyglist}[language=java,caption={First Java program},%
       listingnamefont=\sffamily\bfseries\color{yellow},%
       captionfont=\sffamily\color{white},captionbgcolor=gray,fontsize=\MacroFont]
// Hello Java program
      .
      .
      .
\end{pyglist}

\bigskip
\DescribeMacro{\listofpyglistings}
If source code listings in a document are decorated with captions, their list can be produced with the |\listofpyglistings| command. This is akin to the list of figures, list of tables and other table-of-contents counterparts.

\bigskip
\DescribeMacro{\listofpyglistingsname}
Heading of the list of listings is |Listings| by default. This can be changed using the |\listofpyglistingsname| command. For example:

\begin{verbatim}
\listofpyglistingsname{List of Programs}
\end{verbatim}

%\begin{center}*\quad*\quad*\end{center}\nopagebreak[4]
\section{\texttt{fancyvrb} options}

The \texttt{pyglist} environment is actualy typeset using the \texttt{Verbatim} environment of the \texttt{fancyvrb} package. That's why all \texttt{fancyvrb} options are also in effect in the \texttt{pyglist} environment. For a more detailed \texttt{fancyvrb} options description, please refer to the documentation of the \texttt{fancyvrb} package. For example,

\begin{verbatim}
\fvset{frame=single}
\begin{pyglist}[language=c,style=tango,numbers=left,numbersep=5pt]
/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");
}
\end{pyglist}
\fvset{frame=none}
\end{verbatim}

produces the following result:

\bigskip
\fvset{frame=single}
\begin{pyglist}[language=c,style=tango,numbers=left,numbersep=5pt,fontsize=\MacroFont]
/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");
}
\end{pyglist}
\fvset{frame=none}

\bigskip

The \texttt{fancyvrb} options set with the |\fvset| command are  global. They can be also set locally using the |fvset| option of the |pyglist| environment. For example,

\begin{verbatim}
\begin{pyglist}[language=java,caption={First Java program},%
       listingnamefont=\sffamily\bfseries\color{white},%
       captionfont=\sffamily\color{white},captionbgcolor=gray,%
       fvset={frame=bottomline,framerule=4pt,rulecolor=\color{gray}}]
// Hello Java program
      .
      .
      .
\end{pyglist}
\end{verbatim}

produces the following result:

\begin{pyglist}[language=java,caption={First Java program},%
       listingnamefont=\sffamily\bfseries\color{white},%
       captionfont=\sffamily\color{white},captionbgcolor=gray,%
       fvset={frame=bottomline,framerule=4pt,rulecolor=\color{gray}},%
       fontsize=\MacroFont]
// Hello Java program
      .
      .
      .
\end{pyglist}

\section{Options}

The following is a full list of available options for the \texttt{pyglist} environment. 

\begin{optlist}
  \item[abovecaptionskip (dimension)]
    White space length above caption of the listing (default: |\bigskipamount|).
  \item[belowcaptionskip (dimension)]
    White space length below caption of the listing (default: |0pt|).
  \item[bgcolor (color name)]
    Background color of the listing (default: |white|).
  \item[caption (string)]
    Caption text of the listing (default: \emph{none}).
  \item[captionbgcolor (color name)]
    Background color of the caption text and label of the listing (default: \emph{none}).
  \item[captionfont (font spec)]
    Font (and possibly other) specifications prepended to the caption text of the listing (default: |{}|).
  \item[encoding (string)]
    Encoding of intermediate input and output files with syntax markup used by Pygments (default: |latin1|). For more information, please refer to the Pygments documentation.
  \item[fontsize (font size)]
    Font size to use for the listing (default: |auto|\Dash the same as the current font).
  \item[fvset (options)]
    The |fancyvrb| package options to use locally for the listing (default: \emph{none}). For more information, please refer to the |fancyvrb| documentation.
  \item[gobble (integer)]
    Removes the first $n$ characters from each input line of the source code (default: |0|).
  \item[language (string)]
    Language of the source code whose syntax is to be highlighted by Pygments (default: |text|). For more information, please refer to the Pygments documentation.
  \item[label (string)]
    Label that makes the listing referable via |\ref{label}| (default: \emph{none}).
  \item[listingname (string)]
    Caption label to use as a prefix to the caption text of the listing (default: |Listing|).
  \item[listingnamefont (font spec)]
    Font (and possibly other) specifications prepended to the caption label of the listing (default: |{}|).
  \item[mathescape (boolean)]
    Enables \LaTeX{} math mode inside comments (default: |false|).
  \item[numbers (none$\mid$left$\mid$right)]
    Numbering of the listing lines (default: \emph{none}).
  \item[numbersep (dimension)]
   Gap between numbers and start of the listing lines (default: |12pt|).
  \item[showspaces (boolean)]
    Enables visible spaces. The space character is printed as the symbol \verb*/ / (default: \emph{none}).
  \item[style (string)]
    Style for Pygments to use for the source code highlighting (default: |default|). For more information, please refer to the Pygments documentation.
  \item[texcl (boolean)]
    Enables \LaTeX{} code inside comments (default: |false|).
\end{optlist}

\section{Todo list}

\begin{itemize}
\item Implement package distribution file. (Problems: (1) the \% sign in the Pygments style file is gobbled up by |\DocStrip| in examples; (2) spurious blanks appear in the highlighted example code.)
\item Implement escape sequences. (This is probably more suitable task for the Pygments lexers and formatters?)
%\item More testing.
\end{itemize}

\section{Version history}

\begin{verlist}
\item[v1.2\quad 2011/08/20] \mbox{}\\
Added |captionbgcolor|, |captionfont|, |listingnamefont|, and |fvset| options.\\
Did minor code cleaning.\\
Revised documentation to include the new options.

\item[v1.1\quad 2011/07/07] \mbox{}\\
Added the |\listofpyglistingsname| user command.\\
Changed handling of the list of listings.\\
Revised documentation and examples.

\item[v1.0\quad 2011/06/07] \mbox{}\\
Initial public release.
\end{verlist}

\end{document}
