Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
How to Include Code in LaTeX
Including code in your LaTeX documents is essential for technical documentation, programming tutorials, and academic papers. This guide covers all methods from simple inline code to advanced syntax highlighting.
Quick Overview of Methods
| Method | Use Case | Syntax Highlighting |
|---|---|---|
\verb | Inline code snippets | No |
verbatim | Simple code blocks | No |
listings | Code with basic highlighting | Yes (basic) |
minted | Professional syntax highlighting | Yes (advanced) |
Method 1: Inline Code with \verb
For short inline code snippets, use the \verb command. The delimiter can be any character that doesn't appear in your code:
Output: Use print("Hello") to output text. The function array[i] accesses element i.
Method 2: Simple Code Blocks with verbatim
For basic code blocks without any formatting, use the verbatim environment:
def hello_world(): print("Hello, World!") hello_world()
Method 3: The listings Package
The listings package provides syntax highlighting and many customization options. First, add it to your preamble:
Then use it in your document:
Supported Languages in listings
The listings package supports many programming languages:
PythonJavaCC++JavaScriptHTMLCSSSQLPHPRubyPerlRMatlabBashTeXMethod 4: The minted Package (Advanced)
For professional-quality syntax highlighting, use the minted package. It uses Pygments for highlighting, which supports 300+ languages:
The minted package requires Python and Pygments to be installed. You also need to compile with --shell-escape flag.
Including Code from External Files
Both listings and minted can include code directly from files:
Inline Code with listings
For inline code with highlighting, use \lstinline:
Customizing Code Appearance
Common customization options for listings:
| Option | Description | Example |
|---|---|---|
numbers | Line numbers position | left, right, none |
frame | Frame around code | single, lines, none |
caption | Code caption | caption=My Code |
label | Reference label | label=lst:example |
breaklines | Wrap long lines | true / false |
Complete Example Document
Best Practices
- Use
verbatimfor quick, simple code snippets without highlighting needs - Use
listingsfor most use cases - it's reliable and doesn't require external dependencies - Use
mintedwhen you need the best syntax highlighting and don't mind the setup - Keep code examples concise and relevant to the document
- Use captions and labels to reference code listings in your text
- Consider using a monospace font that clearly distinguishes similar characters (0/O, 1/l/I)
In online LaTeX editors, the listings package works out of the box. For minted, enable "Shell escape" in the project settings.
Tip: Need help configuring code blocks? Underleaf's AI can generate properly formatted listings and minted environments from your code snippets instantly.
How to Include Code in LaTeX
Including code in your LaTeX documents is essential for technical documentation, programming tutorials, and academic papers. This guide covers all methods from simple inline code to advanced syntax highlighting.
Quick Overview of Methods
| Method | Use Case | Syntax Highlighting |
|---|---|---|
\verb | Inline code snippets | No |
verbatim | Simple code blocks | No |
listings | Code with basic highlighting | Yes (basic) |
minted | Professional syntax highlighting | Yes (advanced) |
Method 1: Inline Code with \verb
For short inline code snippets, use the \verb command. The delimiter can be any character that doesn't appear in your code:
Output: Use print("Hello") to output text. The function array[i] accesses element i.
Method 2: Simple Code Blocks with verbatim
For basic code blocks without any formatting, use the verbatim environment:
def hello_world(): print("Hello, World!") hello_world()
Method 3: The listings Package
The listings package provides syntax highlighting and many customization options. First, add it to your preamble:
Then use it in your document:
Supported Languages in listings
The listings package supports many programming languages:
PythonJavaCC++JavaScriptHTMLCSSSQLPHPRubyPerlRMatlabBashTeXMethod 4: The minted Package (Advanced)
For professional-quality syntax highlighting, use the minted package. It uses Pygments for highlighting, which supports 300+ languages:
The minted package requires Python and Pygments to be installed. You also need to compile with --shell-escape flag.
Including Code from External Files
Both listings and minted can include code directly from files:
Inline Code with listings
For inline code with highlighting, use \lstinline:
Customizing Code Appearance
Common customization options for listings:
| Option | Description | Example |
|---|---|---|
numbers | Line numbers position | left, right, none |
frame | Frame around code | single, lines, none |
caption | Code caption | caption=My Code |
label | Reference label | label=lst:example |
breaklines | Wrap long lines | true / false |
Complete Example Document
Best Practices
- Use
verbatimfor quick, simple code snippets without highlighting needs - Use
listingsfor most use cases - it's reliable and doesn't require external dependencies - Use
mintedwhen you need the best syntax highlighting and don't mind the setup - Keep code examples concise and relevant to the document
- Use captions and labels to reference code listings in your text
- Consider using a monospace font that clearly distinguishes similar characters (0/O, 1/l/I)
In online LaTeX editors, the listings package works out of the box. For minted, enable "Shell escape" in the project settings.
Tip: Need help configuring code blocks? Underleaf's AI can generate properly formatted listings and minted environments from your code snippets instantly.
Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
