# Shrinking monospace style for listings package

I am working on a thesis and putting together a "pretty" listing. I'm looking at setting up a small font style for a code view. What I have here is:

\usepackage{listings}
\lstset{language=C,
numberstyle=\footnotesize,
basicstyle=\footnotesize,
numbers=left,
stepnumber=1,
breaklines=true}
%\usepackage[subsection]{placeins}
\usepackage{float}


But this comes out in a typical kerned font, vs. I am looking for smaller monospace, say, 10 points or so.

-
Welcome to TeX.sx! Usually you should add a full minimal working example (MWE) that illustrates your problem. Then we can see what packages you are using etc. Here the \lstset seems to be enough, however. – Martin Scharrer Oct 29 '11 at 6:56
@MartinScharrer: Thanks! I will remember. – Paul Nathan Oct 29 '11 at 17:11

Use the default

basicstyle=\footnotesize\ttfamily,...


or better with package microtype

\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\newcommand\Small{\fontsize{9}{9.2}\selectfont}
\newcommand*\LSTfont{\Small\ttfamily\SetTracking{encoding=*}{-60}\lsstyle}
...
\begin{lstlisting}[basicstyle=\LSTfont,...]
...


Which gives a better result.

-
Any recommendation to LuaLaTeX users on what to do here? – Ingo Jul 24 '14 at 10:18
See documentation of package microtype – Herbert Jul 24 '14 at 10:54

You are missing a \ttfamily in the basicstyle. If this doesn't give you the size you want try \scriptsize or even \tiny instead of \footnotesize. You don't need to add the same size for the numberstyle again because basicstyle is used for everything by default.

\documentclass{article}
\usepackage{listings}
\lstset{language=C,
numberstyle=\footnotesize,
basicstyle=\ttfamily\footnotesize,
numbers=left,
stepnumber=1,
breaklines=true}

\begin{document}
Normal text

\begin{lstlisting}
int a, b, c;
b = 2;
c = b++;
a = b + c;
\end{lstlisting}

Normal text
\end{document}

-
scriptsize worked for me, because it's between tiny and footnote size. it was the best trade off. – OneWorld Oct 8 '12 at 8:27

The "kerned" appearance comes from the default settings of package listings.

## Default settings with basicstyle=\ttfamily

Package listings uses columns=fixed as default. It provides the best vertical alignment, as you can see the code in a mono-spaced font in an source code editor. The settings means, the character are placed in boxes of width 0.6em:

\documentclass{article}
% Quick alignment test:
%   MMMM a
%   iiii a
\usepackage{listings}
\lstset{
basicstyle  = \ttfamily,
% basewidth = {.5em,0.5em},
% columns   = flexible,
% columns   = fullflexible,
}
% \usepackage[T1]{fontenc}
% \usepackage[variablett]{lmodern}
\begin{document}
\lstinputlisting[
language={[LaTeX]{TeX}},
]{\jobname.tex}
\end{document}


## Optimized basewidth

The width 0.6em for the fixed columns takes into account that wider characters should also fit into the box without overlapping the adjacent charactes. However mono-spaced fonts have already a fixed width for all characters. The width of a character is usually 0.5em, where the font designer has already squeezed the broader glyphs. Thus we can reduce basewidth from 0.6em,0.45em to 0.5em,0.5em. The second value is used for columns=flexible. This mode does not align all characters, but words. Words usually have narrower and wider characters, thus the average is smaller than the width for the widest character. But this does not make sense for mono-spaced fonts. Smaller values than the glyph width will only disturb the vertical alignment.

\documentclass{article}
% Quick alignment test:
%   MMMM a
%   iiii a
\usepackage{listings}
\lstset{
basicstyle = \ttfamily,
basewidth  = {.5em,0.5em},
% columns  = flexible,
% columns  = fullflexible,
}
% \usepackage[T1]{fontenc}
% \usepackage[variablett]{lmodern}
\begin{document}
\lstinputlisting[
language={[LaTeX]{TeX}},
]{\jobname.tex}
\end{document}


## Variable width typewriter font

If the vertical alignment is not too important (as in the lines of \lstset in the example), then a variable width typewriter font can be used. The Latin Modern fonts (further development of the Computer Modern fonts), provide such a font that can be enabled via option variablett of package lmodern. (Alternatively \fontfamily{lmvtt}\selectfont could be used.)

Because setting a variable width font in fixed columns looks uglier than using a mono-spaced font, the example uses columns=flexible. Also the basewidth is reduced to 0.4em (default is 0.45em leaves too many gaps).

\documentclass{article}
% Quick alignment test:
%   MMMM a
%   iiii a
\usepackage{listings}
\lstset{
basicstyle = \ttfamily,
basewidth  = {.5em,0.4em},
columns    = flexible,
% columns  = fullflexible,
}
\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}
\begin{document}
\lstinputlisting[
language={[LaTeX]{TeX}},
]{\jobname.tex}
\end{document}


• Drawback of the flexible columns settings is that the vertical alignment inside the lines might get lost. The line indenting works, however.
• As additional advantage the Latin Modern fonts provides a bold version (also in the mono-spaced variante).

## Variable width typewriter font with full flexible columns

And now the example with setting columns=fullflexible:

\documentclass{article}
% Quick alignment test:
%   MMMM a
%   iiii a
\usepackage{listings}
\lstset{
basicstyle = \ttfamily,
basewidth  = {.5em,0.4em},
% columns  = flexible,
columns    = fullflexible,
}
\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}
\begin{document}
\lstinputlisting[
language={[LaTeX]{TeX}},
]{\jobname.tex}
\end{document}


## Summary

• Instead of Computer Modern fonts I would use Latin Modern fonts, because they provide more font variants including bold.

• If the source code contains some kind of tables and the vertical alignment is important, a mono-spaced font with adjusted basewidth can be used:

\lstset{
basicstyle=\ttfamily,
basewidth={0.5em,0.5em},
}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

• If the source code only requires indented lines as vertical alignment, a variable width typewriter font can be used:

\lstset{
basicstyle=\ttfamily,
% basewidth={0.5em,0.4em}, % for other columns settings
columns=fullflexible,
}
\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}


This also decreases the line lengths, an advantage, if the code contains many long lines that would not fit in the available space of the line.

-
Heiko: Insanely great answer. Thanks for the followup! – Paul Nathan Jul 19 '13 at 20:05
Thanks a lot for basewidth, that's the parameter I was looking for. – Maxime Jun 26 '14 at 12:49