listings when you need portable code blocks with no external highlighter. Use minted when syntax quality matters and the build environment provides the latexminted executable and Pygments. On current TeX Live 2024+ installations, minted v3 can normally use trusted restricted shell escape without the unsafe global -shell-escape flag.
Quick answer: choose
listings for journals, CI systems, or editors where external commands may be unavailable. Choose minted for richer Pygments highlighting. TeX Live 2024+ with minted v3 usually compiles normally; older TeX Live and some MiKTeX setups require explicit permission for latexminted.Related topics: Text formatting | Fonts | Headers & footers | Errorslistings vs minted
Choose in 30 seconds
- Choose
listingsif the PDF must compile anywhere, the source is untrusted, or the publisher controls the build. - Choose
mintedif highlighting quality matters and you control or have verified the toolchain. - Choose
listingswhen you are unsure. You can switch later without changing the surrounding document structure.
Quick Start
Minimal listings setup
Use this when you want a simple code block and do not want to change your compiler settings.
Minimal minted setup
Use this when you want Pygments syntax highlighting and the build environment provides minted v3 and latexminted.
With an up-to-date TeX Live 2024+ installation, compile normally:
The usual minted failure
If minted does not compile, the problem is usually one of these:
minted,latexminted, or its Python dependencies are not installed- the TeX distribution does not permit
latexmintedthrough restricted shell escape - an older TeX Live or MiKTeX setup needs explicit permission
- the editor, CI runner, or publisher blocks all external commands
listings instead.
LaTeX Cloud Studio keeps unrestricted
-shell-escape disabled for untrusted compilation. Use listings for guaranteed portability. minted v3 may work through the restricted, trusted latexminted path when it is available in the active compiler image.Cache highlighted code for restricted builds
minted caches highlighted output in _minted by default. If the final build cannot run latexminted, generate the cache in an allowed environment and then enable frozencache:
Keep the generated cache with the document. With frozencache=true, compilation fails when a required cache entry is missing instead of trying to execute the highlighter. This is useful for publisher and CI workflows, but listings remains simpler when cached artifacts are not accepted.
Inline code
For short snippets inside a paragraph:The listings Package
Basic Code Formatting
Expected output:Expected 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
Expected output:Expected 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
Do I need Python, Pygments, and latexminted installed?
Do I need Python, Pygments, and latexminted installed?
minted v3 performs highlighting through latexminted, which uses Pygments. A current TeX package manager may install these components together; otherwise install latexminted and its dependencies in the Python environment used by the compiler. A document with a complete frozen cache no longer needs Python or Pygments during the final build.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 current
minted v3 on TeX Live 2024+ through its trusted restricted executable, use a verified frozen cache, or choose listings. If a journal or CI service forbids external execution and cached artifacts, listings is the portable choice.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
Common Options
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.
