Debian Codegraph @

texmf-dist/doc/latex/verbments/verbments.tex

raw
1 % This file requires Pygments and Python installed on the computer,
2 % as well as the -shell-escape LaTeX compiler option
3 %
4 \PassOptionsToPackage{svgnames}{xcolor}
5 \documentclass{article}
6
7 \usepackage{doc}
8 \usepackage{url,multicol}
9 \usepackage{hyperref}
10 \usepackage{verbments}
11
12 \hypersetup{
13 pdftitle=The verbments package: Pretty printing source code in LaTeX,
14 pdfauthor=Dejan Zivkovic,
15 pdfsubject={verbments LaTeX package manual},
16 colorlinks, linkcolor={DarkBlue},
17 citecolor={black}, urlcolor={black}
18 }
19
20 % Special dashes
21 \def\thinskip{\hskip 0.16667em\relax}
22 \def\endash{--}
23 \def\emdash{\endash-}
24 \def\d@sh#1#2{\unskip#1\thinskip#2\thinskip\ignorespaces}
25 \def\dash{\d@sh\nobreak\endash}
26 \def\Dash{\d@sh\nobreak\emdash}
27
28 % A list of options for a package/class
29 \newenvironment{optlist}{\begin{description}%
30 \renewcommand\makelabel[1]{%
31 \descriptionlabel{\mdseries\textsf{##1}}}%
32 \itemsep0.25\itemsep}%
33 {\end{description}}
34 %
35 \newenvironment{verlist}{\begin{description}%
36 \renewcommand\makelabel[1]{%
37 \descriptionlabel{\bfseries{##1}}}%
38 \itemsep0.25\itemsep}%
39 {\end{description}}
40 %
41 \setlength{\parindent}{0pt}
42 \addtolength{\parskip}{0.3\baselineskip}
43 \setlength{\columnseprule}{0.6pt}
44 \newcommand{\vmPackage}{\texttt{verbments}}
45 \MakeShortVerb{\|}
46
47 \begin{document}
48
49 \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).}}
50 \author{Dejan \v{Z}ivkovi\'{c}\\{\small Singidunum University, Serbia}\\[-1mm]{\footnotesize\href{mailto:[email protected]}
51 {\nolinkurl{[email protected]}}}}
52 \date{Version 1.2\\ August 20, 2011}
53
54 \maketitle
55
56 \begin{abstract}
57 \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.
58 \end{abstract}
59
60 \begin{multicols}{2}
61 \setlength{\parskip}{0pt}
62 \tableofcontents
63 \end{multicols}
64
65 \section{Introduction}
66
67 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.
68
69 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}.)
70
71 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}.
72
73 \section{Installation}
74
75 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.
76
77 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.)
78
79 \section{Basic usage}
80
81 \DescribeEnv{{-}shell-escape}
82 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.
83
84 \DescribeEnv{pyglist}
85 Using \vmPackage{} in a \LaTeX{} document is straightforward\Dash you simply enclose a source code in the \texttt{pyglist} environment:
86
87 \def\lb{\{}
88 \def\rb{\}}
89 \begin{Verbatim}[commandchars=+\{\},gobble=1,fontsize=\MacroFont]
90 \begin+lb{pyglist}+rb[+meta{options}]
91 +meta{source code}
92 \end+lb{pyglist}+rb
93 \end{Verbatim}
94
95 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:
96
97 \begin{verbatim}
98 \begin{pyglist}[language=java]
99 // Hello Java program
100 import java.util.*;
101 public class Hello {
102 public static void main(String[] args) {
103 Scanner kb = new Scanner(System.in);
104 System.out.print("What is your name? ");
105 String name = kb.nextLine();
106 System.out.println("Hello " + name + "!");
107 }
108 }
109 \end{pyglist}
110 \end{verbatim}
111
112 This environment uses the default Pygments style and the Java example code is typeset as follows:
113
114 \bigskip
115 \begin{pyglist}[language=java,fontsize=\MacroFont]
116 // Hello Java program
117 import java.util.*;
118 public class Hello {
119 public static void main(String[] args) {
120 Scanner kb = new Scanner(System.in);
121 System.out.print("What is your name? ");
122 String name = kb.nextLine();
123 System.out.println("Hello " + name + "!");
124 }
125 }
126 \end{pyglist}
127
128 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.,
129
130 \begin{verbatim}
131 \begin{pyglist}[language=java,texcl=true,style=vs,bgcolor=Moccasin]
132 // \fbox{Hello} Java program
133 .
134 .
135 .
136 \end{pyglist}
137 \end{verbatim}
138
139 then the source code is typeset with the Visual Studio style and Moccasin background color:
140
141 \bigskip
142 \begin{pyglist}[language=java,texcl=true,style=vs,bgcolor=Moccasin,fontsize=\MacroFont]
143 // \fbox{Hello} Java program
144 import java.util.*;
145 public class Hello {
146 public static void main(String[] args) {
147 Scanner kb = new Scanner(System.in);
148 System.out.print("What is your name? ");
149 String name = kb.nextLine();
150 System.out.println("Hello " + name + "!");
151 }
152 }
153 \end{pyglist}
154
155 \bigskip
156 \DescribeMacro{\plset}
157 Since the option list of the \texttt{pyglist} environment may become lengthy, options can be globally specified using the |\plset| command. For example:
158
159 \begin{verbatim}
160 \plset{language=java,texcl=true,style=vs,bgcolor=Moccasin}
161 \end{verbatim}
162
163 With the options globally set in this way, now to get the previous highlighted example code it is enough to specify:
164
165 \begin{verbatim}
166 \begin{pyglist}
167 // \fbox{Hello} Java program
168 .
169 .
170 .
171 \end{pyglist}
172 \end{verbatim}
173
174 \section{Captions}
175
176 Source code listings can containg captions. For example,
177
178 \begin{verbatim}
179 \begin{pyglist}[language=java,caption={First Java program}]
180 // Hello Java program
181 .
182 .
183 .
184 \end{pyglist}
185 \end{verbatim}
186
187 produces the following result:
188
189 \begin{pyglist}[language=java,caption={First Java program},fontsize=\MacroFont]
190 // Hello Java program
191 .
192 .
193 .
194 \end{pyglist}
195
196 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,
197
198 \begin{verbatim}
199 \begin{pyglist}[language=java,caption={First Java program},%
200 listingname=\textbf{Program}]
201 // Hello Java program
202 .
203 .
204 .
205 \end{pyglist}
206 \end{verbatim}
207
208 produces the following result:
209
210 \begin{pyglist}[language=java,caption={First Java program},%
211 listingname=\textbf{Program},fontsize=\MacroFont]
212 // Hello Java program
213 .
214 .
215 .
216 \end{pyglist}
217
218 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,
219
220 \begin{verbatim}
221 \begin{pyglist}[language=java,caption={First Java program},%
222 listingnamefont=\sffamily\bfseries\color{yellow},%
223 captionfont=\sffamily\color{white},captionbgcolor=gray]
224 // Hello Java program
225 .
226 .
227 .
228 \end{pyglist}
229 \end{verbatim}
230
231 produces the following result:
232
233 \begin{pyglist}[language=java,caption={First Java program},%
234 listingnamefont=\sffamily\bfseries\color{yellow},%
235 captionfont=\sffamily\color{white},captionbgcolor=gray,fontsize=\MacroFont]
236 // Hello Java program
237 .
238 .
239 .
240 \end{pyglist}
241
242 \bigskip
243 \DescribeMacro{\listofpyglistings}
244 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.
245
246 \bigskip
247 \DescribeMacro{\listofpyglistingsname}
248 Heading of the list of listings is |Listings| by default. This can be changed using the |\listofpyglistingsname| command. For example:
249
250 \begin{verbatim}
251 \listofpyglistingsname{List of Programs}
252 \end{verbatim}
253
254 %\begin{center}*\quad*\quad*\end{center}\nopagebreak[4]
255 \section{\texttt{fancyvrb} options}
256
257 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,
258
259 \begin{verbatim}
260 \fvset{frame=single}
261 \begin{pyglist}[language=c,style=tango,numbers=left,numbersep=5pt]
262 /* Hello World program */
263
264 #include<stdio.h>
265
266 main()
267 {
268 printf("Hello World");
269 }
270 \end{pyglist}
271 \fvset{frame=none}
272 \end{verbatim}
273
274 produces the following result:
275
276 \bigskip
277 \fvset{frame=single}
278 \begin{pyglist}[language=c,style=tango,numbers=left,numbersep=5pt,fontsize=\MacroFont]
279 /* Hello World program */
280
281 #include<stdio.h>
282
283 main()
284 {
285 printf("Hello World");
286 }
287 \end{pyglist}
288 \fvset{frame=none}
289
290 \bigskip
291
292 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,
293
294 \begin{verbatim}
295 \begin{pyglist}[language=java,caption={First Java program},%
296 listingnamefont=\sffamily\bfseries\color{white},%
297 captionfont=\sffamily\color{white},captionbgcolor=gray,%
298 fvset={frame=bottomline,framerule=4pt,rulecolor=\color{gray}}]
299 // Hello Java program
300 .
301 .
302 .
303 \end{pyglist}
304 \end{verbatim}
305
306 produces the following result:
307
308 \begin{pyglist}[language=java,caption={First Java program},%
309 listingnamefont=\sffamily\bfseries\color{white},%
310 captionfont=\sffamily\color{white},captionbgcolor=gray,%
311 fvset={frame=bottomline,framerule=4pt,rulecolor=\color{gray}},%
312 fontsize=\MacroFont]
313 // Hello Java program
314 .
315 .
316 .
317 \end{pyglist}
318
319 \section{Options}
320
321 The following is a full list of available options for the \texttt{pyglist} environment.
322
323 \begin{optlist}
324 \item[abovecaptionskip (dimension)]
325 White space length above caption of the listing (default: |\bigskipamount|).
326 \item[belowcaptionskip (dimension)]
327 White space length below caption of the listing (default: |0pt|).
328 \item[bgcolor (color name)]
329 Background color of the listing (default: |white|).
330 \item[caption (string)]
331 Caption text of the listing (default: \emph{none}).
332 \item[captionbgcolor (color name)]
333 Background color of the caption text and label of the listing (default: \emph{none}).
334 \item[captionfont (font spec)]
335 Font (and possibly other) specifications prepended to the caption text of the listing (default: |{}|).
336 \item[encoding (string)]
337 Encoding of intermediate input and output files with syntax markup used by Pygments (default: |latin1|). For more information, please refer to the Pygments documentation.
338 \item[fontsize (font size)]
339 Font size to use for the listing (default: |auto|\Dash the same as the current font).
340 \item[fvset (options)]
341 The |fancyvrb| package options to use locally for the listing (default: \emph{none}). For more information, please refer to the |fancyvrb| documentation.
342 \item[gobble (integer)]
343 Removes the first $n$ characters from each input line of the source code (default: |0|).
344 \item[language (string)]
345 Language of the source code whose syntax is to be highlighted by Pygments (default: |text|). For more information, please refer to the Pygments documentation.
346 \item[label (string)]
347 Label that makes the listing referable via |\ref{label}| (default: \emph{none}).
348 \item[listingname (string)]
349 Caption label to use as a prefix to the caption text of the listing (default: |Listing|).
350 \item[listingnamefont (font spec)]
351 Font (and possibly other) specifications prepended to the caption label of the listing (default: |{}|).
352 \item[mathescape (boolean)]
353 Enables \LaTeX{} math mode inside comments (default: |false|).
354 \item[numbers (none$\mid$left$\mid$right)]
355 Numbering of the listing lines (default: \emph{none}).
356 \item[numbersep (dimension)]
357 Gap between numbers and start of the listing lines (default: |12pt|).
358 \item[showspaces (boolean)]
359 Enables visible spaces. The space character is printed as the symbol \verb*/ / (default: \emph{none}).
360 \item[style (string)]
361 Style for Pygments to use for the source code highlighting (default: |default|). For more information, please refer to the Pygments documentation.
362 \item[texcl (boolean)]
363 Enables \LaTeX{} code inside comments (default: |false|).
364 \end{optlist}
365
366 \section{Todo list}
367
368 \begin{itemize}
369 \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.)
370 \item Implement escape sequences. (This is probably more suitable task for the Pygments lexers and formatters?)
371 %\item More testing.
372 \end{itemize}
373
374 \section{Version history}
375
376 \begin{verlist}
377 \item[v1.2\quad 2011/08/20] \mbox{}\\
378 Added |captionbgcolor|, |captionfont|, |listingnamefont|, and |fvset| options.\\
379 Did minor code cleaning.\\
380 Revised documentation to include the new options.
381
382 \item[v1.1\quad 2011/07/07] \mbox{}\\
383 Added the |\listofpyglistingsname| user command.\\
384 Changed handling of the list of listings.\\
385 Revised documentation and examples.
386
387 \item[v1.0\quad 2011/06/07] \mbox{}\\
388 Initial public release.
389 \end{verlist}
390
391 \end{document}