Professional Book-Style Source Code Typesetting with LaTeX Listings Package

Nov 24, 2025 · Programming · 11 views · 7.8

Keywords: LaTeX | source code typesetting | listings package

Abstract: This article provides a comprehensive guide on achieving professional book-style source code typesetting in LaTeX documents using the listings and caption packages. Based on high-scoring Stack Overflow answers, it delves into essential configurations including basic style settings, syntax highlighting, frame customization, and caption formatting. Complete configuration examples and step-by-step implementation guidelines are provided, with special focus on Java code presentation optimization.

Introduction

In technical documentation and professional books, the quality of source code presentation significantly impacts reader experience. LaTeX, as a professional typesetting system, offers powerful code formatting capabilities through the listings package. This article systematically explains how to configure LaTeX for professional-level source code display based on high-quality solutions from the Stack Overflow community.

Core Configuration Analysis

Achieving professional book-style code typesetting requires fine-tuned configuration of the listings package. The following provides detailed analysis of key configuration parameters:

\lstset{
basicstyle=\footnotesize\ttfamily,
numberstyle=\tiny,
numbersep=5pt,
tabsize=2,
extendedchars=true,
breaklines=true,
keywordstyle=\color{red},
frame=b,
stringstyle=\color{white}\ttfamily,
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
showstringspaces=false
}

Caption Format Customization

Another crucial aspect of professional code listings is aesthetically pleasing caption formatting. The caption package enables custom caption styles:

\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}}

Complete Implementation Example

The following demonstrates a complete LaTeX document integrating the above configurations:

\documentclass{report}
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{courier}
\lstset{
basicstyle=\footnotesize\ttfamily,
numberstyle=\tiny,
numbersep=5pt,
tabsize=2,
extendedchars=true,
breaklines=true,
keywordstyle=\color{red},
frame=b,
stringstyle=\color{white}\ttfamily,
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
showstringspaces=false
}
\lstloadlanguages{Java}
\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}}
\begin{document}
\lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java}
\end{document}

Technical Summary

By properly configuring the listings and caption packages, code presentation comparable to professional technical books can be achieved. Key configurations include font selection, color schemes, frame settings, and caption format customization. This configuration approach works for both inline code and external code files imported via the \lstinputlisting command.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.