summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/minted/minted.dtx
blob: fcde9e66f463cc99b63606e135d6f0aa5147c4e1 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
% \iffalse meta-comment
% 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.
%
% Additionally, the project may be distributed under the terms of the new BSD
% license.
%
% 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.
% \fi
% \iffalse
%<*driver>
\ProvidesFile{minted.dtx}
%</driver>
%<*package>
\NeedsTeXFormat{LaTeX2e}
%
\ProvidesPackage{minted}[2010/01/27 v1.6 Yet another Pygments shim for LaTeX]
%
\RequirePackage{keyval}
\RequirePackage{fancyvrb}
\RequirePackage{color}
\RequirePackage{float}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{ifplatform}
%</package>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\DisableCrossrefs
\RecordChanges
%\OnlyDescription

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

\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}
   \vspace{1ex}
   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{0.5\linewidth}
     \small\inputminted[resetmargins]{latex}{example.out}
   \end{minipage}
   \hspace{0.05\linewidth}
   \begin{minipage}{0.4\linewidth}
     \small\input{example.out}
   \end{minipage}
   \vspace{1ex}}}

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

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

\setlength{\parindent}{0pt}
\addtolength{\parskip}{0.5\baselineskip}

\begin{document}
\DocInput{minted.dtx}
\end{document}
%</driver>
% \fi
%
% \changes{0.0.4}{2010/01/08}{Initial version}
% \changes{1.6}{2010/01/22}{Simpler versioning scheme}
%
% \GetFileInfo{minted.sty}
%
% \newcommand\pkg[1]{\textsf{#1}}
% \newcommand\app[1]{\textsf{#1}}
%
% \title{The \pkg{minted} package:\\Highlighted source code in \LaTeX
%   \thanks{This document corresponds to \pkg{minted}~\fileversion, last changed~\filedate.}}
% \author{Konrad Rudolph\\\texttt{konrad\char`\_rudolph@madrat.net}}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
%   \noindent\pkg{minted} is a package that facilitates expressive syntax highlighting
%   using the powerful \app{Pygments} library. The package also provides options to
%   customize the highlighted source code output.
% \end{abstract}
%
% \begin{multicols}{2}
%   \setlength{\parskip}{0pt}
%   \tableofcontents
% \end{multicols}
%
% \fvset{
%   codes={\catcode`\%=9}, ^^A Ignore initial |%|
%   numbersep=5pt
% }
% \setlength{\fboxsep}{1ex}
%
%
% \section{Introduction}
%
% \pkg{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}
%
% 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 \pkg{listings}, \pkg{minted} requires
% the installation of an additional software, \app{Pygments}.
% This may seem like a disadvantage but there are advantages, as well:
%
% \app{Pygments} provides far superior syntax highlighting compared to conventional packages.
% For example, \pkg{listings} basically only highlights strings, comments and keywords.
% \app{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 four different colors for identifiers (five, if you count keywords) and escapes from
% inside strings, none of which pose a problem to \app{Pygments}.
%
% Additionally, installing \app{Pygments} is actually incredibly easy (see the next section).
%
%
% \section{Installation}
%
% \app{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.
%
% Next, install \href{http://pypi.python.org/pypi/setuptools}{\app{setuptools}} which facilitates the distribution of Python applications.
%
% You can then install \app{Pygments} using the following simple command:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ sudo easy_install Pygments
% \end{Verbatim}
%
% (If you've already got \app{Pygments} installed, be advised that \pkg{minted} requires at least version
% 1.2.)
%
%
% \subsection{Windows}
%
% Windows support is sketchy at the moment.
% There are two complications: installation and usage.
%
% \begin{description}
%   \item[Installation]
%     The above setting assumes that |easy_install| is in a path that Windows automatically find.
%     to do this, you must usually set your |PATH| environment variable accordingly (e.g. to
%     |C:\Python26\Scripts|).
%   \item[Usage]
%     \app{Pygments} currently does not ship with a Windows compatible application.
%     In order to still run it, you need to create a small command script and put it someplace where
%     Windows will find it (e.g. the aforementioned |Scripts| directory, which you will have registered
%     in the |PATH| variable anyway).
%     The script needs to be called |pygmentize.cmd| and it needs to contain the following content:
%
%     \def\mintedbs#1{\char`\\}
%     \def\mintedpcc#1{\char37\relax}
%     \begin{Verbatim}[gobble=7,commandchars=\\\{\}]
%       @echo off
%       set PYTHONPATH=C:\mintedbs{}Python26
%       \mintedpcc{}PYTHONPATH\mintedpcc{}\mintedbs{}python.exe \mintedpcc{}PYTHONPATH\mintedpcc\mintedbs{}Scripts\mintedbs{}pygmentize \mintedpcc{}*
%     \end{Verbatim}
% \end{description}
%
% \section{Basic usage}
%
%
% \subsection{Preliminary}
%
% Since \pkg{minted} makes calls to the outside world (i.e. \app{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 \pkg{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 \cmd\mint\oarg{options}\marg{language}/\meta{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 \cmd\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 \app{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}
%
% \app{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{Floated listings}
%
% \DescribeEnv{listing}
% \pkg{minted} provides the |listing| environment to wrap around a source code block.
% That way, the source code will be put into a floating box.
% You can also provide a |\caption| and a |\label| for such a listing in the usual way (that is,
% as for the |table| and |figure| environments):
% \begin{VerbatimOut}[gobble=1]{minted.doc.out}
%   \begin{listing}[H]
%     \mint{cl}/(car (cons 1 2))/
%     \caption{Example of a listing.}
%     \label{lst:example}
%   \end{listing}
%
%   Listing \ref{lst:example} contains an example of a listing.
% \end{VerbatimOut}
% \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out}
%
% will yield:
%
% \begin{samepage}
% \rule{\textwidth}{0.4pt}\\
% \input{minted.doc.out}
% \ \\\rule{\textwidth}{0.4pt}\\
% \end{samepage}
%
% \DescribeMacro{\listoflistings}
% The |\listoflistings| macro will insert a list of all (floated) listings into the document:
%
% \begin{example}
%   \listoflistings
% \end{example}
%
% \DescribeMacro{\listingscaption}
% The string ``Listing'' in a listing's caption can be changed.
% To do this, simply redefine the macro |\listingscaption|, e.g.:
%
% \mint{latex}/\renewcommand\listingscaption{Program code}/
%
% \DescribeMacro{\listoflistingscaption}
% Likewise, the caption of the listings list, ``List of listings'' can be changed by redefining
% |\listoflistingscaption| like so:
%
% \mint{latex}/\renewcommand\listoflistingscaption{List of program codes}/
%
%
% \section{Options}
%
%
% \subsection{Usage}
%
% All \pkg{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 \pkg{fancyvrb} documentation for details.
%
% |\mint| accepts the same options:
%
% \begin{example}
%   \mint[linenos]{perl}|$x=~/foo/|
% \end{example}
%
% 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 \pkg{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[bgcolor (string)]
%     Background color of the listing (default: \emph{none}).
%     Notice that the value of this option must \emph{not} be a color command. Instead, it must be a color
%     \emph{name}, given as a string, of a previously-defined color:
%
% \begin{example}
%   \definecolor{bg}{rgb}{0.95,0.95,0.95}
%   \begin{minted}[bgcolor=bg]{php}
%   <?php
%     echo "Hello, $x";
%   ?>
%   \end{minted}
% \end{example}
%
%   \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).
%   \item[fontfamily (family name)]
%     The font family to use (default: +tt+).
%     +tt+, +courier+ and +helvetica+ are pre-defined.
%   \item[fontseries (series name)]
%     The font series to use (default: +auto+ -- the same as the current font).
%   \item[fontsize (font size)]
%     The size of the font to use (default: +auto+ -- the same as the current font).
%   \item[fontshape (font shape)]
%     The font shape to use (default: +auto+ -- the same as the current font).
%   \item[formatcom (command)]
%     A format to execute before printing verbatim text (default: \emph{none}).
%   \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[lastline (integer)]
%     Last line to be shown (default: \emph{last line of input}).
%   \item[linenos (boolean)]
%     Enables line numbers (default +false+).
%     In order to customize the display style of line numbers, you need to redefine the +\theFancyVerbLine+
%     macro:
%
% \begin{example}
%   \renewcommand{\theFancyVerbLine}{\sffamily
%     \textcolor[rgb]{0.5,0.5,1.0}{\scriptsize
%     \oldstylenums{\arabic{FancyVerbLine}}}}
%
%   \begin{minted}[linenos,
%     firstnumber=11]{python}
%   def all(iterable):
%       for i in iterable:
%           if not i: 
%               return False
%       return True
%   \end{minted}
% \end{example}
%
%   \item[mathescape (boolean)]
%     Enable \LaTeX{} math mode inside comments (default: +false+).
%     Do \emph{not} use spaces inside math mode -- they will be rendered like other full-width verbatim spaces.
%     Usage as in package \pkg{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[obeytabs (boolean)]
%     Treat tabs as tabs instead of converting them to spaces (default: +false+).
%   \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[showtabs (boolean)]
%     Enables visible tabs -- only works in combination with +obeytabs+ (default: +false+).
%   \item[stepnumber (integer)]
%     Interval at which line numbers appear (default: +1+).
%   \item[tabsize (integer)]
%     The number of spaces a tab is equivalent to if +obeytabs+ is not active (default: +8+).
%   \item[texcl (boolean)]
%     Enables \LaTeX{} code inside comments (default: +false+).
%     Usage as in package \pkg{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{Defining shortcuts}
%
% Large documents with a lot of listings will nonetheless use the same source language and the
% same set of options for most listings.
% Always specifying all options is redundant, a lot to type and makes performing changes hard.
%
% \pkg{minted} therefore defines a set of commands that lets you define shortcuts for the highlighting
% commands.
% Each shortcut is specific for one programming language.
% \DescribeMacro{\newminted}
% |\newminted| defines a new alias for the |minted| environment:
%
% \begin{example}
%   \newminted{cpp}{gobble=2,linenos}
%
%   \begin{cppcode}
%     template <typename T>
%     T id(T value) {
%         return value;
%     }
%   \end{cppcode}
% \end{example}
%
% If you want to provide extra options on the fly, or override existing default options, you can do that, too:
%
% \begin{example}
%   \newminted{cpp}{gobble=2,linenos}
%
%   \begin{cppcode*}{linenos=false,
%                    frame=single}
%     int const answer = 42;
%   \end{cppcode*}
% \end{example}
%
% Notice the star ``|*|'' behind the environment name -- due to restrictions in \pkg{fancyvrb}'s handling
% of options, it is necessary to provide a \emph{separate} environment that accepts options, and the options
% are \emph{not} optional on the starred version of the environment.
%
% The default name of the environment is \meta{language}|code|.
% If this name clashes with another environment or if you want to choose an own name for another reason, you may
% do so by specifying it as the first argument: \cmd\newminted\oarg{environment name}\marg{language}\marg{options}.
%
% \DescribeMacro{\newmint}
% The above macro only defines shortcuts for the |minted| environment.
% The main reason is that the short command form |\mint| often needs different options -- at the very least, it
% will generally not use the |gobble| option.
% A shortcut for |\mint| is defined using \cmd\newmint\oarg{macro name}\marg{language}\marg{options}.
% The arguments and usage are identical to |\newminted|.
% If no \meta{macro name} is specified, \meta{language} is used.
%
% \begin{example}
%   \newmint{perl}{showspaces}
%
%   \perl/my $foo = $bar;/
% \end{example}
%
%
% \section{To do list}
%
%\begin{itemize}
%  \item Allow multiple stylesheets in one file.
%  \item Allow quotes in \pkg{fancyvrb} arguments.
%\end{itemize}
%
%
% \section{Known issues}
%
% \begin{itemize}
%   \item Extended characters do not work inside the |minted| environment, even in conjunction with
%     package \pkg{inputenc}.
%     \textbf{Solution}: Use |xelatex| instead of plain \LaTeX.
% \end{itemize}
%
% \StopEventually{
%   \addcontentsline{toc}{section}{Change History}
%   \PrintChanges}
%
%
% \section{Implementation}
%
%
% \subsection{System-specific settings}
%
% \changes{1.6}{2010/01/27}{Windows support added}
%
% Since we communicate with the ``outside world'', some operations must be defined system-dependently.
%
% \begin{macro}{\DeleteFile}
%
% Delete a file; we're careful in case someone has already defined this macro elsewhere.
%
%    \begin{macrocode}
\ifwindows
  \providecommand\DeleteFile[1]{\immediate\write18{del #1}}
\else
  \providecommand\DeleteFile[1]{\immediate\write18{rm #1}}
\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TestAppExists}
%
% Check whether a given application exists on the system.
% Usage is a bit roundabout (should be fixed?) -- to test whether an application exists, use the
% following code:
%
% \begin{Verbatim}[gobble=2]
%   \TestAppExists{appname}
%   \ifthenelse{\boolean{AppExists}}
%     {app exists}{app doesn't exist}
% \end{Verbatim}
%
%    \begin{macrocode}
\newboolean{AppExists}
\providecommand\TestAppExists[1]{
  \ifwindows
%    \end{macrocode}
%
% On Windows, we need to use path expansion and write the result to a file.
% If the application doesn't exist, the file will be empty (except for a newline);
% otherwise, it will contain the full path of the application.
%
%    \begin{macrocode}
    \DeleteFile{\jobname.aex}
    \immediate\write18{for \string^\@percentchar i in (#1.exe #1.bat #1.cmd)
      do set >\jobname.aex <nul: /p x=\string^\@percentchar \string~$PATH:i>>\jobname.aex} %$
    \newread\@appexistsfile
    \immediate\openin\@appexistsfile\jobname.aex
    \expandafter\def\expandafter\@tmp@cr\expandafter{\the\endlinechar}
    \endlinechar=-1\relax
    \readline\@appexistsfile to \@apppathifexists
    \endlinechar=\@tmp@cr
    \ifthenelse{\equal{\@apppathifexists}{}}
     {\AppExistsfalse}
     {\AppExiststrue}
    \immediate\closein\@appexistsfile
    \DeleteFile{\jobname.aex}
\immediate\typeout{file deleted}
  \else
%    \end{macrocode}
%
% On Unix-like systems, we do a straightforward |which| test and create a file upon success,
% whose existence we can then check.
%
%    \begin{macrocode}
    \immediate\write18{which -s #1 && touch \jobname.aex}
    \IfFileExists{\jobname.aex}
     {\AppExiststrue
      \DeleteFile{\jobname.aex}}
     {\AppExistsfalse}
  \fi}
%    \end{macrocode}
% \end{macro}
%
%
% \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 with an optional default argument.
% If a |key| option is specified without |=value|, the default is assumed.
%
%    \begin{macrocode}
\newcommand\minted@define@opt[3][]{
  \minted@defopt{#2}
  \ifthenelse{\equal{#1}{}}{
    \define@key{minted@opt}{#2}{\@namedef{minted@opt@#2}{#3}}}
   {\define@key{minted@opt}{#2}[#1]{\@namedef{minted@opt@#2}{#3}}}}
%    \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 \pkg{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}
%
% Extra switch options are also passed on to \pkg{fancyvrb}.
%
%    \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.
%
% \changes{0.1.5}{2010/01/10}{Fixed \texttt{firstnumber} option}
% \changes{0.1.5}{2010/01/10}{Added \texttt{fillcolor} option}
% \changes{0.1.5}{2010/01/10}{Removed \texttt{caption} option}
% \changes{1.6}{2010/01/27}{Added font-related options}
%
%    \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@opt{bgcolor}{#1}
\minted@define@extra{frame}
\minted@define@extra{framesep}
\minted@define@extra{framerule}
\minted@define@extra{rulecolor}
\minted@define@extra{numbersep}
\minted@define@extra{firstnumber}
\minted@define@extra{stepnumber}
\minted@define@extra{firstline}
\minted@define@extra{lastline}
\minted@define@extra{baselinestretch}
\minted@define@extra{xleftmargin}
\minted@define@extra{xrightmargin}
\minted@define@extra{fillcolor}
\minted@define@extra{tabsize}
\minted@define@extra{fontfamily}
\minted@define@extra{fontsize}
\minted@define@extra{fontshape}
\minted@define@extra{fontseries}
\minted@define@extra{formatcom}
\minted@define@extra@switch{numberblanklines}
\minted@define@extra@switch{showspaces}
\minted@define@extra@switch{resetmargins}
\minted@define@extra@switch{samepage}
\minted@define@extra@switch{showtabs}
\minted@define@extra@switch{obeytabs}
%    \end{macrocode}
%
%
% \subsection{Internal helpers}
%
% \begin{environment}{\minted@bgbox}
%
% Here, we define an environment that may be wrapped around a minted code to assign a background color.
%
% First, we need to define a new save box.
%
%    \begin{macrocode}
\newsavebox{\minted@bgbox}
%    \end{macrocode}
%
% Now we can define de environment that captures a code fragment inside a minipage and applies a background
% color.
%
%    \begin{macrocode}
\newenvironment{minted@colorbg}[1]{
%\setlength{\fboxsep}{-\fboxrule}
  \def\minted@bgcol{#1}
  \noindent
  \begin{lrbox}{\minted@bgbox}
  \begin{minipage}{\linewidth-2\fboxsep}}
 {\end{minipage}
  \end{lrbox}%
  \colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}
%    \end{macrocode}
% \end{environment}
%
% \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\write18{\minted@cmd}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\begin{minted@colorbg}{\minted@opt@bgcolor}}
  \input{\jobname.out.pyg}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\end{minted@colorbg}}
  \DeleteFile{\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.
%
%    \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}
    \DeleteFile{\jobname.pyg}}]{minted@verb}#3}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{minted}
%
% Highlight a longer piece of code inside a verbatim environment.
%
%    \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{}}
  \DeleteFile{\jobname.pyg}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\inputminted}
%
% Highlight an external source file.
%
%    \begin{macrocode}
\newcommand\inputminted[3][]{
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \minted@pygmentize[#3]{#2}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Command shortcuts}
%
% \changes{1.6}{2010/01/22}{Added command shortcuts}
%
% We allow the user to define shortcuts for the highlighting commands.
%
% \begin{macro}{\newminted}
%
% Define a new language-specific alias for the |minted| environment.
%
%    \begin{macrocode}
\newcommand\newminted[3][]{
%    \end{macrocode}
%
% First, we look whether a custom environment name was given as the first
% optional argument.
% If that's not the case, construct it from the language name (append ``|code|'').
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@envname{#2code}}
   {\def\minted@envname{#1}}
%    \end{macrocode}
%
% Now, we define two environments.
% The first takes no further arguments.
% The second, starred version, takes an extra argument that specifies option
% overrides.
%
%    \begin{macrocode}
  \newenvironment{\minted@envname}
   {\VerbatimEnvironment\begin{minted}[#3]{#2}}
   {\end{minted}}
  \newenvironment{\minted@envname *}[1]
   {\VerbatimEnvironment\begin{minted}[#3,##1]{#2}}
   {\end{minted}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newmint}
%
% Define a new language-specific alias for the |\mint| short form.
%
%    \begin{macrocode}
\newcommand\newmint[3][]{
%    \end{macrocode}
%
% Same as with |\newminted|, look whether an explicit name is provided.
% If not, take the language name as command name.
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@shortname{#2}}
   {\def\minted@shortname{#1}}
%    \end{macrocode}
%
% And define the macro.
%
%    \begin{macrocode}
  \expandafter\newcommand\csname\minted@shortname\endcsname[2][]{
    \mint[#3,##1]{#2}##2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newmintedfile}
%
% Finally, define a new language-specific alias for |\inputminted|.
%
%    \begin{macrocode}
\newcommand\newmintedfile[3][]{
%    \end{macrocode}
%
% Here, the default macro name (if none is provided) appends ``|file|'' to the language name.
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@shortname{#2file}}
   {\def\minted@shortname{#1}}
%    \end{macrocode}
%
% \dots and define the macro.
%
%    \begin{macrocode}
  \expandafter\newcommand\csname\minted@shortname\endcsname[2][]{
    \inputminted[#3,##1]{#2}{##2}}}
%    \end{macrocode}
%
% \end{macro}
%
%
% \subsection{Float support}
%
% \changes{0.1.5}{2010/01/10}{Added float support}
%
% \begin{environment}{listing}
%
% Defines a new floating environment to use for floated listings.
%
%    \begin{macrocode}
\newfloat{listing}{h}{lol}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\listingcaption}
%
% The name that is displayed before each individual listings caption and its number.
% The macro |\listingscaption| can be redefined by the user.
%
%    \begin{macrocode}
\newcommand\listingscaption{Listing}
%    \end{macrocode}
%
% The following definition should not be changed by the user.
%
%    \begin{macrocode}
\floatname{listing}{\listingscaption}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\listoflistingscaption}
%
% The caption that is displayed for the list of listings.
%
%    \begin{macrocode}
\newcommand\listoflistingscaption{List of listings}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\listoflistings}
%
% Used to produce a list of listings (like |\listoffigures| etc.).
% This may well clash with other packages (e.g. \pkg{listings}) but we choose to ignore this
% since these two packages shouldn't be used together in the first place.
%
%    \begin{macrocode}
\providecommand\listoflistings{\listof{listing}{\listoflistingscaption}}
%    \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}
%
% 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}
%
% Check whether \app{pygmentize} is installed.
%
%    \begin{macrocode}
  \TestAppExists{pygmentize}
  \ifAppExists\else
    \PackageError{minted}
     {You must have `pygmentize' installed
      to use this package}
     {Refer to the installation instructions in the minted
      documentation for more information.}
  \fi
%    \end{macrocode}
%
% \Finale