Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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,
frame=shadowbox,
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.

share|improve this question
1  
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
add comment

3 Answers

up vote 22 down vote accepted

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.

share|improve this answer
add comment

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,
frame=shadowbox,
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}
share|improve this answer
    
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
add comment

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}

Result with default settings

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}

Result with optimized <code>basewidth</code>

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}

Result with lmvtt and flexible columns

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

Result with full flexible columns

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.

share|improve this answer
    
Heiko: Insanely great answer. Thanks for the followup! –  Paul Nathan Jul 19 '13 at 20:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.