1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
% 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}
|