Programming

LaTeX source code listing like in professional books

25 September 2026 · 6 min read

LaTeX source code listing like in professional books

Creating beautifully typeset source code listings, just like in professional textbooks and publications, is crucial for clear communication and enhanced readability in your LaTeX documents. Whether you’re writing a technical manual, a programming guide, or simply showcasing your code, achieving a polished and professional look is essential. This article explores the techniques and packages that empower you to elevate your source code presentation within LaTeX, ensuring your code is both visually appealing and easy to understand.

Choosing the Right Package: listings vs. minted

LaTeX offers several packages for typesetting source code, but two stand out: listings and minted. The listings package is built-in and offers a good range of features for basic code highlighting and formatting. It’s relatively easy to use and doesn’t require external dependencies. However, for more advanced syntax highlighting and customization, minted is the superior choice. Powered by the powerful Pygments library, minted supports a vast array of programming languages and offers fine-grained control over the appearance of your code listings.

Deciding which package to use depends on your specific needs. For simple code examples, listings might suffice. But for complex projects demanding accurate and visually appealing syntax highlighting, minted is the preferred option, offering a wider spectrum of customization options.

Working with the listings Package

The listings package is a robust tool for basic code formatting within LaTeX. To use it, simply include \usepackage{listings} in your document’s preamble. You can then embed your code using the lstlisting environment. For example:

\begin{lstlisting}[language=C++,caption=C++ Example,basicstyle=\ttfamily\footnotesize] include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; } \end{lstlisting} 

This will produce a nicely formatted C++ code block with a caption and specified font style. The listings package also allows for customization of keywords, comments, and other code elements.

Harnessing the Power of minted

For truly professional-grade code listings, minted shines. This package leverages the Pygments library, providing syntax highlighting for a vast array of languages. To use minted, you’ll need to have Python and Pygments installed on your system. Include \usepackage{minted} in your preamble. Then, you can embed code like this:

\begin{minted}{python} import numpy as np def my_function(): print("Hello from Python!") my_function() \end{minted} 

This will generate a beautifully highlighted Python code block. minted offers an extensive set of options for customizing the look and feel of your code, including themes, line numbers, and more. Its integration with Pygments allows for accurate and visually appealing syntax highlighting, crucial for complex code examples.

Advanced Formatting and Customization

Both listings and minted provide options for customizing the appearance of your code listings. You can adjust fonts, colors, background styles, and more. With minted, you can even specify different stylesheets or create your own. This level of control allows you to seamlessly integrate code listings into your document’s design, ensuring a cohesive and professional look. For instance, using the frame option with minted can add a border around your code block, further enhancing its visual separation from the surrounding text.

Choosing appropriate fonts and colors is essential for readability and aesthetics. Monospaced fonts like Consolas or Inconsolata are commonly used for code listings due to their clear and consistent character spacing. Consider the overall design of your document and select a style that complements it.

  • Use a consistent coding style guide.
  • Choose appropriate fonts and colors.
  1. Install the necessary packages.
  2. Include the package in your document preamble.
  3. Embed your code using the appropriate environment.

Infographic Placeholder: A visual guide comparing listings and minted.

As Donald Knuth, the creator of TeX, emphasizes, “The idea of typography is the arrangement of the printed page to make it most appealing to the eye and most effective in conveying its message.” Applying this principle to code listings within LaTeX is crucial for clarity and readability. By mastering the techniques presented in this article and utilizing the available packages, you can elevate your code presentation, creating professional-looking documents that effectively communicate complex technical information. Learn more about advanced LaTeX techniques.

  • Remember to choose the package that best suits your needs.
  • Experiment with different customization options.

Explore advanced LaTeX features like custom commands and environments to further refine your document’s presentation. For detailed documentation and examples, refer to the official LaTeX documentation and the CTAN package repository. Dive into the world of professional typesetting and unlock the full potential of LaTeX for your technical writing needs.

For further reading, explore these resources:

FAQ: Common Questions about LaTeX Code Listings

Q: How do I add line numbers to my code listings?

A: For both listings and minted, you can add line numbers using options within the environment. For listings, use numbers=left or numbers=right. For minted, use linenos. Refer to the respective package documentation for further customization options.

Question & Answer :
How should a latex source code listing look like to produce an output like in known books, for example one for the Spring Framework? I’ve tried with the latex listings package but wasn’t able to produce something that looked as nice as the one below. So I’m primarely interested in the formatting instructions to produce something like the sample below (from Manning’s sample chapter for Spring in Action):

From Manning’s Spring in Action

EDIT With the help especially of Tormod Fjeldskår here’s the complete snippet to produce the desired look:

\usepackage{listings} \usepackage{courier} \lstset{ basicstyle=\footnotesize\ttfamily, % Default font % numbers=left, % Location of line numbers numberstyle=\tiny, % Style of line numbers % stepnumber=2, % Margin between line numbers numbersep=5pt, % Margin between line numbers and text tabsize=2, % Size of tabs extendedchars=true, breaklines=true, % Lines will be wrapped keywordstyle=\color{red}, frame=b, % keywordstyle=[1]\textbf, % keywordstyle=[2]\textbf, % keywordstyle=[3]\textbf, % keywordstyle=[4]\textbf, \sqrt{\sqrt{}} stringstyle=\color{white}\ttfamily, % Color of strings showspaces=false, showtabs=false, xleftmargin=17pt, framexleftmargin=17pt, framexrightmargin=5pt, framexbottommargin=4pt, % backgroundcolor=\color{lightgray}, showstringspaces=false } \lstloadlanguages{ % Check documentation for further languages ... % [Visual]Basic, % Pascal, % C, % C++, % XML, % HTML, Java } % \DeclareCaptionFont{blue}{\color{blue}} % \captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}} \usepackage{caption} \DeclareCaptionFont{white}{\color{white}} \DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}} \captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}} 

Use it with this in your document:

\lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java} 

It seems to me that what you really want, is to customize the look of the captions. This is most easily done using the caption package. For instructions how to use this package, see the manual (PDF). You would probably need to create your own custom caption format, as described in chapter 4 in the manual.

Edit: Tested with MikTex:

\documentclass{report} \usepackage{color} \usepackage{xcolor} \usepackage{listings} \usepackage{caption} \DeclareCaptionFont{white}{\color{white}} \DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}} \captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white} % This concludes the preamble \begin{document} \begin{lstlisting}[label=some-code,caption=Some Code] public void here() { goes().the().code() } \end{lstlisting} \end{document} 

Result:

Preview