| 1 |
# PygmenTeX |
| 2 |
|
| 3 |
A Python-based LaTeX package for typesetting code listings using Pygments. |
| 4 |
|
| 5 |
Pygments is a generic syntax highlighter for general use in all kinds of |
| 6 |
software such as forum systems, wikis or other applications that need to |
| 7 |
prettify source code. |
| 8 |
|
| 9 |
**Requirements:** You need to have Python and Pygments installed. For instructions, please refer to the [Pygments home page](http://pygments.org/). |
| 10 |
|
| 11 |
## The PygmenTeX Difference: Efficiency through Batching |
| 12 |
|
| 13 |
Unlike other packages that call Pygments for *each* code snippet (a slow process), PygmenTeX collects all snippets and processes them in a **single batch**. This can lead to a significant reduction in compilation time for documents with many listings. |
| 14 |
|
| 15 |
## Quick Start |
| 16 |
|
| 17 |
1. **Installation:** Place the `pygmentex.sty` file in a directory where LaTeX can find it. Ensure the `pygmentex.py` script is executable and located in a directory in your `PATH`. |
| 18 |
|
| 19 |
2. **Use it in your LaTeX document:** |
| 20 |
```latex |
| 21 |
\documentclass{article} |
| 22 |
\usepackage{pygmentex} % Uses the 'default' style by default |
| 23 |
|
| 24 |
\begin{document} |
| 25 |
|
| 26 |
% Highlight a code snippet |
| 27 |
\begin{pygmented}[lang=c] |
| 28 |
#include <stdio.h> |
| 29 |
int main(void) |
| 30 |
{ |
| 31 |
int a, b, c; |
| 32 |
printf("Enter two numbers to add: "); |
| 33 |
scanf("%d%d", &a, &b); |
| 34 |
c = a + b; |
| 35 |
printf("Sum of entered numbers = %d\n", c); |
| 36 |
return 0; |
| 37 |
} |
| 38 |
\end{pygmented} |
| 39 |
|
| 40 |
% Highlight in line |
| 41 |
In this program, \pyginline[lang=c]|int| is a type and |
| 42 |
\pyginline[lang=c]|"Enter two numbers to add: "| is a literal |
| 43 |
string. |
| 44 |
|
| 45 |
% Highlight an external file |
| 46 |
\inputpygmented[lang=java, style=monokai]{Factorial.java} |
| 47 |
|
| 48 |
\end{document} |
| 49 |
``` |
| 50 |
|
| 51 |
3. **Compile the document with LaTeX (first run):** Snippets are collected into a `.snippets` file. |
| 52 |
4. **Run the Python Script:** Process the snippets with `pygmentex.py` to generate a `.pygmented` file. |
| 53 |
```bash |
| 54 |
pygmentex.py your_document.snippets |
| 55 |
``` |
| 56 |
5. **Compile the document with LaTeX (second run):** The final document is produced using the pre-formatted output. |
| 57 |
|
| 58 |
> **Note:** Shell escape (`--shell-escape`) is **not required**. The processing is a separate, explicit step. **However, if shell escape is enabled, the `pygmentex.py` script may be executed automatically.** |
| 59 |
|
| 60 |
For full documentation and examples, see the [compiled manual](manual/pygmentex-manual.pdf). |
| 61 |
|
| 62 |
## License |
| 63 |
|
| 64 |
This project is licensed under the LaTeX Project Public License (LPPL), version v1.3c or later. |