listings if you want code blocks that work without external tools. Use minted if you want better syntax highlighting and you can compile with -shell-escape. This guide covers both packages, inline code, file imports, and the common errors that usually break minted.
Quick answer:
listings is simpler and works in more build setups. minted uses Pygments, so it needs -shell-escape, but it gives better highlighting and more control over styles.Related topics: Text formatting | Fonts | Headers & footers | Errorslistings vs minted
| If you need… | Use | Why |
|---|---|---|
| A code block that compiles without shell-escape | listings | It stays inside LaTeX and is easier to use in restricted environments |
| Better syntax highlighting and more built-in themes | minted | It uses Pygments for language-aware highlighting |
| Inline code in ordinary text | \lstinline or \mintinline | Both packages support inline code |
| A build setup that works on locked-down CI or editors | listings | minted often fails when shell execution is disabled |
| Publication-style code examples | minted | Output is usually cleaner with less manual styling |
Quick Start
Minimal listings setup
Use this when you want a simple code block and do not want to change your compiler settings.
listings-quickstart.tex
Minimal minted setup
Use this when you want syntax highlighting and your editor or build command supports -shell-escape.
minted-quickstart.tex
The usual minted failure
If minted does not compile, the problem is usually one of these:
-shell-escapeis missing- Python or Pygments is not available in the build environment
- your editor or CI runner blocks external commands
listings instead.
Inline code
For short snippets inside a paragraph:inline-code.tex
The listings Package
Basic Code Formatting
Rendered Output
A code block with a light gray background and single-line frame border. Line numbers (1-10) appear in the left margin in a subtle gray color. The Python code displays in a monospace font with consistent formatting: the function definition, docstring, conditional logic, and loop are all clearly visible with proper indentation preserved.
Language-Specific Styling
Rendered Output
Language-specific syntax highlighting - Each style applies different colors and formatting:
- Python style: Blue keywords, red strings, gray comments, light blue background with left border
- Java style: Purple keywords, orange strings, green comments, top/bottom frame
- C++ style: Blue keywords, dark red strings, green comments, rounded frame
Custom Language Definitions
The minted Package
Basic minted Setup
Advanced minted Styling
Highlighting Specific Lines
Code Listings in Floats
Floating Code Listings
Code Listings with Subfigures
Inline Code and Escaping
Inline Code with Special Characters
Performance and Customization
Custom Color Schemes
Code Import from Files
Best Practices
Production-Ready Setup
Troubleshooting & FAQ
minted Error: shell escape enabled (or -shell-escape)?
minted Error: shell escape enabled (or -shell-escape)?
Most minted errors happen when LaTeX isn’t compiled with shell-escape. Fix by compiling with
--shell-escape (or enable it in your editor). In LaTeX Cloud Studio, shell-escape is enabled automatically for minted.Do I need Python and Pygments installed?
Do I need Python and Pygments installed?
Yes. minted uses Pygments (a Python library) for syntax highlighting. On local setups, install
python3 and pygments and compile with --shell-escape. Cloud Studio manages this for you.minted vs listings – which should I use?
minted vs listings – which should I use?
Use
listings for lightweight, dependency-free code blocks; choose minted for high-quality syntax highlighting and extensive styling. For journal-ready output, minted is usually preferred.How do I avoid shell-escape for strict environments?
How do I avoid shell-escape for strict environments?
Use
listings instead of minted, or pre-generate highlighted code externally and include as verbatim. Some journals forbid shell-escape; listings is safe.Colored backgrounds and line highlighting don’t appear
Colored backgrounds and line highlighting don’t appear
Ensure your document isn’t forcing monochrome (e.g., via print mode) and that options like
bgcolor, highlightlines, and frame are set on the environment or via \setminted{...}.Quick Reference
Essential Commands
| Package | Command | Purpose | Example | ||
|---|---|---|---|---|---|
| listings | \lstset{} | Global configuration | \lstset{language=Python} | ||
| listings | \begin{lstlisting} | Code block | \begin{lstlisting}[language=Java] | ||
| listings | \lstinline | Inline code | `\lstinline | code | ` |
| minted | \setminted{} | Global configuration | \setminted{fontsize=\small} | ||
| minted | \begin{minted} | Code block | \begin{minted}{python} | ||
| minted | \mintinline | Inline code | `\mintinline | code | ` |
Common Options
| Option | Effect | Example |
|---|---|---|
language | Set programming language | language=Python |
linenos | Show line numbers | linenos=true |
fontsize | Set font size | fontsize=\footnotesize |
breaklines | Allow line breaking | breaklines=true |
frame | Add frame around code | frame=single |
bgcolor | Background color | bgcolor=gray!10 |
Supported Languages
Both packages support many languages including:- Python, Java, C++, C, JavaScript, TypeScript
- HTML, CSS, SQL, LaTeX, Bash, PowerShell
- Go, Rust, Swift, Kotlin, Scala, Haskell
- MATLAB, R, Julia, Perl, Ruby, PHP
Next: Learn about Headers and footers for page decoration and numbering, or explore Multiple columns for layout.
