Underleaf Logo
Underleaf
BlogPricing
Log InTry Free

Learn LaTeX

Getting Started

  • What is LaTeX?
  • LaTeX for Beginners

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

MethodUse CaseSyntax Highlighting
\verbInline code snippetsNo
verbatimSimple code blocksNo
listingsCode with basic highlightingYes (basic)
mintedProfessional syntax highlightingYes (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++JavaScriptHTMLCSSSQLPHPRubyPerlRMatlabBashTeX

Method 4: The minted Package (Advanced)

For professional-quality syntax highlighting, use the minted package. It uses Pygments for highlighting, which supports 300+ languages:

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:

OptionDescriptionExample
numbersLine numbers positionleft, right, none
frameFrame around codesingle, lines, none
captionCode captioncaption=My Code
labelReference labellabel=lst:example
breaklinesWrap long linestrue / false

Complete Example Document

Best Practices

  • Use verbatim for quick, simple code snippets without highlighting needs
  • Use listings for most use cases - it's reliable and doesn't require external dependencies
  • Use minted when 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)

Try it free

Skip the syntax — let AI write it.

Snap a photo of an equation, a table, or full handwritten notes and Underleaf turns it into clean, compilable LaTeX in seconds. 10 free credits every month, no credit card required.

Convert your page
Handwritten notes converted into a typeset LaTeX document

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

MethodUse CaseSyntax Highlighting
\verbInline code snippetsNo
verbatimSimple code blocksNo
listingsCode with basic highlightingYes (basic)
mintedProfessional syntax highlightingYes (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++JavaScriptHTMLCSSSQLPHPRubyPerlRMatlabBashTeX

Method 4: The minted Package (Advanced)

For professional-quality syntax highlighting, use the minted package. It uses Pygments for highlighting, which supports 300+ languages:

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:

OptionDescriptionExample
numbersLine numbers positionleft, right, none
frameFrame around codesingle, lines, none
captionCode captioncaption=My Code
labelReference labellabel=lst:example
breaklinesWrap long linestrue / false

Complete Example Document

Best Practices

  • Use verbatim for quick, simple code snippets without highlighting needs
  • Use listings for most use cases - it's reliable and doesn't require external dependencies
  • Use minted when 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)
Underleaf Logo
Underleaf

Empowering students and researchers with AI-powered tools for academic writing.

Go to appContact us

Company

PricingBlogTutorialsConference TemplatesConference Deadlines

Free Tools

Image to LaTeXExcel to LaTeXArXiv to LaTeXTikZ GeneratorLaTeX Table GeneratorCitation GeneratorThesis GeneratorChrome ExtensionAll Tools

Ask AI about Underleaf

Ask ChatGPTAsk ClaudeAsk Perplexity

© 2026 Underleaf. All rights reserved.