5

I want to include code in my LaTeX document using the listings package.

My googling has not found me any configuration of the font size that actually fits approximately 80 columns to the width of the document.

Most examples I've seen use pre-defined sizes:

\lstset{
  basicstyle=\ttfamily\footnotesize,
  ...
}

So far the ones I have found have all been too large (\small, \footnotesize, \scriptsize) or too small (\tiny).

Is there no way to manually configure the size of the font (possibly without messing up all other font configuration), or is there at least a preconfigured setting that comes close to 80 columns?

I realize margins might be different etc., but so far all the ones I have seen have been way off.

0
7

This might go a little short, because of the discrete steps used. I try font sizes until eighty columns fit. It's important to use freely scalable fonts or the length will be shorter (it's up to you to decide), so either fix-cm or lmodern or a PostScript font for typewriter type.

\documentclass[11pt]{article}

% freely scalable fonts
\usepackage{fix-cm}

\usepackage{listings}

\usepackage{lipsum} % for the example

\newlength{\eightytt}
\newcommand{\testthewidth}{%
  \fontsize{\dimen0}{0}\selectfont
  \settowidth{\dimen2}{x}%
  \ifdim 80\dimen2>\textwidth
    \advance\dimen0 by -.1pt
    \expandafter\testthewidth
  \else
    \global\eightytt\dimen0
  \fi
}

\AtBeginDocument{%
  \dimen0=\csname f@size\endcsname pt
  \begingroup
  \ttfamily
  \testthewidth
  \endgroup
  \lstset{
    columns=fullflexible,
    basicstyle=\fontsize{\eightytt}{1.2\eightytt}\ttfamily
  }%
}

\begin{document}

\lipsum[2]
\begin{lstlisting}
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
\end{lstlisting}
\end{document}

enter image description here


If nonflexible columns are desired, we can take into account the fact that the default for the cell width is 0.6em in the current font:

\documentclass[11pt]{article}

% freely scalable fonts
\usepackage{fix-cm}

\usepackage{listings}

\usepackage{lipsum} % for the example

\newlength{\eightytt}
\newcommand{\testthewidth}{%
  \fontsize{\dimen0}{0}\selectfont
  \sbox0{x\global\dimen1=0.6em}%
  \ifdim 80\dimen1>\textwidth
    \advance\dimen0 by -.1pt
    \expandafter\testthewidth
  \else
    \global\eightytt\dimen0
  \fi
}

\AtBeginDocument{%
  \dimen0=\csname f@size\endcsname pt
  \begingroup
  \ttfamily
  \testthewidth
  \endgroup
  \lstset{
    basicstyle=\fontsize{\eightytt}{1.2\eightytt}\ttfamily
  }%
}

\begin{document}

\lipsum[2]
\begin{lstlisting}
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
\end{lstlisting}
\end{document}

enter image description here

8
  • Thank you for the suggestion. Flexible columns are not an option, however, as they mess up the indentation. I'm including Haskell code which is very dependent on indentation, so inaccurate indentation actually means incorrect code. I tried without flexible columns, and it still doesn't quite break on 80 columns. Right now basicstyle=\ttfamily\scriptsize is what comes closest (looks roughly similar to the output from your solution). Nov 5 '13 at 19:24
  • 1
    @jlicht “Flexible” columns with a monospaced font should give no problem with indentation.
    – egreg
    Nov 5 '13 at 22:04
  • @jlicht I've added the computation for non flexible columns.
    – egreg
    Nov 5 '13 at 22:19
  • I had to increase the iteration to 84 for some reason, possible because of a margin xleftmargin=23pt I have for my line numbers. It works now, however! Lengthy, but its a solution. Silly that it can't be done in an easier way, hopefully there'll be support in listings or an external package for it someday. Cheers, Accepted. Nov 5 '13 at 22:49
  • Uh-oh, it is not word wrapping anymore, though. Any idea why that might be? Nov 5 '13 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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