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
|
\documentclass[10pt,a4paper]{article}
%\usepackage[margin=13mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[font=normalsize,format=plain,labelfont=bf,up,textfont=it,up]{caption}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tcolorbox}
\usepackage{fancyvrb-ex}
\usepackage{tikz}
\usepackage{calc}
\usepackage{pygmentex}
\usepackage{lipsum}
\fvset{gobble=0,showtabs,tabsize=1,frame=lines,framerule=1pt,numbers=left,fontsize=\scriptsize}
\tcbuselibrary{skins,breakable}
\definecolor{shadecolor}{rgb}{0.9,0.9,0.9}
\definecolor{lightgreen}{rgb}{0.8,0.95,0.8}
\begin{document}
\title{Testing the Pygmen\TeX{} package}
\author{José Romildo Malaquias\\
\href{mailto:malaquias@ufop.edu.br}{malaquias@ufop.edu.br}\\
\href{https://bitbucket.org/romildo/pygmentex/src/master/}{bitbucket.org/romildo/pygmentex}
}
\maketitle
\section{The Pygmen\TeX{} package}
This document demonstrates how to use the Pygment\TeX{} package to
typeset code listings with \LaTeX{} and
Pygments\footnote{\url{http://pygments.org/}}.
Pygments is a generic syntax highlighter for general use in all kinds of
software such as forum systems, wikis or other applications that need to
prettify source code.
Pygmen\TeX{} provides an environment and two commands for typesetting code
listings in a \LaTeX{} document:
\begin{itemize}
\item the \verb|pygmented| environment typesets its contents as a
source code listing,
\item the \verb|\inputpygmented| command typesets the contents of a
file, including the result in the \LaTeX{} document, and
\item the \verb|\pyginline| command typesets its contents, keeping the
result in the same line.
\end{itemize}
They accept many options that allow the user to configure the listing in
many ways.
Read the remaining of this document to have an idea of what the package
is capable of.
\section{Requirements}
Current versions of Pygmen\TeX require Python version 3. Earlier versions
required Python version 2. All versions require the Python Pygments library.
\section{How to use the package}
\paragraph{1.} Add the package to the document preamble.
\begin{verbatim}
\usepackage{pygmentex}
\end{verbatim}
\paragraph{2.} Use the environment or commands mentioned previously to include source
code listings on your document.
\paragraph{3.} Compile using \texttt{pdflatex}.\footnote{Other \LaTeX compilers
may also work but have not been tested by the author.}
All the source code listings in the document wil be collected and saved in a
temporary file with the extension \texttt{.snippets} in its name.
\paragraph{4.} Run \texttt{pygmentex documentname.snippets}.
The python application \texttt{pygmentex} is
distributed with the Pygmen\TeX{} package).
It will produce another temporary file with the extension
\texttt{.pygmented}, containing \LaTeX{} code for the code listings
previously collected. The next time the document is compiled, they are
included to produce the final typeset document.
\paragraph{5.} Rerun \texttt{pdflatex} as usual.
\paragraph{Note.} Running the external python application
\texttt{pygmentex} can be done automatically from withing \LaTeX{} if
the optional argument \texttt{--shell-escape} is used.
With
\begin{verbatim}
pdflatex --shell-escape <file>
\end{verbatim}
the external run of \texttt{pygmentex} is not needed. It will be run
automatically at the end of the document compilation.
There is a package option \texttt{force} that will force running
\texttt{pygmentex} every time the document is compiled.
\section{First examples}
A simple verbatim text is the first example.
\begin{Example}
\begin{pygmented}[]
Hello world!
This is a simple demonstration text.
\end{pygmented}
\end{Example}
The followig C program reads two integers and calculates their sum.
\begin{Example}
\begin{pygmented}[lang=c]
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter two numbers to add: ");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of entered numbers = %d\n", c);
return 0;
}
\end{pygmented}
\end{Example}
\begin{Example}
In this program, \pyginline[lang=c]|int| is a type and
\pyginline[lang=c]|"Enter two numbers to add: "| is a literal string.
\end{Example}
Next you can see a Java program to calculate the factorial of a number.
\begin{Example}
\inputpygmented[lang=java]{Factorial.java}
\end{Example}
\section{Options}
\subsection{\texttt{lang}}
The programming language of the listing code can be specified using the
\verb|lang| option.
To get a list of all available languages, execute the following command
on the command line:
\begin{verbatim}
$ pygmentize -L lexers
\end{verbatim}
\subsection{\texttt{sty}}
Instead of using the default style you may choose another stylesheet
provided by Pygments by its name using the \verb|sty| option.
To get a list of all available stylesheets, execute the following
command on the command line:
\begin{verbatim}
$ pygmentize -L styles
\end{verbatim}
Creating your own styles is also very easy. Just follow the instructions
provided on the website.
As examples you can see a C program typeset with different styles.
\begin{Example}
\noindent
\begin{minipage}[t]{0.49\linewidth}
\begin{pygmented}[lang=c,gobble=4,sty=murphy]
#include<stdio.h>
main()
{ int n;
printf("Enter a number: ");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}
\end{pygmented}
\end{minipage}
\hfil
\begin{minipage}[t]{0.49\linewidth}
\begin{pygmented}[lang=c,gobble=4,sty=trac]
#include<stdio.h>
main()
{ int n;
printf("Enter a number: ");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}
\end{pygmented}
\end{minipage}
\end{Example}
\subsection{\texttt{font}}
The value of the option \verb|font| is typeset before the content of the
listing. Usualy it is used to specify a font to be used. See the
following example.
\begin{Example}
\begin{pygmented}[lang=scala,font=\rmfamily\scshape\large]
object bigint extends Application {
def factorial(n: BigInt): BigInt =
if (n == 0) 1 else n * factorial(n-1)
val f50 = factorial(50); val f49 = factorial(49)
println("50! = " + f50)
println("49! = " + f49)
println("50!/49! = " + (f50 / f49))
}
\end{pygmented}
\end{Example}
\subsection{\texttt{colback}}
The option \verb|colback| can be used to choose a background color, as
is shown in the folowing example.
\begin{Example}
\begin{pygmented}[lang=fsharp,colback=green!25]
let rec factorial n =
if n = 0
then 1
else n * factorial (n - 1)
System.Console.WriteLine(factorial anInt)
\end{pygmented}
\end{Example}
\subsection{\texttt{gobble}}
The option \verb|gobble| specifies the number of characters to suppress
at the beginning of each line (up to a maximum of 9). This is mainly
useful when environments are indented (Default: empty — no character
suppressed).
\begin{Example}
A code snippet inside a minipage:
\begin{minipage}[t]{.5\linewidth}
\begin{pygmented}[lang=d,gobble=8]
ulong fact(ulong n)
{
if(n < 2)
return 1;
else
return n * fact(n - 1);
}
\end{pygmented}
\end{minipage}
\end{Example}
\subsection{\texttt{tabsize}}
The option \verb|tabsize| specifies the number of of spaces given by a
tab character (Default: 8).
\begin{Verbatim}[showtabs,tabsize=1]
\begin{pygmented}[lang=common-lisp,tabsize=4]
;; Triple the value of a number
(defun triple (X)
"Compute three times X."
(* 3 X))
\end{pygmented}
\end{Verbatim}
\begin{pygmented}[lang=common-lisp,tabsize=4]
;; Triple the value of a number
(defun triple (X)
"Compute three times X."
(* 3 X))
\end{pygmented}
\subsection{\texttt{linenos}, \texttt{linenostart}, \texttt{linenostep}, \texttt{linenosep}}
The lines of a listing can be numbered. The followig options control
numbering of lines.
\begin{itemize}
\item Line numbering is enabled or disable with the \verb|linenos|
boolean option.
\item The number used for the first line can be set with the option
\verb|linenostart|.
\item The step between numbered lines can be set with the option
\verb|linenostep|.
\item The space between the line number and the line of the listing
can be set with the option \verb|linenosep|.
\end{itemize}
In the followig listing you can see a Scheme function to calculate the
factorial of a number.
\begin{Example}
\begin{pygmented}[lang=scheme,linenos,linenostart=1001,linenostep=2,linenosep=5mm]
;; Building a list of squares from 0 to 9.
;; Note: loop is simply an arbitrary symbol used as
;; a label. Any symbol will do.
(define (list-of-squares n)
(let loop ((i n) (res '()))
(if (< i 0)
res
(loop (- i 1) (cons (* i i) res)))))
\end{pygmented}
\end{Example}
\subsection{\texttt{caption} and \texttt{label}}
The option \verb|caption| can be used to set a caption for the listing.
The option \verb|label| allows the assignment of a label to the listing.
Here is an example:
\begin{Example}
\begin{pygmented}[lang=c++,label=lst:test,caption=A \textbf{C++} example]
// This program adds two numbers and prints their sum.
#include <iostream>
int main()
{
int a;
int b;
int sum;
sum = a + b;
std::cout << "The sum of " << a << " and " << b
<< " is " << sum << "\n";
return 0;
}
\end{pygmented}
\end{Example}
\begin{Example}
Listing \ref{lst:test} is a C++ program.
\end{Example}
\subsection{\texttt{texcomments}, \texttt{mathescape} and \texttt{escapeinside}}
The option \verb|texcomments|, if set to \texttt{true}, enables \LaTeX{}
comment lines. That is, LaTex markup in comment tokens is not escaped
so that \LaTeX{} can render it.
The \verb|mathescape|, if set to \texttt{true}, enables \LaTeX{} math
mode escape in comments. That is, \verb|$...$| inside a comment will
trigger math mode.
The option \verb|escapeinside|, if set to a string of length two,
enables escaping to \LaTeX{}. Text delimited by these two characters
is read as \LaTeX{} code and typeset accordingly. It has no effect in
string literals. It has no effect in comments if \verb|texcomments| or
\verb|mathescape| is set.
Some examples follows.
\begin{Example}
\begin{pygmented}[lang=c++,texcomments]
#include <iostream>
using namespace std;
main()
{
cout << "Hello World"; // prints \underline{Hello World}
return 0;
}
\end{pygmented}
\end{Example}
\begin{Example}
\begin{pygmented}[lang=python,mathescape]
# Returns $\sum_{i=1}^{n}i$
def sum_from_one_to(n):
r = range(1, n + 1)
return sum(r)
\end{pygmented}
\end{Example}
\begin{Example}
\begin{pygmented}[lang=c,escapeinside=||]
if (|\textit{condition}|)
|\textit{command$_1$}|
else
|\textit{command$_2$}|
\end{pygmented}
\end{Example}
\subsection{\texttt{inline method} and \texttt{boxing method}}
After being prettified by Pygments, the listings are enclosed in a
command (for \verb|\pyginline|) or in an environment (for
\verb|pygmented| and \verb|inputpygmented|). By default
\verb|\pyginline| uses the command \verb|\efbox| from the \texttt{efbox}
package, and \verb|pygmented| and \verb|inputpygmented| use the
environment \verb|mdframed| from the \texttt{mdframed} package.
The enclosing command or environment should be configurable using a list
of key-value pairs written between square brackets.
The enclosing command for
\verb|\pyginline| can be changed with the option
\verb|inline method|. For instance, in the following the command
\verb|\tcbox| from the \verb|tcolorbox| package is used:
\begin{Example}
In the previous Java program,
\pyginline[lang=java,inline method=tcbox]|"Factorial of "| is a
literal string.
\end{Example}
The enclosing environment for \verb|pygmented| and
\verb|inputpygmented| can be changed with the option
\verb|boxing method|. For instance, here is a hello world program in
C\#, enclosed in a \verb|tcolorbox| environment:
\begin{Example}
\begin{pygmented}[lang=csharp,boxing method=tcolorbox]
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
\end{pygmented}
\end{Example}
Any option unknown to Pygmen\TeX{} are passed to the enclosing command
or environment.
For instance:
\begin{Example}
\begin{pygmented}[lang=xml,boxing method=tcolorbox,colframe=red,boxrule=2mm]
<!-- This is a note -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
\end{pygmented}
\end{Example}
\section{Setting global options for Pygmen\TeX{}}
Global options can be setting using the \verb|setpygmented| command.
See the examples that follows.
\begin{Example}
\setpygmented{lang=haskell, colback=red!30, font=\ttfamily\small}
\begin{pygmented}[]
sum :: Num a => [a] -> a
sum [] = 0
sum (x:xs) = x + sum xs
\end{pygmented}
\end{Example}
\begin{Example}
\begin{pygmented}[colback=blue!20, boxing method=tcolorbox]
elem :: Eq a => a -> [a] -> Bool
elem _ [] = False
elem x (y:ys) = x == y || elem x ys
\end{pygmented}
\end{Example}
\begin{Example}
\setpygmented{lang=snobol}
\begin{pygmented}[]
OUTPUT = "What is your name?"
Username = INPUT
OUTPUT = "Thank you, " Username
END
\end{pygmented}
\end{Example}
\begin{Example}
\setpygmented{test/.style={colback=yellow!33,boxing method=tcolorbox,colframe=blue}}
\begin{pygmented}[test, lang=vbnet]
Module Module1
Sub Main()
Console.WriteLine("Hello, world!")
End Sub
End Module
\end{pygmented}
\end{Example}
\begin{Example}
\begin{pygmented}[lang=tcl]
puts "Hello, world!"
\end{pygmented}
\end{Example}
\section{More examples of inline code snippets}
\begin{Example}
An inline source code snippet:
\pyginline[lang=c]|const double alfa = 3.14159;|.
This is a C declaration with initialization.
\end{Example}
\begin{Example}
\pyginline[lang=prolog,colback=yellow]=avo(A,B) :- pai(A,X), pai(X,B).=
is a Prolog clause. Its head is
\pyginline[lang=prolog,sty=emacs,colback=yellow,linecolor=red]=avo(A,B)=
and its body is
\pyginline[lang=prolog,sty=vim,colback=black,hidealllines]=pai(A,X), pai(X,B)=.
\end{Example}
\begin{Example}
See the identifier \pyginline[inline method=efbox,colback=green!25]|variable|,
which names something. String literals in C looks like
\pyginline[lang=c,inline method=tcbox,colback=blue!20,boxrule=2pt]|"hello, world!\n"|.
\end{Example}
\setpygmented{colback=shadecolor}
\begin{Example}
This one
\pyginline[lang=ocaml,font=\ttfamily\scriptsize,topline=false]:let x = [1;2;3] in length x:
is an OCaml expression with local bindings. With OCaml one can do
imperative, functional and object oriented programming.
\end{Example}
\begin{Example}
Now some Java code:
\pyginline[lang=java,sty=colorful,font=\ttfamily\itshape,linewidth=1pt]|public int f(double x)|.
This is a method header.
\end{Example}
\section{More examples of displayed code snippets}
\setpygmented{lang=scheme,colback=shadecolor,sty=emacs}
In listing \ref{lst:fact} you can see a function definition in the
Scheme language. This function computes the factorial of a natural
number.
\newline\rule{\linewidth}{2pt}
\begin{pygmented}[
sty=emacs,
linenos,
label=lst:fact,
caption=A Scheme function.
]
(define fact
(lambda (n)
(if (= n 0)
1
(* n (fact (- n 1))))))
\end{pygmented}
Here you have some more code to further testing the package. Listing
\ref{lst:haskell} is a Haskell program. When run this program interacts
with the user asking the user name, reading a line input by the user,
and showing a greeting message to the user.
\inputpygmented[%
lang=haskell,
linenos,
linenostart=79831,
innerlinecolor=yellow, innerlinewidth=6pt,
middlelinecolor=blue, middlelinewidth=10pt,
outerlinecolor=green, outerlinewidth=12pt,
roundcorner=4,
colback=shadecolor,
caption=A haskell interactive program,
label=lst:haskell,
]{pygmentex_demo.hs}
This is a rule:
\noindent\rule{\linewidth}{2pt}
Now a Pascal procedure:
\inputpygmented[
lang=delphi,
linewidth=1.5pt,
font=\ttfamily\sffamily\large,
colback=yellow
]{pygmentex_demo.delphi}
and a Pascal program
\inputpygmented[lang=pascal,linenos,linenostart=5801]{pygmentex_demo.pas}
A Python code snippet:
\inputpygmented[
lang=python,
sty=emacs,
linenos,
linenostep=3,
linewidth=1pt,
colback=lightgreen
]{pygmentex_demo.py}
\section{Using code snippets in environments}
The following is a \textbf{description} environment.
\begin{description}
\item[An item] \lipsum[31]
\begin{pygmented}[lang=scala,colback=yellow,
% title=Item A
]
def qsort(xs: List[Int]): List[Int] =
xs match {
case Nil =>
Nil
case pivot :: tail =>
qsort(tail filter { _ < pivot }) :::
pivot :: qsort(tail filter { _ >= pivot })
}
\end{pygmented}
\lipsum[32]
\item[Another item] \lipsum[33]
\begin{pygmented}[lang=lua,colback=yellow]
function entry0 (o)
N=N + 1
local title = o.title or '(no title)'
fwrite('<LI><A HREF="#%d">%s</A>\n', N, title)
end
\end{pygmented}
\lipsum[34]
\end{description}
\section{A long program}
Here you can read the source code for a hand written lexical analyser
for the \emph{straight-line} programming language that I have developed
in Java.
\inputpygmented[boxing method=mdframed,lang=java,sty=autumn,colback=red!8,font=\ttfamily\small,tabsize=2,frametitle=\emph{Ad hoc} lexical analyser]{pygmentex_demo.java}
\section{Some fancy examples using \texttt{tcolorbox}}
The followig example uses \texttt{tcolorbox} to typeset the code
listing.
\newcounter{example}
\newlength{\examlen}
\colorlet{colexam}{red!75!black}
\begin{pygmented}[boxing method=tcolorbox,lang=scala,
title=Example \arabic{example}: hello from \texttt{Scala},
code={\refstepcounter{example}%
\settowidth{\examlen}{\Large\bfseries Example \arabic{example}}},%
coltitle=colexam,fonttitle=\Large\bfseries,
enhanced,breakable,
before=\par\medskip,
parbox=false,
frame hidden,interior hidden,segmentation hidden,
boxsep=0pt,left=0pt,right=3mm,toptitle=2mm,pad at break=0mm,
overlay unbroken={\draw[colexam,line width=1pt] (frame.north west)
--([xshift=-0.5pt]frame.north east)--([xshift=-0.5pt]frame.south east)
--(frame.south west);
\draw[colexam,line width=2pt] ([yshift=0.5pt]frame.north west)
-- +(\examlen,0pt);},
overlay first={\draw[colexam,line width=1pt] (frame.north west)
--([xshift=-0.5pt]frame.north east)--([xshift=-0.5pt]frame.south east);
\draw[red!75!black,line width=2pt] ([yshift=0.5pt]frame.north west)
-- +(\examlen,0pt);},
overlay middle={\draw[colexam,line width=1pt] ([xshift=-0.5pt]frame.north east)
--([xshift=-0.5pt]frame.south east); },
overlay last={\draw[colexam,line width=1pt] ([xshift=-0.5pt]frame.north east)
--([xshift=-0.5pt]frame.south east)--(frame.south west);}%
]
object HelloWorld extends App {
println("Hello, world!")
}\end{pygmented}
\begin{pygmented}[boxing method=tcolorbox,lang=java,
enhanced,colback=blue!10!white,colframe=orange,top=4mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
overlay unbroken and first={%
\node[rectangle,rounded corners,draw=black,fill=blue!20!white,
inner sep=1mm,anchor=west,font=\small]
at ([xshift=4.5mm]frame.north west) {\strut\textbf{My fancy title}};},
]
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!")
}
}
\end{pygmented}
\begin{pygmented}[boxing method=tcolorbox,lang=haskell,
enhanced,sharp corners=uphill,
colback=blue!25!white,colframe=blue!25!black,coltext=blue!90!black,
fontupper=\Large\bfseries,arc=6mm,boxrule=2mm,boxsep=5mm,
borderline={0.3mm}{0.3mm}{white}
]
module Main (main) where
main :: IO ()
main = putStrLn "Hello, world!"
\end{pygmented}
\begin{pygmented}[boxing method=tcolorbox,lang=c++,
enhanced,frame style image=blueshade.png,
opacityback=0.75,opacitybacktitle=0.25,
colback=blue!5!white,colframe=blue!75!black,
title=My title
]
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "Hello, world!" << endl;
return 0;
}
\end{pygmented}
\begin{pygmented}[boxing method=tcolorbox,lang=d,
enhanced,attach boxed title to top center={yshift=-3mm,yshifttext=-1mm},
colback=blue!5!white,colframe=blue!75!black,colbacktitle=red!80!black,
title=My title,fonttitle=\bfseries,
boxed title style={size=small,colframe=red!50!black}
]
/* This program prints a
hello world message
to the console. */
import std.stdio;
void main()
{
writeln("Hello, World!");
}
\end{pygmented}
\section{Some fancy examples using \texttt{mdframed}}
The followig example uses \texttt{mdframed} to typeset the code listing.
\global\mdfdefinestyle{exampledefault}{%
linecolor=red,linewidth=3pt,%
leftmargin=1cm,rightmargin=1cm
}
\begin{pygmented}[boxing method=mdframed,lang=ada,style=exampledefault]
with Ada.Text_IO;
procedure Hello_World is
use Ada.Text_IO;
begin
Put_Line("Hello, world!");
end;
\end{pygmented}
\global\mdfapptodefinestyle{exampledefault}{%
topline=false,bottomline=false,
}
\begin{pygmented}[boxing method=mdframed,lang=pascal,style=exampledefault,frametitle={Saying \emph{hello} from Pascal}]
program HelloWorld;
begin
WriteLn('Hello, world!');
end.
\end{pygmented}
\global\mdfdefinestyle{separateheader}{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=blue!20]
{\strut Saying \emph{hello} in Modula-2};},
innertopmargin=10pt,linecolor=blue!20,%
linewidth=2pt,topline=true,
frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
frametitlerule=false,
backgroundcolor=white,
}
\begin{pygmented}[boxing method=mdframed,lang=modula2,style=separateheader]
MODULE Hello;
FROM STextIO IMPORT WriteString;
BEGIN
WriteString("Hello World!");
END Hello.
\end{pygmented}
\tikzset{titregris/.style =
{draw=gray, thick, fill=white, shading = exersicetitle, %
text=gray, rectangle, rounded corners, right,minimum height=.7cm}}
\pgfdeclarehorizontalshading{exersicebackground}{100bp}
{color(0bp)=(green!40); color(100bp)=(black!5)}
\pgfdeclarehorizontalshading{exersicetitle}{100bp}
{color(0bp)=(red!40);color(100bp)=(black!5)}
\newcounter{exercise}
\renewcommand*\theexercise{Exercise~n\arabic{exercise}}
\makeatletter
\def\mdf@@exercisepoints{}%new mdframed key:
\define@key{mdf}{exercisepoints}{%
\def\mdf@@exercisepoints{#1}
}
\mdfdefinestyle{exercisestyle}{%
outerlinewidth=1em,outerlinecolor=white,%
leftmargin=-1em,rightmargin=-1em,%
middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
apptotikzsetting={\tikzset{mdfbackground/.append style ={%
shading = exersicebackground}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{exercise}},
singleextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}\hbox{~}};
\ifdefempty{\mdf@@exercisepoints}%
{}%
{\node[titregris,left,xshift=-1cm] at (P)%
{~\mdf@frametitlefont{\mdf@@exercisepoints points}\hbox{~}};}%
},
firstextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}\hbox{~}};
\ifdefempty{\mdf@@exercisepoints}%
{}%
{\node[titregris,left,xshift=-1cm] at (P)%
{~\mdf@frametitlefont{\mdf@@exercisepoints points}\hbox{~}};}%
},
}
\makeatother
\begin{pygmented}[boxing method=mdframed,lang=go,style=exercisestyle]
// hello world in 'go'
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
\end{pygmented}
\begin{pygmented}[boxing method=mdframed,lang=objective-c,style=exercisestyle,exercisepoints=10]
/* hello from objective-c */
#import <stdio.h>
#import <Foundation/Foundation.h>
int main(void)
{
NSLog(@"Hello, world!\n");
return 0;
}
\end{pygmented}
\mdfdefinestyle{another}{%
linecolor=red,middlelinewidth=2pt,%
frametitlerule=true,%
apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
shade,left color=white, right color=blue!20}}},
frametitlerulecolor=green!60,
frametitlerulewidth=1pt,
innertopmargin=\topskip,
}
\begin{pygmented}[boxing method=mdframed,lang=c,style=another,frametitle={Hello from C}]
#include <stdio.h>
int main(int argc, char **argv) {
printf("Hello, world!\n");
return 0;
}
\end{pygmented}
\section{Debugging}
Paths given to \verb|\inputpygmented| should be relative to the top level project
directory, not to the file that contains the command (if that file is in a
subdirectory). Pygmen\TeX generates only a single top-level \texttt{.snippets} file,
and paths are not munged to account for code in subdirectories.
\section{Conclusion}
That is all.
\end{document}
|