Debian Codegraph @

texmf-dist/doc/latex/pygmentex/manual/pygmentex-manual.tex

raw
1 \documentclass[10pt,a4paper]{article}
2 %\usepackage[margin=13mm]{geometry}
3 \usepackage[utf8]{inputenc}
4 \usepackage[T1]{fontenc}
5 \usepackage[font=normalsize,format=plain,labelfont=bf,up,textfont=it,up]{caption}
6 \usepackage{xcolor}
7 \usepackage{hyperref}
8 \usepackage[framemethod=tikz]{mdframed}
9 \usepackage{tcolorbox}
10 \usepackage{fancyvrb-ex}
11 \usepackage{tikz}
12 \usepackage{calc}
13 \usepackage{pygmentex}
14 \usepackage{lipsum}
15
16 \fvset{gobble=0,showtabs,tabsize=1,frame=lines,framerule=1pt,numbers=left,fontsize=\scriptsize}
17
18 \tcbuselibrary{skins,breakable}
19
20 \definecolor{shadecolor}{rgb}{0.9,0.9,0.9}
21 \definecolor{lightgreen}{rgb}{0.8,0.95,0.8}
22
23 \begin{document}
24
25 \title{Testing the Pygmen\TeX{} package}
26 \author{José Romildo Malaquias\\
27 \href{mailto:[email protected]}{[email protected]}\\
28 \url{https://github.com/romildo/pygmentex}
29 }
30 \date{\today}
31 \maketitle
32
33 \section{The Pygmen\TeX{} package}
34
35 This document demonstrates how to use the Pygment\TeX{} package to
36 typeset code listings with \LaTeX{} and
37 Pygments\footnote{\url{http://pygments.org/}}.
38
39 Pygments is a generic syntax highlighter for general use in all kinds of
40 software such as forum systems, wikis or other applications that need to
41 prettify source code.
42
43 Pygmen\TeX{} provides an environment and two commands for typesetting code
44 listings in a \LaTeX{} document:
45 \begin{itemize}
46 \item the \verb|pygmented| environment typesets its contents as a
47 source code listing,
48 \item the \verb|\inputpygmented| command typesets the contents of a
49 file, including the result in the \LaTeX{} document, and
50 \item the \verb|\pyginline| command typesets its contents, keeping the
51 result in the same line.
52 \end{itemize}
53 They accept many options that allow the user to configure the listing in
54 many ways.
55
56 Read the remaining of this document to have an idea of what the package
57 is capable of.
58
59 \section{Requirements}
60
61 Current versions of Pygmen\TeX require Python version 3. Earlier versions
62 required Python version 2. All versions require the Python Pygments library.
63
64 \section{How to use the package}
65
66 \paragraph{1.} Add the package to the document preamble.
67 \begin{verbatim}
68 \usepackage{pygmentex}
69 \end{verbatim}
70
71 \paragraph{2.} Use the environment or commands mentioned previously to include source
72 code listings on your document.
73
74 \paragraph{3.} Compile using \texttt{pdflatex}.\footnote{Other \LaTeX compilers
75 may also work but have not been tested by the author.}
76 All the source code listings in the document wil be collected and saved in a
77 temporary file with the extension \texttt{.snippets} in its name.
78
79 \paragraph{4.} Run \texttt{pygmentex documentname.snippets}.
80 The python application \texttt{pygmentex} is
81 distributed with the Pygmen\TeX{} package).
82 It will produce another temporary file with the extension
83 \texttt{.pygmented}, containing \LaTeX{} code for the code listings
84 previously collected. The next time the document is compiled, they are
85 included to produce the final typeset document.
86
87 \paragraph{5.} Rerun \texttt{pdflatex} as usual.
88
89 \paragraph{Note.} Running the external python application
90 \texttt{pygmentex} can be done automatically from withing \LaTeX{} if
91 the optional argument \texttt{--shell-escape} is used.
92
93 With
94 \begin{verbatim}
95 pdflatex --shell-escape <file>
96 \end{verbatim}
97 the external run of \texttt{pygmentex} is not needed. It will be run
98 automatically at the end of the document compilation.
99
100 There is a package option \texttt{force} that will force running
101 \texttt{pygmentex} every time the document is compiled.
102
103 \section{First examples}
104
105 A simple verbatim text is the first example.
106
107 \begin{Example}
108 \begin{pygmented}[]
109 Hello world!
110 This is a simple demonstration text.
111 \end{pygmented}
112 \end{Example}
113
114 The followig C program reads two integers and calculates their sum.
115
116 \begin{Example}
117 \begin{pygmented}[lang=c]
118 #include <stdio.h>
119 int main(void)
120 {
121 int a, b, c;
122 printf("Enter two numbers to add: ");
123 scanf("%d%d", &a, &b);
124 c = a + b;
125 printf("Sum of entered numbers = %d\n", c);
126 return 0;
127 }
128 \end{pygmented}
129 \end{Example}
130
131 \begin{Example}
132 In this program, \pyginline[lang=c]|int| is a type and
133 \pyginline[lang=c]|"Enter two numbers to add: "| is a literal string.
134 \end{Example}
135
136 Next you can see a Java program to calculate the factorial of a number.
137
138 \begin{Example}
139 \inputpygmented[lang=java]{Factorial.java}
140 \end{Example}
141
142 \section{Options}
143 \subsection{\texttt{lang}}
144 The programming language of the listing code can be specified using the
145 \verb|lang| option.
146
147 To get a list of all available languages, execute the following command
148 on the command line:
149 \begin{verbatim}
150 $ pygmentize -L lexers
151 \end{verbatim}
152
153 \subsection{\texttt{sty}}
154
155 Instead of using the default style you may choose another stylesheet
156 provided by Pygments by its name using the \verb|sty| option.
157
158 To get a list of all available stylesheets, execute the following
159 command on the command line:
160 \begin{verbatim}
161 $ pygmentize -L styles
162 \end{verbatim}
163
164 Creating your own styles is also very easy. Just follow the instructions
165 provided on the website.
166
167 As examples you can see a C program typeset with different styles.
168
169 \begin{Example}
170 \noindent
171 \begin{minipage}[t]{0.49\linewidth}
172 \begin{pygmented}[lang=c,gobble=4,sty=murphy]
173 #include<stdio.h>
174 main()
175 { int n;
176 printf("Enter a number: ");
177 scanf("%d",&n);
178 if ( n%2 == 0 )
179 printf("Even\n");
180 else
181 printf("Odd\n");
182 return 0;
183 }
184 \end{pygmented}
185 \end{minipage}
186 \hfil
187 \begin{minipage}[t]{0.49\linewidth}
188 \begin{pygmented}[lang=c,gobble=4,sty=trac]
189 #include<stdio.h>
190 main()
191 { int n;
192 printf("Enter a number: ");
193 scanf("%d",&n);
194 if ( n%2 == 0 )
195 printf("Even\n");
196 else
197 printf("Odd\n");
198 return 0;
199 }
200 \end{pygmented}
201 \end{minipage}
202 \end{Example}
203
204 \subsection{\texttt{font}}
205
206 The value of the option \verb|font| is typeset before the content of the
207 listing. Usualy it is used to specify a font to be used. See the
208 following example.
209
210 \begin{Example}
211 \begin{pygmented}[lang=scala,font=\rmfamily\scshape\large]
212 object bigint extends Application {
213 def factorial(n: BigInt): BigInt =
214 if (n == 0) 1 else n * factorial(n-1)
215
216 val f50 = factorial(50); val f49 = factorial(49)
217 println("50! = " + f50)
218 println("49! = " + f49)
219 println("50!/49! = " + (f50 / f49))
220 }
221 \end{pygmented}
222 \end{Example}
223
224 \subsection{\texttt{colback}}
225
226 The option \verb|colback| can be used to choose a background color, as
227 is shown in the folowing example.
228
229 \begin{Example}
230 \begin{pygmented}[lang=fsharp,colback=green!25]
231 let rec factorial n =
232 if n = 0
233 then 1
234 else n * factorial (n - 1)
235 System.Console.WriteLine(factorial anInt)
236 \end{pygmented}
237 \end{Example}
238
239
240 \subsection{\texttt{gobble}}
241
242 The option \verb|gobble| specifies the number of characters to suppress
243 at the beginning of each line (up to a maximum of 9). This is mainly
244 useful when environments are indented (Default: empty — no character
245 suppressed).
246
247 \begin{Example}
248 A code snippet inside a minipage:
249 \begin{minipage}[t]{.5\linewidth}
250 \begin{pygmented}[lang=d,gobble=8]
251 ulong fact(ulong n)
252 {
253 if(n < 2)
254 return 1;
255 else
256 return n * fact(n - 1);
257 }
258 \end{pygmented}
259 \end{minipage}
260 \end{Example}
261
262
263 \subsection{\texttt{autogobble}}
264
265 The boolean option \verb|autogobble| automatically removes common leading
266 whitespace from the code snippet. This is highly recommended when your
267 \verb|pygmented| environments are indented to match the surrounding \LaTeX{}
268 source code, as it saves you from having to manually count and specify the
269 exact number of characters to suppress using the standard \verb|gobble|
270 option.
271
272 \begin{Example}
273 A code snippet deeply indented in the \LaTeX{} source:
274 \begin{itemize}
275 \item Item one
276 \item Item two
277 \begin{pygmented}[lang=python,autogobble,colback=yellow!20]
278 def greet(name):
279 # The indentation relative to the 'def' keyword is preserved!
280 print(f"Hello, {name}!")
281 return True
282 \end{pygmented}
283 \end{itemize}
284 \end{Example}
285
286
287 \subsection{\texttt{tabsize}}
288
289 The option \verb|tabsize| specifies the number of of spaces given by a
290 tab character (Default: 8).
291
292 \begin{Verbatim}[showtabs,tabsize=1]
293 \begin{pygmented}[lang=common-lisp,tabsize=4]
294 ;; Triple the value of a number
295 (defun triple (X)
296 "Compute three times X."
297 (* 3 X))
298 \end{pygmented}
299 \end{Verbatim}
300
301 \begin{pygmented}[lang=common-lisp,tabsize=4]
302 ;; Triple the value of a number
303 (defun triple (X)
304 "Compute three times X."
305 (* 3 X))
306 \end{pygmented}
307
308
309 \subsection{\texttt{linenos}, \texttt{linenostart}, \texttt{linenostep}, \texttt{linenosep}}
310
311 The lines of a listing can be numbered. The followig options control
312 numbering of lines.
313 \begin{itemize}
314 \item Line numbering is enabled or disable with the \verb|linenos|
315 boolean option.
316 \item The number used for the first line can be set with the option
317 \verb|linenostart|.
318 \item The step between numbered lines can be set with the option
319 \verb|linenostep|.
320 \item The space between the line number and the line of the listing
321 can be set with the option \verb|linenosep|.
322 \end{itemize}
323
324 In the followig listing you can see a Scheme function to calculate the
325 factorial of a number.
326
327 \begin{Example}
328 \begin{pygmented}[lang=scheme,linenos,linenostart=1001,linenostep=2,linenosep=5mm]
329 ;; Building a list of squares from 0 to 9.
330 ;; Note: loop is simply an arbitrary symbol used as
331 ;; a label. Any symbol will do.
332
333 (define (list-of-squares n)
334 (let loop ((i n) (res '()))
335 (if (< i 0)
336 res
337 (loop (- i 1) (cons (* i i) res)))))
338 \end{pygmented}
339 \end{Example}
340
341 \subsection{\texttt{caption} and \texttt{label}}
342
343 The option \verb|caption| can be used to set a caption for the listing.
344 The option \verb|label| allows the assignment of a label to the listing.
345
346 Here is an example:
347
348 \begin{Example}
349 \begin{pygmented}[lang=c++,label=lst:test,caption=A \textbf{C++} example]
350 // This program adds two numbers and prints their sum.
351 #include <iostream>
352 int main()
353 {
354 int a;
355 int b;
356 int sum;
357 sum = a + b;
358 std::cout << "The sum of " << a << " and " << b
359 << " is " << sum << "\n";
360 return 0;
361 }
362 \end{pygmented}
363 \end{Example}
364
365 \begin{Example}
366 Listing \ref{lst:test} is a C++ program.
367 \end{Example}
368
369 \subsection{\texttt{texcomments}, \texttt{mathescape} and \texttt{escapeinside}}
370
371 The option \verb|texcomments|, if set to \texttt{true}, enables \LaTeX{}
372 comment lines. That is, LaTex markup in comment tokens is not escaped
373 so that \LaTeX{} can render it.
374
375 The \verb|mathescape|, if set to \texttt{true}, enables \LaTeX{} math
376 mode escape in comments. That is, \verb|$...$| inside a comment will
377 trigger math mode.
378
379 The option \verb|escapeinside|, if set to a string of length two,
380 enables escaping to \LaTeX{}. Text delimited by these two characters
381 is read as \LaTeX{} code and typeset accordingly. It has no effect in
382 string literals. It has no effect in comments if \verb|texcomments| or
383 \verb|mathescape| is set.
384
385 Some examples follows.
386
387 \begin{Example}
388 \begin{pygmented}[lang=c++,texcomments]
389 #include <iostream>
390 using namespace std;
391 main()
392 {
393 cout << "Hello World"; // prints \underline{Hello World}
394 return 0;
395 }
396 \end{pygmented}
397 \end{Example}
398
399 \begin{Example}
400 \begin{pygmented}[lang=python,mathescape]
401 # Returns $\sum_{i=1}^{n}i$
402 def sum_from_one_to(n):
403 r = range(1, n + 1)
404 return sum(r)
405 \end{pygmented}
406 \end{Example}
407
408 \begin{Example}
409 \begin{pygmented}[lang=c,escapeinside=||]
410
411 if (|\textit{condition}|)
412 |\textit{command$_1$}|
413 else
414 |\textit{command$_2$}|
415 \end{pygmented}
416 \end{Example}
417
418
419 \subsection{\texttt{inline method} and \texttt{boxing method}}
420
421 After being prettified by Pygments, the listings are enclosed in a
422 command (for \verb|\pyginline|) or in an environment (for
423 \verb|pygmented| and \verb|inputpygmented|). By default
424 \verb|\pyginline| uses the command \verb|\efbox| from the \texttt{efbox}
425 package, and \verb|pygmented| and \verb|inputpygmented| use the
426 environment \verb|mdframed| from the \texttt{mdframed} package.
427
428 The enclosing command or environment should be configurable using a list
429 of key-value pairs written between square brackets.
430
431 The enclosing command for
432 \verb|\pyginline| can be changed with the option
433 \verb|inline method|. For instance, in the following the command
434 \verb|\tcbox| from the \verb|tcolorbox| package is used:
435
436 \begin{Example}
437 In the previous Java program,
438 \pyginline[lang=java,inline method=tcbox]|"Factorial of "| is a
439 literal string.
440 \end{Example}
441
442 The enclosing environment for \verb|pygmented| and
443 \verb|inputpygmented| can be changed with the option
444 \verb|boxing method|. For instance, here is a hello world program in
445 C\#, enclosed in a \verb|tcolorbox| environment:
446
447 \begin{Example}
448 \begin{pygmented}[lang=csharp,boxing method=tcolorbox]
449 using System;
450 class Program
451 {
452 public static void Main(string[] args)
453 {
454 Console.WriteLine("Hello, world!");
455 }
456 }
457 \end{pygmented}
458 \end{Example}
459
460 Any option unknown to Pygmen\TeX{} are passed to the enclosing command
461 or environment.
462
463 For instance:
464
465 \begin{Example}
466 \begin{pygmented}[lang=xml,boxing method=tcolorbox,colframe=red,boxrule=2mm]
467 <!-- This is a note -->
468 <note>
469 <to>Tove</to>
470 <from>Jani</from>
471 <heading>Reminder</heading>
472 <body>Don't forget me this weekend!</body>
473 </note>
474 \end{pygmented}
475 \end{Example}
476
477 \section{Setting global options for Pygmen\TeX{}}
478
479 Global options can be setting using the \verb|setpygmented| command.
480 See the examples that follows.
481
482 \begin{Example}
483 \setpygmented{lang=haskell, colback=red!30, font=\ttfamily\small}
484
485 \begin{pygmented}[]
486 sum :: Num a => [a] -> a
487 sum [] = 0
488 sum (x:xs) = x + sum xs
489 \end{pygmented}
490 \end{Example}
491
492 \begin{Example}
493 \begin{pygmented}[colback=blue!20, boxing method=tcolorbox]
494 elem :: Eq a => a -> [a] -> Bool
495 elem _ [] = False
496 elem x (y:ys) = x == y || elem x ys
497 \end{pygmented}
498 \end{Example}
499
500 \begin{Example}
501 \setpygmented{lang=snobol}
502
503 \begin{pygmented}[]
504 OUTPUT = "What is your name?"
505 Username = INPUT
506 OUTPUT = "Thank you, " Username
507 END
508 \end{pygmented}
509 \end{Example}
510
511 \begin{Example}
512 \setpygmented{test/.style={colback=yellow!33,boxing method=tcolorbox,colframe=blue}}
513
514 \begin{pygmented}[test, lang=vbnet]
515 Module Module1
516 Sub Main()
517 Console.WriteLine("Hello, world!")
518 End Sub
519 End Module
520 \end{pygmented}
521 \end{Example}
522
523 \begin{Example}
524 \begin{pygmented}[lang=tcl]
525 puts "Hello, world!"
526 \end{pygmented}
527 \end{Example}
528
529 \section{More examples of inline code snippets}
530
531 \begin{Example}
532 An inline source code snippet:
533 \pyginline[lang=c]|const double alfa = 3.14159;|.
534 This is a C declaration with initialization.
535 \end{Example}
536
537 \begin{Example}
538 \pyginline[lang=prolog,colback=yellow]=avo(A,B) :- pai(A,X), pai(X,B).=
539 is a Prolog clause. Its head is
540 \pyginline[lang=prolog,sty=emacs,colback=yellow,linecolor=red]=avo(A,B)=
541 and its body is
542 \pyginline[lang=prolog,sty=vim,colback=black,hidealllines]=pai(A,X), pai(X,B)=.
543 \end{Example}
544
545 \begin{Example}
546 See the identifier \pyginline[inline method=efbox,colback=green!25]|variable|,
547 which names something. String literals in C looks like
548 \pyginline[lang=c,inline method=tcbox,colback=blue!20,boxrule=2pt]|"hello, world!\n"|.
549 \end{Example}
550
551 \setpygmented{colback=shadecolor}
552
553 \begin{Example}
554 This one
555 \pyginline[lang=ocaml,font=\ttfamily\scriptsize,topline=false]:let x = [1;2;3] in length x:
556 is an OCaml expression with local bindings. With OCaml one can do
557 imperative, functional and object oriented programming.
558 \end{Example}
559
560 \begin{Example}
561 Now some Java code:
562 \pyginline[lang=java,sty=colorful,font=\ttfamily\itshape,linewidth=1pt]|public int f(double x)|.
563 This is a method header.
564 \end{Example}
565
566 \section{More examples of displayed code snippets}
567
568 \setpygmented{lang=scheme,colback=shadecolor,sty=emacs}
569
570 In listing \ref{lst:fact} you can see a function definition in the
571 Scheme language. This function computes the factorial of a natural
572 number.
573 \newline\rule{\linewidth}{2pt}
574 \begin{pygmented}[
575 sty=emacs,
576 linenos,
577 label=lst:fact,
578 caption=A Scheme function.
579 ]
580 (define fact
581 (lambda (n)
582 (if (= n 0)
583 1
584 (* n (fact (- n 1))))))
585 \end{pygmented}
586
587 Here you have some more code to further testing the package. Listing
588 \ref{lst:haskell} is a Haskell program. When run this program interacts
589 with the user asking the user name, reading a line input by the user,
590 and showing a greeting message to the user.
591
592 \inputpygmented[%
593 lang=haskell,
594 linenos,
595 linenostart=79831,
596 innerlinecolor=yellow, innerlinewidth=6pt,
597 middlelinecolor=blue, middlelinewidth=10pt,
598 outerlinecolor=green, outerlinewidth=12pt,
599 roundcorner=4,
600 colback=shadecolor,
601 caption=A haskell interactive program,
602 label=lst:haskell,
603 ]{pygmentex-manual.hs}
604
605 This is a rule:
606
607 \noindent\rule{\linewidth}{2pt}
608
609 Now a Pascal procedure:
610
611 \inputpygmented[
612 lang=delphi,
613 linewidth=1.5pt,
614 font=\ttfamily\sffamily\large,
615 colback=yellow
616 ]{pygmentex-manual.delphi}
617 and a Pascal program
618 \inputpygmented[lang=pascal,linenos,linenostart=5801]{pygmentex-manual.pas}
619
620 A Python code snippet:
621
622 \inputpygmented[
623 lang=python,
624 sty=emacs,
625 linenos,
626 linenostep=3,
627 linewidth=1pt,
628 colback=lightgreen
629 ]{pygmentex-manual.py}
630
631 \section{Using code snippets in environments}
632
633 The following is a \textbf{description} environment.
634
635 \begin{description}
636 \item[An item] \lipsum[31]
637 \begin{pygmented}[lang=scala,colback=yellow,
638 % title=Item A
639 ]
640 def qsort(xs: List[Int]): List[Int] =
641 xs match {
642 case Nil =>
643 Nil
644 case pivot :: tail =>
645 qsort(tail filter { _ < pivot }) :::
646 pivot :: qsort(tail filter { _ >= pivot })
647 }
648 \end{pygmented}
649 \lipsum[32]
650
651 \item[Another item] \lipsum[33]
652 \begin{pygmented}[lang=lua,colback=yellow]
653 function entry0 (o)
654 N=N + 1
655 local title = o.title or '(no title)'
656 fwrite('<LI><A HREF="#%d">%s</A>\n', N, title)
657 end
658 \end{pygmented}
659 \lipsum[34]
660 \end{description}
661
662 \section{A long program}
663
664 Here you can read the source code for a hand written lexical analyser
665 for the \emph{straight-line} programming language that I have developed
666 in Java.
667
668 \inputpygmented[boxing method=mdframed,lang=java,sty=autumn,colback=red!8,font=\ttfamily\small,tabsize=2,frametitle=\emph{Ad hoc} lexical analyser]{pygmentex-manual.java}
669
670 \section{Some fancy examples using \texttt{tcolorbox}}
671
672 The followig example uses \texttt{tcolorbox} to typeset the code
673 listing.
674
675 \newcounter{example}
676 \newlength{\examlen}
677 \colorlet{colexam}{red!75!black}
678
679 \begin{pygmented}[boxing method=tcolorbox,lang=scala,
680 title=Example \arabic{example}: hello from \texttt{Scala},
681 code={\refstepcounter{example}%
682 \settowidth{\examlen}{\Large\bfseries Example \arabic{example}}},%
683 coltitle=colexam,fonttitle=\Large\bfseries,
684 enhanced,breakable,
685 before=\par\medskip,
686 parbox=false,
687 frame hidden,interior hidden,segmentation hidden,
688 boxsep=0pt,left=0pt,right=3mm,toptitle=2mm,pad at break=0mm,
689 overlay unbroken={\draw[colexam,line width=1pt] (frame.north west)
690 --([xshift=-0.5pt]frame.north east)--([xshift=-0.5pt]frame.south east)
691 --(frame.south west);
692 \draw[colexam,line width=2pt] ([yshift=0.5pt]frame.north west)
693 -- +(\examlen,0pt);},
694 overlay first={\draw[colexam,line width=1pt] (frame.north west)
695 --([xshift=-0.5pt]frame.north east)--([xshift=-0.5pt]frame.south east);
696 \draw[red!75!black,line width=2pt] ([yshift=0.5pt]frame.north west)
697 -- +(\examlen,0pt);},
698 overlay middle={\draw[colexam,line width=1pt] ([xshift=-0.5pt]frame.north east)
699 --([xshift=-0.5pt]frame.south east); },
700 overlay last={\draw[colexam,line width=1pt] ([xshift=-0.5pt]frame.north east)
701 --([xshift=-0.5pt]frame.south east)--(frame.south west);}%
702 ]
703 object HelloWorld extends App {
704 println("Hello, world!")
705 }
706 \end{pygmented}
707
708 \begin{pygmented}[boxing method=tcolorbox,lang=java,
709 enhanced,colback=blue!10!white,colframe=orange,top=4mm,
710 enlarge top by=\baselineskip/2+1mm,
711 enlarge top at break by=0mm,pad at break=2mm,
712 fontupper=\normalsize,
713 overlay unbroken and first={%
714 \node[rectangle,rounded corners,draw=black,fill=blue!20!white,
715 inner sep=1mm,anchor=west,font=\small]
716 at ([xshift=4.5mm]frame.north west) {\strut\textbf{My fancy title}};},
717 ]
718 public class Hello {
719 public static void main(String[] args) {
720 System.out.println("Hello, world!")
721 }
722 }
723 \end{pygmented}
724
725 \begin{pygmented}[boxing method=tcolorbox,lang=haskell,
726 enhanced,sharp corners=uphill,
727 colback=blue!25!white,colframe=blue!25!black,coltext=blue!90!black,
728 fontupper=\Large\bfseries,arc=6mm,boxrule=2mm,boxsep=5mm,
729 borderline={0.3mm}{0.3mm}{white}
730 ]
731 module Main (main) where
732
733 main :: IO ()
734 main = putStrLn "Hello, world!"
735 \end{pygmented}
736
737 \begin{pygmented}[boxing method=tcolorbox,lang=c++,
738 enhanced,frame style image=blueshade.png,
739 opacityback=0.75,opacitybacktitle=0.25,
740 colback=blue!5!white,colframe=blue!75!black,
741 title=My title
742 ]
743 #include <iostream>
744 using namespace std;
745 int main(int argc, char** argv) {
746 cout << "Hello, world!" << endl;
747 return 0;
748 }
749 \end{pygmented}
750
751 \begin{pygmented}[boxing method=tcolorbox,lang=d,
752 enhanced,attach boxed title to top center={yshift=-3mm,yshifttext=-1mm},
753 colback=blue!5!white,colframe=blue!75!black,colbacktitle=red!80!black,
754 title=My title,fonttitle=\bfseries,
755 boxed title style={size=small,colframe=red!50!black}
756 ]
757 /* This program prints a
758 hello world message
759 to the console. */
760
761 import std.stdio;
762
763 void main()
764 {
765 writeln("Hello, World!");
766 }
767 \end{pygmented}
768
769
770 \section{Some fancy examples using \texttt{mdframed}}
771
772 The followig example uses \texttt{mdframed} to typeset the code listing.
773
774 \global\mdfdefinestyle{exampledefault}{%
775 linecolor=red,linewidth=3pt,%
776 leftmargin=1cm,rightmargin=1cm
777 }
778
779 \begin{pygmented}[boxing method=mdframed,lang=ada,style=exampledefault]
780 with Ada.Text_IO;
781
782 procedure Hello_World is
783 use Ada.Text_IO;
784 begin
785 Put_Line("Hello, world!");
786 end;
787 \end{pygmented}
788
789 \global\mdfapptodefinestyle{exampledefault}{%
790 topline=false,bottomline=false,
791 }
792
793 \begin{pygmented}[boxing method=mdframed,lang=pascal,style=exampledefault,frametitle={Saying \emph{hello} from Pascal}]
794 program HelloWorld;
795
796 begin
797 WriteLn('Hello, world!');
798 end.
799 \end{pygmented}
800
801 \global\mdfdefinestyle{separateheader}{%
802 frametitle={%
803 \tikz[baseline=(current bounding box.east),outer sep=0pt]
804 \node[anchor=east,rectangle,fill=blue!20]
805 {\strut Saying \emph{hello} in Modula-2};},
806 innertopmargin=10pt,linecolor=blue!20,%
807 linewidth=2pt,topline=true,
808 frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
809 frametitlerule=false,
810 backgroundcolor=white,
811 }
812
813 \begin{pygmented}[boxing method=mdframed,lang=modula2,style=separateheader]
814 MODULE Hello;
815 FROM STextIO IMPORT WriteString;
816 BEGIN
817 WriteString("Hello World!");
818 END Hello.
819 \end{pygmented}
820
821
822 \tikzset{titregris/.style =
823 {draw=gray, thick, fill=white, shading = exersicetitle, %
824 text=gray, rectangle, rounded corners, right,minimum height=.7cm}}
825 \pgfdeclarehorizontalshading{exersicebackground}{100bp}
826 {color(0bp)=(green!40); color(100bp)=(black!5)}
827 \pgfdeclarehorizontalshading{exersicetitle}{100bp}
828 {color(0bp)=(red!40);color(100bp)=(black!5)}
829 \newcounter{exercise}
830 \renewcommand*\theexercise{Exercise~n\arabic{exercise}}
831 \makeatletter
832 \def\mdf@@exercisepoints{}%new mdframed key:
833 \define@key{mdf}{exercisepoints}{%
834 \def\mdf@@exercisepoints{#1}
835 }
836 \mdfdefinestyle{exercisestyle}{%
837 outerlinewidth=1em,outerlinecolor=white,%
838 leftmargin=-1em,rightmargin=-1em,%
839 middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
840 apptotikzsetting={\tikzset{mdfbackground/.append style ={%
841 shading = exersicebackground}}},
842 innertopmargin=1.2\baselineskip,
843 skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
844 skipbelow={-1em},
845 needspace=3\baselineskip,
846 frametitlefont=\sffamily\bfseries,
847 settings={\global\stepcounter{exercise}},
848 singleextra={%
849 \node[titregris,xshift=1cm] at (P-|O) %
850 {~\mdf@frametitlefont{\theexercise}\hbox{~}};
851 \ifdefempty{\mdf@@exercisepoints}%
852 {}%
853 {\node[titregris,left,xshift=-1cm] at (P)%
854 {~\mdf@frametitlefont{\mdf@@exercisepoints points}\hbox{~}};}%
855 },
856 firstextra={%
857 \node[titregris,xshift=1cm] at (P-|O) %
858 {~\mdf@frametitlefont{\theexercise}\hbox{~}};
859 \ifdefempty{\mdf@@exercisepoints}%
860 {}%
861 {\node[titregris,left,xshift=-1cm] at (P)%
862 {~\mdf@frametitlefont{\mdf@@exercisepoints points}\hbox{~}};}%
863 },
864 }
865 \makeatother
866
867 \begin{pygmented}[boxing method=mdframed,lang=go,style=exercisestyle]
868 // hello world in 'go'
869 package main
870
871 import "fmt"
872
873 func main() {
874 fmt.Println("Hello, world!")
875 }
876 \end{pygmented}
877
878 \begin{pygmented}[boxing method=mdframed,lang=objective-c,style=exercisestyle,exercisepoints=10]
879 /* hello from objective-c */
880
881 #import <stdio.h>
882 #import <Foundation/Foundation.h>
883
884 int main(void)
885 {
886 NSLog(@"Hello, world!\n");
887 return 0;
888 }
889 \end{pygmented}
890
891 \mdfdefinestyle{another}{%
892 linecolor=red,middlelinewidth=2pt,%
893 frametitlerule=true,%
894 apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
895 shade,left color=white, right color=blue!20}}},
896 frametitlerulecolor=green!60,
897 frametitlerulewidth=1pt,
898 innertopmargin=\topskip,
899 }
900
901 \begin{pygmented}[boxing method=mdframed,lang=c,style=another,frametitle={Hello from C}]
902 #include <stdio.h>
903 int main(int argc, char **argv) {
904 printf("Hello, world!\n");
905 return 0;
906 }
907 \end{pygmented}
908
909 \section{Debugging}
910 Paths given to \verb|\inputpygmented| should be relative to the top level project
911 directory, not to the file that contains the command (if that file is in a
912 subdirectory). Pygmen\TeX generates only a single top-level \texttt{.snippets} file,
913 and paths are not munged to account for code in subdirectories.
914
915 \section{Conclusion}
916
917 That is all.
918
919 \end{document}