summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/minted/minted.dtx
blob: 56ef060cac35786d9effa1317622dfb3b5f17af2 (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
% \iffalse
% minted.dtx
% Copyright 2010 Konrad Rudolph
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Konrad Rudolph.
%
% This work consists of the files mints.dtx and mints.ins
% and the derived file mints.sty.
%
% Nanos gigantum humeris insidentes.
%
% Special thanks to Philipp Stephani and the rest of the guys from
% comp.text.tex.
%<*driver>
\ProvidesFile{minted.dtx}
%</driver>
%<*package>
\NeedsTeXFormat{LaTeX2e}
%
\ProvidesPackage{minted}[2010/01/08 0.0.4 Yet another Pygments shim for LaTeX]
%
\RequirePackage{keyval}
\RequirePackage{fancyvrb}
\RequirePackage{color}
%</package>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\DisableCrossrefs
%\OnlyDescription

\usepackage{lstdoc}
\usepackage{minted}
\usepackage{courier} ^^A Useful monospace font (= has \bfseries).
\usepackage{hyperref}

\hypersetup{
  pdftitle=The minted package: Highlighted source code in LaTeX,
  pdfauthor=Konrad Rudolph,
  pdfsubject={Minted LaTeX package manual},
}

% Shamelessly stolen from http://blog.karssen.org/2009/11/15/a-latex-example-environment/
\newenvironment{example}
  {\VerbatimEnvironment
   \begin{VerbatimOut}[gobble=3]{example.out}}
  {\end{VerbatimOut}
   \noindent\rule{\textwidth}{0.5pt}
   \begin{minipage}{0.5\linewidth}
     \small\inputminted{latex}{example.out}
   \end{minipage}\qquad
   \begin{minipage}{0.4\linewidth}
     \small\input{example.out}
   \end{minipage}\\
   \noindent\rule{\textwidth}{0.5pt}}

\newenvironment{optionlist}
  {\begin{list}{}{\renewcommand\makelabel[1]{\textsf{##1}}}
   \DeleteShortVerb{\|}\MakeShortVerb{\+}}
  {\end{list}\DeleteShortVerb{\+}\MakeShortVerb{\|}}

\newcommand\thestyle{trac}
\usemintedstyle{\thestyle}

\begin{document}
\DocInput{minted.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{minted} package:\\Highlighted source code in \LaTeX}
% \author{Konrad Rudolph\\\texttt{konrad\char`_rudolph@madrat.net}}
%
% \maketitle
%
% \tableofcontents
%
% \fvset{codes={\catcode`\%=9}} ^^A Ignore initial |%|
%
% \section{Introduction}
%
% \textsf{minted} is a package that allows formatting source code in \LaTeX.
% For example:
% \begin{VerbatimOut}[gobble=1]{minted.doc.out}
%   \begin{minted}{language}
%     code
%   \end{minted}
% \end{VerbatimOut}
% \inputminted[gobble=2]{latex}{minted.doc.out}
%
% \noindent will highlight a piece of code in a chosen language.
% The display can be customized by a number of arguments and colour schemes.
%
% Unlike some other packages, most notably \textsf{listings}, \textsf{minted} requires
% the installation of an additional software, \textsf{Pygments}.
% This may seem like a disadvantage but there are advantages, as well:
%
% \textsf{Pygments} provides far superior syntax highlighting compared to conventional packages.
% For example, \textsf{listings} basically only highlights strings, comments and keywords.
% \textsf{Pygments}, on the other hand, can be completely customized to highlight any token kind the
% source language might support.
% This might include special formatting sequences inside strings, numbers, different kinds of
% identifiers and exotic constructs such as HTML tags.
%
% Some languages make this especially desirable.
% Consider the following Ruby code as an extreme, but at the same time typical, example:
%
% \begin{minted}[gobble=3]{ruby}
%   class Foo
%       def init
%           pi = Math::PI
%           @var = "Pi is approx. #{pi}"
%       end
%   end
% \end{minted}
%
% Here we have three different colours for identifiers (four, if you count keywords) and escapes from
% inside strings, none of which pose a problem to \textsf{Pygments}.
%
% Additionally, installing \textsf{Pygments} is actually incredibly easy (see the next section).
%
% \section{Installation}
%
% \textsf{Pygments} is written in Python so make sure that at least Python 2.6 is installed on you system:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ python --version
%   Python 2.6.2
% \end{Verbatim}
%
% If that's not the case, you can download it from \href{http://www.python.org/download/}{the website} or
% use your operating system's package manager.
%
% You can then install \textsf{Pygments} using the following simple command:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ sudo easy_install Pygments
% \end{Verbatim}
%
% (If you've already got \textsf{Pygments} installed, be advised that \textsf{minted} requires at least version
% 1.2.)
%
% \section{Basic usage}
%
% \subsection{Preliminary}
%
% Since \textsf{minted} makes calls to the outside world (i.e. \textsf{Pygments}), you need to tell the
% \LaTeX{} processor about this by passing it the |-shell-escape| option or it won't allow such calls.
% In effect, instead of calling the processor like this:
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ latex input
% \end{Verbatim}
%
% you need to call it like this:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ latex -shell-escape input
% \end{Verbatim}
%
% The same holds for other processors, such as |pdflatex| or |xelatex|.
%
% \subsection{Formatting source code}
%
% \DescribeEnv{minted}
% Using \textsf{minted} is straightforward. For example, to highlight a Python source code, we might use
% the following code snippet (result on the right):
%
% \begin{example}
%   \begin{minted}{python}
%   def boring(args = None):
%       pass
%   \end{minted}
% \end{example}
%
% Optionally, the environment accepts a number of options in |key=value| notation, which are described
% in more detail below.
%
% \DescribeMacro{\mint}
% For one-line source codes, you can alternatively use a shorthand notation similar to |\verb|:
%
% \begin{example}
%   \mint{python}|import this|
% \end{example}
%
% The complete syntax is \texttt{\char`\\mint\oarg{options}\marg{language}/code/}
% Where the code delimither |/|, like with |\verb|, can be almost any punctuation character. ^^A|
% Again, this command supports a number of options described below.
%
% \DescribeMacro{\inputminted}
% Finally, there's the comment |\inputminted| command to read and format whole files.
% Its syntax is \texttt{\char`\\inputminted\oarg{options}\marg{language}\marg{filename}}.
%
% \subsection{Using different styles}
%
% Instead of using the default style you may choose an another stylesheet provided by \textsf{Pygments}
% by its name.
% For example, this document uses the ``\texttt{\thestyle}'' style.
% \DescribeMacro{\usemintedstyle}
% To do this, put the following into the prelude of your document:
%
% \begin{minted}[gobble=3]{latex}
%   \usemintedstyle{name}
% \end{minted}
%
% To get a list of all available stylesheets, execute the following command on the command line:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ pygmentize -L styles
% \end{Verbatim}
%
% Creating own styles is also very easy. Just follow the instructions provided on the
% \href{http://pygments.org/docs/styles/#creating-own-styles}{website}.
%
% \subsection{Supported languages}
%
% \textsf{Pygments} at the moment supports over 150 different programming languages, template languages
% and other markup languages.
% To see an exhaustive list of the currently supported languages, use the command
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ pygmentize -L lexers
% \end{Verbatim}
%
% \section{Options}
%
% \subsection{Usage}
%
% All \textsf{minted} highlight commands accept the same set of options.
% Options are specified as a comma-separated list of |key=value| pairs.
% For example, we can specify that the lines should be numbered:
%
% \begin{example}
%   \begin{minted}[linenos=true]{c++}
%   #include <iostream>
%   int main() {
%       std::cout << "Hello "
%                 << "world"
%                 << std::endl;
%   }
%   \end{minted}
% \end{example}
%
% An option value of |true| may also be omitted entirely (including the |=|).
% To customize the display of the line numbers further, override the |\theFancyVerbLine| command.
% Consult the \textsf{fancyvrb} documentation for details.

% Here's another example: we want to use the \LaTeX{} math mode inside comments:
%
% \begin{example}
%   \begin{minted}[mathescape]{python}
%   # Returns $\sum_{i=1}^{n}i$
%   def sum_from_one_to(n):
%       r = range(1, n + 1)
%       return sum(r)
%   \end{minted}
% \end{example}
%
% To make your \LaTeX{} code more readable you might want to indent the code inside a |minted|
% environment.
% The option |gobble| removes these unnecessary whitespace characters from the output:
%
% \begin{example}
%   \begin{minted}[gobble=2,showspaces]{python}
%     def boring(args = None):
%         pass
%   \end{minted}
%
%   versus
%
%   \begin{minted}[showspaces]{python}
%     def boring(args = None):
%         pass
%   \end{minted}
% \end{example}
%
% \subsection{Available options}
%
% Following is a full list of available options.
% For more detailed option descriptions please refer to the \textsf{fancyvrb} documentation,
% except where noted otherwise.
% \begin{optionlist}
%   \item[baselinestretch (+auto+\textbar dimension)]:
%     Value to use as for baselinestretch inside the listing (default: +auto+).
%   \item[firstline (integer)]:
%     First line to be shown (default: +1+).
%     All lines before that line are ignored and do not appear in the output.
%   \item[firstnumber (+auto+\textbar integer)]:
%     Line number of the first line (default: +auto+ = 1).
%^^A TODO Why does LaTeX insert *horizontal* bars here instead of vertical bars?!
%   \item[frame (+none+\textbar +leftline+\textbar +topline+\textbar +bottomline+\textbar +lines+\textbar +single+)]:
%     The type of frame to put around the source code listing (default: +none+).
%   \item[framerule (dimension)]:
%     Width of the frame (default: +0.4pt+).
%   \item[framesep (dimension)]:
%     Distance between frame and content (default: +\fboxsep+).
%   \item[gobble (integer)]:
%     Remove the first $n$ characters from each input line (default: +0+).
%   \item[label (+{[string]string}+)]:
%     Assigns the strings as labels to the listing (default: \emph{none}).
%   \item[lastline (integer)]:
%     Last line to be shown (default: \emph{last line of input}).
%   \item[linenos (boolean)]:
%     Enables line numbers (default +false+).
%   \item[mathescape (boolean)]:
%     Enable \LaTeX{} math mode inside comments (default: +false+).
%     Usage as in package \textsf{listings}.
%   \item[numberblanklines (boolean)]:
%     Enables or disables numbering of blank lines (default: +true+).
%   \item[numbersep (dimension)]:
%     Gap between numbers and start of line (default: +12pt+).
%   \item[resetmargins (boolean)]:
%     Resets the left margin inside other environments (default: +false+).
%   \item[rulecolor (color command)]:
%     The color of the frame (default: \emph{black})
%   \item[samepage (boolean)]:
%     Forces the whole listing to appear on the same page, even if it doesn't fit (default: +false+).
%   \item[showspaces (boolean)]:
%     Enables visible spaces: \verb*/visible spaces/ (default: +false+).
%   \item[stepnumber (integer)]:
%     Interval at which line numbers appear (default: +1+).
%   \item[texcl (boolean)]:
%     Enables \LaTeX{} code inside comments (default: +false+).
%     Usage as in package \textsf{listings}.
%   \item[xleftmargin (dimension)]:
%     Indentation to add before the listing (default: +0+).
%   \item[xrightmargin (dimension)]:
%     Indentation to add after the listing (default: +0+).
% \end{optionlist}
%
% \section{To do list}
%
%\begin{itemize}
%  \item Add check for pygmentize installation and version.
%  \item Allow multiple stylesheets in one file.
%  \item Allow quotes in \textsf{fancyvrb} arguments.
%\end{itemize}
%
% \StopEventually{}
%
% \section{Implementation}
%
% \subsection{Option processing}
%
% \begin{macro}{\minted@resetoptions}
%
% Reset options.
%
%    \begin{macrocode}
\newcommand\minted@resetoptions{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@defopt}
%
% Define an option internally and register it with in the
% |\minted@resetoptions| command.
%
%    \begin{macrocode}
\newcommand\minted@defopt[1]{
  \expandafter\def\expandafter\minted@resetoptions\expandafter{%
    \minted@resetoptions
    \@namedef{minted@opt@#1}{}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@opt}
%
% Actually use (i.e. read) an option value.
% Options are passed to |\detokenize| so that |\immediate\write18| will work properly.
%
%    \begin{macrocode}
\newcommand\minted@opt[1]{
  \expandafter\detokenize%
    \expandafter\expandafter\expandafter{\csname minted@opt@#1\endcsname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@opt}
%
% Define a generic option.
%
%    \begin{macrocode}
\newcommand\minted@define@opt[2]{
  \minted@defopt{#1}
  \define@key{minted@opt}{#1}{\@namedef{minted@opt@#1}{#2}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@switch}
%
% Define an option switch (values are either |true| or |false|, and |true| may be
% omitted, e.g. |foobar| is the same as |foobar=true|).
%
%    \begin{macrocode}
\newcommand\minted@define@switch[2]{
  \minted@defopt{#1}
  \define@booleankey{minted@opt}{#1}{
    \@namedef{minted@opt@#1}{#2}}
   {\@namedef{minted@opt@#1}{}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@extra}
%
% Extra options are passed on to \textsf{fancyvrb}.
%
%    \begin{macrocode}
\minted@defopt{extra}
\newcommand\minted@define@extra[1]{
  \define@key{minted@opt}{#1}{
    \expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1=##1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@extra@switch}
%    \begin{macrocode}
\newcommand\minted@define@extra@switch[1]{
  \define@booleankey{minted@opt}{#1}{
    \expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1}}
     {\expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1=false}}}
%    \end{macrocode}
% \end{macro}
%
% \noindent Actual option definitions.
%
%    \begin{macrocode}
\minted@define@switch{texcl}{-P texcomments}
\minted@define@switch{mathescape}{-P mathescape}
\minted@define@switch{linenos}{-P linenos}
\minted@define@opt{gobble}{-F gobble:n=#1}
\minted@define@extra{frame}
\minted@define@extra{framesep}
\minted@define@extra{framerule}
\minted@define@extra{rulecolor}
\minted@define@extra{numbersep}
\minted@define@extra{stepnumber}
\minted@define@extra{firstline}
\minted@define@extra{lastline}
\minted@define@extra{baselinestretch}
\minted@define@extra{label}
\minted@define@extra{xleftmargin}
\minted@define@extra{xrightmargin}
\minted@define@extra@switch{numberblanklines}
\minted@define@extra@switch{showspaces}
\minted@define@extra@switch{resetmargins}
\minted@define@extra@switch{samepage}
%    \end{macrocode}
%
% \subsection{Internal helpers}
%
% \begin{macro}{\minted@savecode}
%
% Save a code to be pygmentized to a file.
%
%    \begin{macrocode}
\newwrite\minted@code
\newcommand\minted@savecode[1]{
  \immediate\openout\minted@code\jobname.pyg
  \immediate\write\minted@code{#1}
  \immediate\closeout\minted@code}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@pygmentize}
%
% Pygmentize the file given as first argument (default: |\jobname.pyg|) using the
% options provided.
%
%    \begin{macrocode}
\newcommand\minted@pygmentize[2][\jobname.pyg]{
  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge \minted@opt{gobble}
    \minted@opt{texcl} \minted@opt{mathescape} \minted@opt{linenos}
    -P "verboptions=\minted@opt{extra}" -o \jobname.out.pyg #1}
  %\immediate\typeout{\minted@cmd} % For debugging.
  \immediate\write18{\minted@cmd}
  \input{\jobname.out.pyg}
  \immediate\write18{rm \jobname.out.pyg}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@usedefaultstyle}
%
% Include the default stylesheet.
%
%    \begin{macrocode}
\newcommand\minted@usedefaultstyle{\usemintedstyle{default}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Public API}
%
% \begin{macro}{\usemintedstyle}
%
% Include stylesheet.
%
%    \begin{macrocode}
\newcommand\usemintedstyle[1]{
  \renewcommand\minted@usedefaultstyle{}
  \immediate\write18{pygmentize -S #1 -f latex > \jobname.pyg}
  \input{\jobname.pyg}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mint}
%
% Highlight a small piece of verbatim code.
% Usage:
% \begin{Verbatim}
%   \mint[options]{language}/code/
% \end{Verbatim}
% where |/| is an arbitrary delimiter, much like for |\verb| and
% fancyvrb's |\Verb|.
%
%    \begin{macrocode}
\newcommand\mint[3][]{
  \DefineShortVerb{#3}
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \SaveVerb[aftersave={
    \UndefineShortVerb{#3}
    \minted@savecode{\FV@SV@minted@verb}
    \minted@pygmentize{#2}
    \immediate\write18{rm \jobname.pyg}}]{minted@verb}#3}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{\minted}
%
% Highlight a longer piece of code inside a verbatim environment.
% Usage:
% \begin{Verbatim}
%   \begin{minted}[options]{language}
%     code
%   \end{minted}
% \end{Verbatim}
%
%    \begin{macrocode}
\newcommand\minted@proglang[1]{}
\newenvironment{minted}[2][]
  {\VerbatimEnvironment
   \renewcommand{\minted@proglang}[1]{#2}
   \minted@resetoptions
   \setkeys{minted@opt}{#1}
   \begin{VerbatimOut}{\jobname.pyg}}%
  {\end{VerbatimOut}
   \minted@pygmentize{\minted@proglang{}}
   \immediate\write18{rm \jobname.pyg}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\inputminted}
%
% Highlight an external source file.
% Usage:
% \begin{Verbatim}
%   \inputminted[options]{language}{path}
% \end{Verbatim}
%
%    \begin{macrocode}
\newcommand\inputminted[3][]{
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \minted@pygmentize[#3]{#2}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Epilogue}
%
% Load default stylesheet -- but only if user has not yet loaded a custom
% stylesheet in the preamble.
%
%    \begin{macrocode}
\AtBeginDocument{
  \minted@usedefaultstyle}
%    \end{macrocode}
%
% \noindent Check whether LaTeX was invoked with |-shell-escape| option.
%
%    \begin{macrocode}
\AtEndOfPackage{
  \ifeof18\PackageError{minted}{You must invoke LaTeX with the -shell-escape
    flag}
  {Pass the -shell-escape flag to LaTeX. Refer to the minted.sty documentation
  for more information.}\fi}
%    \end{macrocode}